There exist a property @property (nonatomic, strong) JSContext *context; and I set jscontext in webviews's delegate methods.
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
[self.indicatorView stopAnimating];
self.webView.hidden = NO;
__weak typeof(self) weakSelf = self;
self.context = [webView valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"];
self.context[@"closeWebview"] = ^() {
weakSelf.dismissViewBlock();
};
}
when locationed succeed I will call the method below:
- (void)sendUserInfoDicToJSLocationSuccess:(BOOL)isSuccess
{
NSDictionary *userInfoDic = [self getResponseDicLocationSuccess:isSuccess];
NSString *responseStr = [userInfoDic jsonString];
if (responseStr.length <= 0) {
NSAssert(NO, nil);
return;
}
// [NSThread sleepForTimeInterval:1];
dispatch_async(dispatch_get_main_queue(), ^{
if (self.context && responseStr.length > 0) {
JSValue *callBackValue = self.context[@"mobileCallback"];
if (callBackValue) {
[callBackValue callWithArguments:@[responseStr]];
}
}
});
}
If I use the time delay, it works well. Or it will crash in webcore thread. The error message is below
WebCore`void WebCore::StyleResolver::applyMatchedProperties<(WebCore::StyleResolver::StyleApplicationPass)1>(WebCore::StyleResolver::MatchResult const&, bool, int, int, bool):
0x8f3ff40: pushl %ebp
0x8f3ff41: movl %esp, %ebp
0x8f3ff43: pushl %ebx
0x8f3ff44: pushl %edi
0x8f3ff45: pushl %esi
0x8f3ff46: subl $0x2c, %esp
0x8f3ff49: movl 0x14(%ebp), %edi
0x8f3ff4c: cmpl $-0x1, %edi
0x8f3ff4f: je 0x8f40072
;
void WebCore::StyleResolver::applyMatchedProperties<(WebCore::StyleResolver::StyleApplicationPass)1>(WebCore::StyleResolver::MatchResult const&, bool, int,
int, bool) + 306
0x8f3ff55: movl 0x18(%ebp), %esi
0x8f3ff58: movl 0xc(%ebp), %ebx
0x8f3ff5b: movl 0x8(%ebp), %edx
0x8f3ff5e: movl 0x130(%edx), %eax
0x8f3ff64: movzwl 0x2c(%eax), %eax
I have solved this problem.
dispatch_async(dispatch_get_main_queue(), ^{
if (self.context && responseStr.length > 0) {
JSValue *callBackValue = self.context[@"mobileCallback"];
[self.context[@"setTimeout"] callWithArguments:@[callBackValue,@0, responseStr]];
}
});