1 H5中已经注册了一个函数叫,sendImage(' '), APP通过JS调用像这个函数传递信息。下面代码是APP把本地的一个图片信息交给H5,然后展示出来。
self.userImage = [UIImage imageNamed:@"433"];//测试用图片数据
NSData *imageData = UIImagePNGRepresentation(self.userImage);
NSString *base64Encoded = [imageData base64EncodedStringWithOptions:0];
/// NSLog(@"base64Encoded: %@",base64Encoded);
NSString *sendImageMethod = [NSString stringWithFormat:@"sendImage('%@')",base64Encoded];
if (_webView) {
[_webView stringByEvaluatingJavaScriptFromString:sendImageMethod];
}
else if(_wkWebView){
[_wkWebView evaluateJavaScript:sendImageMethod completionHandler:^(id _Nullable response, NSError * _Nullable error) {
}];
}
2 H5当中本身就有一个window.postMessage函数,APP可以通过这个函数像H5传值 ,window.postMessage(' ',' ',' ') ,注意的是咋iOS当中每个参数都需要用, ' ' 来分割开。
NSURL *url = [NSURL URLWithString:self.urlStr];
NSString *hostUrl = [NSString stringWithFormat:@"%@://%@:%@", url.scheme, url.host, url.port];
NSString *jsonstring = [json AS_JSONString];
NSString *sendWifiMethod = [NSString stringWithFormat:@"window.postMessage('%@', '%@')",jsonstring,hostUrl];
if(_webView){
[_webView evaluateJavaScript:sendWifiMethod completionHandler:^(id _Nullable response, NSError * _Nullable error) {
}];
}