WKJavaScriptExceptionMessage=ReferenceError: Can't find variable: xxxx

oc调用js的方法的时候回出现这样的报错,直接看我的报错代码

- (void)appWillEnterBackGournd:(NSNotification *)info{
    if (self.wkweb != nil && self.callBackFile.enterGroudDic != nil){
        NSString * backStr = [NSString stringWithFormat:@"%@(%@)", self.callBackFile.enterGroudDic[@"args"][@"eventDispatcher"], @"XLH5SDKbackground"];
        [self.wkweb evaluateJavaScript:backStr completionHandler:^(id _Nullable object, NSError * _Nullable error) {
            NSLog(@" - %@ -- %@ --- ",error,object);
        }];
    }
}

对于oc而言,如果我们约定要给H5传一个字符串作为参数的话,注意这个字符串如果如上索引,直接以%@,在加oc的字符串是无法满足H5那边的数据结构的命名格式的。例如我直接将@"XLH5SDKbackground"作为字符串传给H5,那么到了H5那边,将会认为你传过来的只是一个object(对象)而不是一个字符串,因此,**要将这个字符串加上一对单引号(’’)**就不会有这个错了。

正确的代码

- (void)appWillForeGround:(NSNotification *)info{
    if (self.wkweb != nil && self.callBackFile.enterGroudDic != nil){
        AppDelegate * appDelegate = (AppDelegate*)[UIApplication sharedApplication].delegate;
        if (appDelegate.interval > 60 * 60 * 2) {   //在后台超过两个小时,回来刷新当前界面
            [self.wkweb reload];
        }
        NSString * backStr = [NSString stringWithFormat:@"%@('%@')", self.callBackFile.enterGroudDic[@"args"][@"eventDispatcher"], @"XLH5SDKforeground"];
        [self.wkweb evaluateJavaScript:backStr completionHandler:^(id _Nullable object, NSError * _Nullable error) {
            NSLog(@" - %@ -- %@ --- ",error,object);
        }];
    }
}

我是磊怀 2849765859 QQ 欢迎联系我

猜你喜欢

转载自blog.csdn.net/weixin_43883776/article/details/86531377