项目常见崩溃11(陆续更新)

做国际化的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 <GIDSignInUIDelegate>

@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<GIDSignInUIDelegate>)[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的, 就不会有崩溃了.

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 206,968评论 6 482
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 88,601评论 2 382
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 153,220评论 0 344
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 55,416评论 1 279
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 64,425评论 5 374
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 49,144评论 1 285
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 38,432评论 3 401
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 37,088评论 0 261
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 43,586评论 1 300
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 36,028评论 2 325
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 38,137评论 1 334
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,783评论 4 324
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 39,343评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,333评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,559评论 1 262
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 45,595评论 2 355
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,901评论 2 345

推荐阅读更多精彩内容

  • 1、通过CocoaPods安装项目名称项目信息 AFNetworking网络请求组件 FMDB本地数据库组件 SD...
    阳明先生_x阅读 15,968评论 3 119
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 12,029评论 4 62
  • 我们身边有许多炒股的亲人、朋友、同事。我们同他们聊天时会发现大多数人炒股并没有赚到钱。查其根本,我们发现,在...
    开心007阅读 1,410评论 0 1
  • 来自菲莱阿斯城的毕达哥拉斯派重要人物俄刻克拉特斯,遇到苏格拉底的学生、来自厄里斯的斐多,特别想了解苏格拉底死时的情...
    一粒沙的天空阅读 1,265评论 0 1
  • AOP - Aspect-OrientedProgramming - 面向切面编程 相关术语基本概念: 1.通知(...
    我的马儿有些瘦阅读 264评论 0 0