JavaScript桥在ios中按如下方式检查操作。
但是,它在JavaScript的一部分中不起作用。
不能只使用部分原型格式吗?
ios代码。
-(void)webViewDidStartLoad:(UIWebView *)webView
{
JSContext *ctx = [webView valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"];
ctx[@"window"][@"mobilebridge"] = @{};
ctx[@"window"][@"mobilebridge"][@"printMessage"] = ^(JSValue *message){
NSLog(@"printMessage----------------------------------================");
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"" message:message.toString preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* ok = [UIAlertAction actionWithTitle:@"Alrty Message" style:UIAlertActionStyleCancel
handler:^(UIAlertAction *action){}];
[alert addAction:ok];
[self presentViewController:alert animated:YES completion:nil];
__block int value = 10;
return ^{
return ++value;
};
};
ctx[@"window"][@"mobilebridge"][@"testPrintMessage"] = ^(JSValue *pMessageId){
NSLog(@"testPrintMessage----------------------------------================");
return ^{
return @"1234";
};
};
}
JavaScript代码。
function loadTest(){
var test = "test";
//run
var aaa = window.mobilebridge.printMessage("test");
//not run
var tttt = CommonTest.testPrint(test);
}
CommonTest.prototype.testPrint = function (pDispatch)
{
var resValue;
resValue = (function(){window.mobilebridge.testPrintMessage(pDispatch); }());
return resValue;
}