zoukankan      html  css  js  c++  java
  • 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要再主线程中执行,问题解决。

  • 相关阅读:
    Window对象与DOM
    redis在linux环境下的安装与启动
    分布式,集群与负载平衡是什么?
    Hadoop之hive安装过程以及运行常见问题
    Hadoop之Pig安装
    eclipse安装Hadoop1.1.2版本开发插件
    linux ant 解决 错误: 找不到或无法加载主类 org.apache.tools.ant.launch.Launcher
    运行.class文件提示找不到或者无法加载主类原因
    bzoj4337: BJOI2015 树的同构
    bzoj1500: [NOI2005]维修数列
  • 原文地址:https://www.cnblogs.com/weiboyuan/p/5311730.html
Copyright © 2011-2022 走看看