美文网首页iOS DeveloperiOS程序猿iOS
ios webView调用原生方法可能出现崩溃的问题

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

作者: 亦暖易冷 | 来源:发表于2017-05-09 17:54 被阅读401次

    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调用

    相关文章

      网友评论

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

        本文链接:https://www.haomeiwen.com/subject/gkkitxtx.html