• iOS 混合网页开发 问题


      在利用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要再主线程中执行,问题解决。

  • 相关阅读:
    Visual Studio 存在版本之间冲突
    Failed to load resoure:the serve responded with a status of 405 (Method Not Allowed)
    Excel 导入解析数据 NPOIExcelHelper
    WIN10 SERVICES -- 部署IIS
    高德地图API-搜索提示并定位到位置,卫星地图和标准地图的切换
    BIMFCE选择全量绘制
    JS中如何获取当前日期,并与输入日期作比较
    html中input标签放入小图标
    div跟随浏览器大小而改变
    vue-amap接入高德地图示例
  • 原文地址:https://www.cnblogs.com/pretty-guy/p/4891286.html
Copyright © 2020-2023  润新知