我在一个带有一个viewController的应用程序中使用GMTOAuth2,它工作正常,然后我将相同的代码添加到另一个包含更多viewControllers的应用程序中,它加载了带有用户名和密码文本字段的webView,但是如果我单击此视图中的任何内容,它会崩溃线程 1:EXC_BAD_ACCESS (code=2, address=0xbf7ffffc) with void SendDelegateMessage(NSInvocation *): delegate (webView:decidePolicyForNavigationAction:request:frame:decisionListener:) 在等待 10 秒后未能返回。主运行循环模式:控制台中的 kCFRunLoopDefaultMode。我的授权部分代码如下所示:
- (void)awakeFromNib {
[super awakeFromNib];
GTMOAuth2Authentication *auth = nil;
auth = [GTMOAuth2ViewControllerTouch authForGoogleFromKeychainForName:kKeychainItemName
clientID:kMyClientID
clientSecret:kMyClientSecret];
if (auth)
{
[auth authorizeRequest:nil
delegate:self
didFinishSelector:@selector(authentication:request:finishedWithError:)];
}
}
- (void)signInToGoogle {
[self signOut];
NSString *keychainItemName = kKeychainItemName;
NSString *scope = @"https://spreadsheets.google.com/feeds https://docs.google.com/feeds";
NSString *clientID = kMyClientID;
NSString *clientSecret = kMyClientSecret;
if ([clientID length] == 0 || [clientSecret length] == 0) {
NSString *msg = @"The sample code requires a valid client ID and client secret to sign in.";
[self displayAlertWithMessage:msg];
return;
}
SEL finishedSel = @selector(viewController:finishedWithAuth:error:);
GTMOAuth2ViewControllerTouch *viewController = [[GTMOAuth2ViewControllerTouch alloc] initWithScope:scope
clientID:clientID
clientSecret:clientSecret
keychainItemName:keychainItemName
finishedSelector:finishedSel];
delegate:self
[self presentViewController:viewController animated:YES completion:^{}];
[[self navigationController] pushViewController:viewController animated:YES];
}
- (void)authentication:(GTMOAuth2Authentication *)auth
request:(NSMutableURLRequest *)request
finishedWithError:(NSError *)error {
if (error != nil) {
NSLog(@"error!");
} else {
[self isAuthorizedWithAuthentication:auth];
self.auth = auth;
}
}
并将相同的类从第一个应用程序复制到第二个应用程序,并制作完全相同的 nib 文件。