iOS JavaScriptCore与H5交互时出现异常提示

在利用JavaScriptCore与H5交互时出现异常提示:

This application is modifying the autolayout engine from a background thread, which can lead to engine corruption and weird crashes.  This will cause an exception in a future release.

从字面意思看,是因为在后台线程修改了UI,于是乎开始验证

_weak typeof(self)weakSelf = self;
    
    JSContext *tContext = [weakSelf.webView valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"];
    
    tContext[@"jsredirect"] = ^(){
        
        NSArray *tArgs = [JSContext currentArguments];
        JSValue *tJSValue = [tArgs firstObject];
        
        @try
        {
            NSDictionary *tActionValueDic = [tJSValue toDictionary];
            
            NSLog(@"JSValue:%@", tActionValueDic);
            
            NSString *tUrlStr = tActionValueDic[@"url"];
            
            if ([tUrlStr isEqualToString:@"chartDispose"]) {
                [[NSUserDefaults standardUserDefaults] setInteger:0 forKey:@"WebKitCacheModelPreferenceKey"];
                [[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"WebKitDiskImageCacheEnabled"];
                [[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"WebKitOfflineWebApplicationCacheEnabled"];
                [[NSUserDefaults standardUserDefaults] synchronize];
            }else{
                CGGradeJumpWebViewController *vc = [[CGGradeJumpWebViewController alloc]init];
                vc.urlStr = tUrlStr;
                NSLog(@"Current Thread:%@",[NSThread currentThread]);
                dispatch_async(dispatch_get_main_queue(), ^{
                    //UI修改
                    NSLog(@"Current Thread:%@",[NSThread currentThread]);
                    [weakSelf.navigationController pushViewController:vc animated:YES];
                });
            }
            
            
        }
        @catch (NSException *exception)
        {
            NSLog(@"exception:%@", exception);
        }
    };

果然,在XCode7中网页交互实在后台线程中执行的(XCode6木有问题),所以在交互时修改UI要再主线程中执行,问题解决。

转载于:https://www.cnblogs.com/weiboyuan/p/5311730.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值