做国际化的app, google登录是一定少不了的, 今天介绍一种google登录引起的崩溃.
先看堆栈
Thread 0 (crashed)
0 CoreFoundation!__exceptionPreprocess + 0x7c
Found by: given as instruction pointer in context
1 libobjc.A.dylib!objc_exception_throw + 0x34
Found by: previous frame's frame pointer
2 CoreFoundation!+[NSException raise:format:] + 0x70
Found by: previous frame's frame pointer
3 Hago!-[GIDSignIn assertValidUIDelegate] + 0xc8
Found by: previous frame's frame pointer
4 Hago!-[GIDSignIn signInWithOptions:] + 0x5c
Found by: previous frame's frame pointer
5 Hago!-[GIDSignIn signIn] + 0x44
Found by: previous frame's frame pointer
6 Hago!-[HGTpLoginMgr login:timeout:] [PKTpLoginMgr.m : 68 + 0x10]
Found by: previous frame's frame pointer
7 Hago!-[HGLoginModule thirdpartyLogin:bindToGuestIfNeed:callback:timeout:] [PKLoginModule.m : 382 + 0x14]
Found by: previous frame's frame pointer
8 Hago!-[HGLoginModule thirdpartyLogin:callback:timeout:] [PKLoginModule.m : 363 + 0x1c]
Found by: previous frame's frame pointer
9 Hago!-[HGLoginUIService thiredpartyLoginWithThirdpartType:successCallback:failureCallbakc:] [PKLoginUIService.m : 75 + 0x1c]
Found by: previous frame's frame pointer
10 Hago!-[HGPopupLoginOtherWaysAlertContentView loginWithType:] [MFPopupLoginOtherWaysAlertContentView.m : 120 + 0xc]
Found by: previous frame's frame pointer
11 Hago!__50-[HGPopupLoginOtherWaysAlertContentView viewInit:]_block_invoke.107 [MFPopupLoginOtherWaysAlertContentView.m : 92 + 0x4]
Found by: previous frame's frame pointer
12 Hago!-[UIControl(Block) callActionBlock:] [UIControl+Block.m : 26 + 0x8]
Found by: previous frame's frame pointer
13 UIKit!-[UIApplication sendAction:to:from:forEvent:] + 0x5c
Found by: previous frame's frame pointer
14 UIKit!-[UIControl sendAction:to:forEvent:] + 0x4c
Found by: previous frame's frame pointer
15 Hago!-[UIButton(touch) mySendAction:to:forEvent:] [UIButton+touch.m : 93 + 0x18]
Found by: previous frame's frame pointer
16 UIKit!-[UIControl _sendActionsForEvents:withEvent:] + 0x1b4
Found by: previous frame's frame pointer
17 UIKit!-[UIControl touchesEnded:withEvent:] + 0x23c
Found by: previous frame's frame pointer
18 Hago!-[HGButton touchesEnded:withEvent:] [PKButton.m : 27 + 0x24]
Found by: previous frame's frame pointer
19 UIKit!-[UIWindow _sendTouchesForEvent:] + 0x9ec
Found by: previous frame's frame pointer
20 UIKit!-[UIWindow sendEvent:] + 0xc84
Found by: previous frame's frame pointer
21 Hago!-[HiidoSDKCht sendEvent1:] [HiidoSDKCht.m : 121 + 0x10]
Found by: previous frame's frame pointer
22 UIKit!-[UIApplication sendEvent:] + 0x150
Found by: previous frame's frame pointer
23 UIKit!__dispatchPreprocessedEventFromEventQueue + 0x938
Found by: previous frame's frame pointer
24 UIKit!__handleEventQueueInternal + 0x1294
Found by: previous frame's frame pointer
25 UIKit!__handleHIDEventFetcherDrain + 0x94
Found by: previous frame's frame pointer
26 CoreFoundation!__CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 0x14
Found by: previous frame's frame pointer
27 CoreFoundation!__CFRunLoopDoSource0 + 0x54
Found by: previous frame's frame pointer
28 CoreFoundation!__CFRunLoopDoSources0 + 0xc8
Found by: previous frame's frame pointer
29 CoreFoundation!__CFRunLoopRun + 0x414
Found by: previous frame's frame pointer
30 CoreFoundation!CFRunLoopRunSpecific + 0x1b0
Found by: previous frame's frame pointer
31 GraphicsServices!GSEventRunModal + 0x60
Found by: previous frame's frame pointer
32 UIKit!UIApplicationMain + 0xcc
Found by: previous frame's frame pointer
33 Hago!main [main.mm : 18 + 0x10]
Found by: previous frame's frame pointer
34 libdyld.dylib!start + 0x0
Found by: previous frame's frame pointer
在过去30天这个崩溃只发生了一次, WTF, 概率这么小? 但还是要修复, 因为这个崩溃挺好重现的, 只是我们的业务场景规避了崩溃的情况, 从崩溃堆栈上看崩溃发生在[GIDSignIn assertValidUIDelegate]
, 这个是谷歌登录内部的方法, 由于我们使用的是谷歌的静态库(.framework), 这里关于GIDSignIn
类如何实现并没有暴露出来, 我们继续看崩溃的原因:
08-12 18:47:33.581 Handle Exception:
08-12 18:47:33.586 Exception Name: NSInvalidArgumentException
08-12 18:47:33.591 Exception Reason: uiDelegate must either be a |UIViewController| or implement the |signIn:presentViewController:| and |signIn:dismissViewController:| methods from |GIDSignInUIDelegate|.
之前我们的代码是在HGLoginViewController
设置的uiDelegate
, 但是从日志上看, 崩溃的时候HGLoginViewController
已经dealloc
了, 所以崩溃并不奇怪.
崩溃的原因是说uiDelegate
必须是UIViewController
或者实现GIDSignInUIDelegate
代理, 在代码中, 我们是这样进行初始化的.
HGGoogleLoginMgr.h
@interface HGGoogleLoginMgr : NSObject
@end
HGGoogleLoginMgr.m
- (instancetype)init
{
if (self = [super init]) {
NSString *clientID = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"G_CLIENT_ID"];
_signInMgr = [GIDSignIn sharedInstance];
_signInMgr.clientID = clientID;
_signInMgr.delegate = self;
_signInMgr.uiDelegate = self;
_signInMgr.shouldFetchBasicProfile = YES;
}
return self;
}
我们初始化的时候就把uiDelegate
写死了, 而且在整个运行期, HGGoogleLoginMgr
的实例并不会释放, 这样就可以了. 然后再实现代理:
- (void)signInWillDispatch:(GIDSignIn *)signIn error:(NSError *)error
{
}
// Present a view that prompts the user to sign in with Google
- (void)signIn:(GIDSignIn *)signIn presentViewController:(UIViewController *)viewController
{
UIViewController *topController = [UIApplication sharedApplication].keyWindow.rootViewController;
while (topController.presentedViewController) {
topController = topController.presentedViewController;
}
[topController presentViewController:viewController animated:YES completion:nil];
}
// Dismiss the "Sign in with Google" view
- (void)signIn:(GIDSignIn *)signIn dismissViewController:(UIViewController *)viewController
{
UIViewController *topController = [UIApplication sharedApplication].keyWindow.rootViewController;
while (topController.presentedViewController) {
topController = topController.presentedViewController;
}
[topController dismissViewControllerAnimated:YES completion:nil];
}
还有一种解法, 因为我们所有的控制器都是基于导航控制器的, 所以, 在每次调用signIn
前都可以去设置一下当前的uiDelegate
- (void)tp_login:(PKTpLoginCallback)callback timeout:(TimeOutHandler)timeout
{
_loginCallback = callback;
_signInMgr.uiDelegate = (id)[UIApplication sharedApplication].keyWindow.rootViewController;
[_signInMgr signOut];
[_signInMgr signIn];
}
这样在我们的导航控制器中实现代理就OK了
- (void)signInWillDispatch:(GIDSignIn *)signIn error:(NSError *)error
{
}
// Present a view that prompts the user to sign in with Google
- (void)signIn:(GIDSignIn *)signIn presentViewController:(UIViewController *)viewController
{
[self presentViewController:viewController animated:YES completion:nil];
}
// Dismiss the "Sign in with Google" view
- (void)signIn:(GIDSignIn *)signIn dismissViewController:(UIViewController *)viewController
{
[self dismissViewControllerAnimated:YES completion:nil];
}
这样我们只要app还在, 那根控制器必然存在, 这时候设置的uiDelegate
是不会为nil
的, 就不会有崩溃了.