google / gtm-oauth2

Google Toolbox for Mac - OAuth 2 Controllers
Apache License 2.0
125 stars 70 forks source link

Loss of authorisation on the simulator iPhone SE #82

Closed Alexandeer closed 7 years ago

Alexandeer commented 7 years ago

I used the tutorial https://developers.google.com/drive/ios/quickstart to configure the system. My problem is that I have to re-authenticate every run the app on the simulator. This happens only on the iOS 10 simulators. In the ios 9.3 simulator everything works well.

static NSString *const kKeychainItemName = @"Drive API";
static NSString *const kClientID         = @"";

@interface ASGoogleCloudService () {

}

@property (nonatomic, strong) GTLServiceDrive *service;

@end

@implementation ASGoogleCloudService

- (instancetype)initialisation
{
    if (self)
    {
        self.service            = [[GTLServiceDrive alloc] init];
        self.service.authorizer = [GTMOAuth2ViewControllerTouch authForGoogleFromKeychainForName:kKeychainItemName clientID:kClientID clientSecret:nil];
        self.service.authorizer.shouldAuthorizeAllRequests = YES;
    }

    return self;
}

- (void)loginOnController:(UIViewController *)controller completion:(CompletionBlock)completion
{
    if (!self.service.authorizer.canAuthorize)
    {
        NSArray *scopes = [NSArray arrayWithObjects:
                           kGTLAuthScopeDrive,
                           kGTLAuthScopeDriveAppdata,
                           kGTLAuthScopeDriveFile,
                           kGTLAuthScopeDriveMetadata,
                           kGTLAuthScopeDriveMetadataReadonly,
                           kGTLAuthScopeDrivePhotosReadonly,
                           kGTLAuthScopeDriveReadonly,
                           kGTLAuthScopeDriveScripts,
                           nil];

        GTMOAuth2ViewControllerTouch *authController = [[GTMOAuth2ViewControllerTouch alloc] initWithScope:[scopes componentsJoinedByString:@" "]
                                                                                                  clientID:kClientID
                                                                                              clientSecret:nil
                                                                                          keychainItemName:kKeychainItemName
                                                                                         completionHandler:^(GTMOAuth2ViewControllerTouch *viewController, GTMOAuth2Authentication *auth, NSError *error) {
            if (error != nil)
            {
                self.service.authorizer = nil;

                if (completion)
                {
                    completion(nil, error);
                }
            }
            else {
                self.service.authorizer = auth;
                [controller dismissViewControllerAnimated:YES completion:nil];

                if (completion)
                {
                    completion(nil, nil);
                }
            }
        }];

        [controller.navigationController pushViewController:authController animated:YES];
    }
    else {
        if (completion)
        {
            completion(nil, nil);
        }
    }
}
thomasvl commented 7 years ago

What version of the library are you using? PR #77 & #81 switched to using a hack of storing the info in NSUserDefaults for the simulators because of some Apple bugs with keychain access there. I'm told the Xcode currently be beta fixes these problems.

Alexandeer commented 7 years ago

Thank you very much. It helped me a lot.