cordova create Demo com.cordova.demo.hello HelloWorld 复制代码
cd Demo 复制代码
cordova platform add ios 复制代码
cordova platforms ls 复制代码
cordova plugin add cordova-plugin-device 复制代码
cordova plugin list 复制代码
cordova plugin add cordova-plugin-wkwebview-engine 复制代码
platform :ios,'9.0' inhibit_all_warnings! target 'WKWebiveCordova' do pod 'cordova-plugin-device' pod 'cordova-plugin-wkwebview-engine end 复制代码
<content src="index.html" /> 复制代码
加载wkwebview时候,要记得载入config.xml文件,代码下面会显示 跳转url界面能显示出来配置如下东西:
<feature name="IntentAndNavigationFilter"> <param name="ios-package" value="CDVIntentAndNavigationFilter" /> <param name="onload" value="true" /> </feature> <feature name="CDVWKWebViewEngine"> <param name="ios-package" value="CDVWKWebViewEngine" /> </feature> <allow-navigation href="http://*/*" /> <allow-navigation href="https://*/*" /> <allow-navigation href="*" /> <preference name="CordovaWebViewEngine" value="CDVWKWebViewEngine" /> <plugin name="cordova-plugin-wkwebview-engine" spec="^1.2.1" /> 复制代码
#import <WebKit/WebKit.h> #import <UIKit/UIKit.h> #import <Cordova/CDVViewController.h> #import <Cordova/CDVCommandDelegateImpl.h> #import <Cordova/CDVCommandQueue.h> NS_ASSUME_NONNULL_BEGIN @interface WKWebview : CDVViewController + (WKWebview *)shareInstance; //设置这个值,加载当前页面 @property (nonatomic, copy) NSString *webUrl; //当前页面webview @property (nonatomic, strong) WKWebView *currentWebView; @end NS_ASSUME_NONNULL_END 复制代码
#import "WKWebview.h" @interface WKWebview () @end @implementation WKWebview - (instancetype)init { self = [super init]; if (self) { self.configFile = [self configPath]; } return self; } - (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onNotification:) name:CDVPluginResetNotification object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onNotificationed:) name:CDVPageDidLoadNotification object:nil]; [self setWebViewBackground]; } - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view self.currentWebView = (WKWebView *)self.webView; self.webView.backgroundColor = [UIColor redColor]; } - (void)viewWillDisappear:(BOOL)animated { [super viewWillDisappear:animated]; [[NSNotificationCenter defaultCenter] removeObserver:self name:CDVPluginResetNotification object:nil]; [[NSNotificationCenter defaultCenter] removeObserver:self name:CDVPageDidLoadNotification object:nil]; } - (NSString *)configPath { NSString *path = [[NSBundle mainBundle]pathForResource:@"config" ofType:@"xml"]; NSLog(@"path===== %@", path); return path; } - (void)dealloc { [[NSNotificationCenter defaultCenter] removeObserver:self]; } - (void)onNotification:(NSNotification *)notification { NSLog(@"webview开始加载"); WKWebView *webview = notification.object; if (![[webview.URL absoluteString] containsString:@"www/error.html"]) { } if ([webview.URL.absoluteString isEqualToString:@"ios:backtotheroot"]) { [self.navigationController popViewControllerAnimated:YES]; } } //wkwebView - (void)onNotificationed:(NSNotification *)notification { NSLog(@"webview加载完成"); WKWebView *webview = notification.object; //去除长按后出现的文本选取框 [webview evaluateJavaScript:@"document.documentElement.style.webkitUserSelect='none';" completionHandler:^(id obj, NSError *error){ }]; [webview evaluateJavaScript:@"document.title" completionHandler:^(id obj, NSError *error) { if ([obj isKindOfClass:[NSString class]]) { self.title = (NSString *)obj; } }]; } - (void)setWebUrl:(NSString *)webUrl { if (!webUrl) { return; } _webUrl = webUrl; if (self.currentWebView) { NSURL *url = [NSURL URLWithString:[webUrl stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]]]; NSURLRequest *request = [NSURLRequest requestWithURL:url]; [self.currentWebView loadRequest:request]; } } - (void)setWebViewBackground { NSLog(@"self.startPage==%@", self.startPage); if ([self.startPage containsString:@"hsjob"]) { self.view.backgroundColor = [UIColor redColor]; } else if ([self.startPage containsString:@"hstravel"]) { self.view.backgroundColor = [UIColor redColor]; } else if ([self.startPage containsString:@"movie"]) { self.view.backgroundColor = [UIColor blackColor]; } } @end 复制代码
WKWebview *vc = [[WKWebview alloc] init]; vc.startPage = @"http://www.baidu.com"; vc.hidesBottomBarWhenPushed = NO; [self.navigationController pushViewController:vc animated:YES]; 复制代码
#import "CDVPlugin.h" #import <Cordova/CDVPlugin.h> NS_ASSUME_NONNULL_BEGIN @interface MyPlugin : CDVPlugin // 获取当前位置信息 - (void)currentPosition:(CDVInvokedUrlCommand *)command; @end NS_ASSUME_NONNULL_END 复制代码
#import "MyPlugin.h" @implementation MyPlugin //获取当前位置信息 - (void)currentPosition:(CDVInvokedUrlCommand *)command { NSLog(@"command是回调的参数,在里面取"); CDVPluginResult *result2 = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsArray:@[ @"data1", @"data2" ]]; [self.commandDelegate sendPluginResult:result2 callbackId:command.callbackId]; } @end 复制代码
<feature name="MyPlugin"> <param name="ios-package" value="MyPlugin" /> </feature> 复制代码
<script type="text/javascript" src="./platform/common/loader.js"></script> // 能并行执行js <script type="text/javascript"> var loadList = ["./platform/common/hsplatform.js"]; var u = navigator.userAgent; var isAndroid = u.indexOf("Android") > -1 || u.indexOf("Adr") > -1; //android终端 var isiOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/); //app内部 这里需要判断加载安卓或者ios的区别,我就默认为ios了 if (u.indexOf("_hsApp_") != -1) { var cordovaURL = "./platform/ios/cordova.js"; if (isiOS) { cordovaURL = "./platform/ios/cordova.js"; } loadList.push(cordovaURL, "./js/index.js"); }else{ var cordovaURL = "./platform/ios/cordova.js"; if (isiOS) { cordovaURL = "./platform/ios/cordova.js"; } loadList.push(cordovaURL, "./js/index.js"); } Loader.load(loadList); </script> 复制代码
document.addEventListener("deviceready", onDeviceReady, false); function onDeviceReady() { console.log("准备"); } // 获取当前位置 Cordova.exec(successCurrentPosition, errorCurrentPosition, "MyPlugin", "currentPosition", [argument]); / ***successCurrentPosition 成功回调 errorCurrentPosition 失败回调 MyPlugin 我们新建的文件名字 currentPosition 调用方法 argument 入参***/ function currentPosition() { console.log("该功能只能在APP内部使用"); var argument = JSON.stringify({ 'relocation': 'false' }); Cordova.exec(successCurrentPosition, errorCurrentPosition, "MyPlugin", "currentPosition", [argument]); } //获取当前位置成功回调 function successCurrentPosition(position) { console.log(position); } //获取当前位置失败回调 function errorCurrentPosition(position) { console.log(position); } 复制代码
<script src="https://cdn.bootcss.com/vConsole/3.2.0/vconsole.min.js"></script> 复制代码
<feature name="Console"> <param name="ios-package" value="CDVLogger" /> <param name="onload" value="true" /> </feature> 复制代码
2022-07-07 05:05:57.442579+0800 WKWebiveCordova[74344:646107] 该功能只能在APP内部使用 2020-07-07 05:05:57.445205+0800 WKWebiveCordova[74344:646107] command是回调的参数,在里面取 2020-07-07 05:15:08.682174+0800 WKWebiveCordova[74813:651778] error==data1,data2 复制代码