ios webView调用原生方法可能出现崩溃的问题

1、webView调用友盟中的QQ分享功能,崩溃错误

*** Assertion failure in +[QQApi sendMessage:], /data/rdm/projects/18708/src/QQApi/QQApi.m:359
2017-05-09 17:27:29.997116+0800 WireShoper[10938:3459257] 
2017-05-09 17:27:30.883339+0800 WireShoper[10938:3459603] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'TencentOpenApi must be called at main thread'

翻译过来就是
由于异常终止应用程序“NSInternalInconsistencyException”,原因是:“TencentOpenApi必须在主线程被调用的
解决方法:

if ([QQApiInterface isQQInstalled]) {//安装了QQ
        //创建分享消息对象
        UMSocialMessageObject *messageObject = [UMSocialMessageObject messageObject];
        //创建网页内容对象
        UMShareWebpageObject *shareObject = [UMShareWebpageObject shareObjectWithTitle:[appDefault objectForKey:@"shareTitle"] descr:[appDefault objectForKey:@"shareDescription"] thumImage:[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[appDefault objectForKey:@"thumbUrl"]]]]];
        //设置网页地址
        shareObject.webpageUrl = [appDefault objectForKey:@"webpageUrl"];
        //分享消息对象设置分享内容对象
        messageObject.shareObject = shareObject;
        dispatch_sync(dispatch_get_main_queue(), ^{
            //Update UI in UI thread here
            //调用分享接口
            [[UMSocialManager defaultManager] shareToPlatform:UMSocialPlatformType_QQ messageObject:messageObject currentViewController:self completion:^(id data, NSError *error) {
                if (error) {
                    NSLog(@"************Share fail with error %@*********",error);
                }else{
                    NSLog(@"response data is %@",data);
                }
            }];
        });
    }

分享到QQ空间也是一样的处理方法

注意:在主线程调用友盟QQ分享是会出现崩溃的,这个只限于webview调用友盟QQ分享
注意:在主线程调用友盟QQ分享是会出现崩溃的,这个只限于webview调用友盟QQ分享
注意:在主线程调用友盟QQ分享是会出现崩溃的,这个只限于webview调用友盟QQ分享

2、webView调用原生跳转功能(跳转的控制器主要做的是百度地图定位功能,摇一摇)可能会出现崩溃,
崩溃错误:


跳转百度地图定位.png

解决方法:

 dispatch_sync(dispatch_get_main_queue(), ^{
            RealTimeTrafficViewController * realTimeVC = [[RealTimeTrafficViewController alloc]init];
            [self presentViewController:realTimeVC animated:YES completion:nil];    
        });

注意:在主线程调用可能会出现崩溃的,这个只限于webview调用
注意:在主线程调用可能会出现崩溃的,这个只限于webview调用
注意:在主线程调用可能会出现崩溃的,这个只限于webview调用

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容