通常WKWebView传值,使用evaluateJavaScript调用js方法即可.
NSString * jsStr = [NSString stringWithFormat:@"someMethod('%@')", data];
[self.webView evaluateJavaScript:jsStr completionHandler:^(id _Nullable result, NSError * _Nullable error) {
}];
项目前端使用Vue,需要在mounted(){}的方法内获取iOS端本地储存的一些数据发起请求.
iOS端可以在js加载前,把数据转成JSON字符串,加到js window下的对象,前端再去读取此对象.具体实现如下:
- (void)wkConfiguration
{
self.configuration = [[WKWebViewConfiguration alloc] init];
self.configuration.userContentController = [WKUserContentController new];
WKPreferences *preferences = [WKPreferences new];
preferences.javaScriptCanOpenWindowsAutomatically = YES;
self.configuration.preferences = preferences;
NSMutableDictionary *dic = [NSMutableDictionary new];
dic[@"username"] = [UserInfoTool username];