Repositorio del curso CCOM4030 el semestre B91 del proyecto Artesanías con el Instituto de Cultura

CDVWebViewEngine.m 24KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607
  1. /*
  2. Licensed to the Apache Software Foundation (ASF) under one
  3. or more contributor license agreements. See the NOTICE file
  4. distributed with this work for additional information
  5. regarding copyright ownership. The ASF licenses this file
  6. to you under the Apache License, Version 2.0 (the
  7. "License"); you may not use this file except in compliance
  8. with the License. You may obtain a copy of the License at
  9. http://www.apache.org/licenses/LICENSE-2.0
  10. Unless required by applicable law or agreed to in writing,
  11. software distributed under the License is distributed on an
  12. "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  13. KIND, either express or implied. See the License for the
  14. specific language governing permissions and limitations
  15. under the License.
  16. */
  17. #import "CDVWebViewEngine.h"
  18. #import "CDVWebViewUIDelegate.h"
  19. #import "CDVWebViewProcessPoolFactory.h"
  20. #import <Cordova/NSDictionary+CordovaPreferences.h>
  21. #import "CDVURLSchemeHandler.h"
  22. #import <objc/message.h>
  23. #define CDV_BRIDGE_NAME @"cordova"
  24. #define CDV_WKWEBVIEW_FILE_URL_LOAD_SELECTOR @"loadFileURL:allowingReadAccessToURL:"
  25. @interface CDVWebViewWeakScriptMessageHandler : NSObject <WKScriptMessageHandler>
  26. @property (nonatomic, weak, readonly) id<WKScriptMessageHandler>scriptMessageHandler;
  27. - (instancetype)initWithScriptMessageHandler:(id<WKScriptMessageHandler>)scriptMessageHandler;
  28. @end
  29. @interface CDVWebViewEngine ()
  30. @property (nonatomic, strong, readwrite) UIView* engineWebView;
  31. @property (nonatomic, strong, readwrite) id <WKUIDelegate> uiDelegate;
  32. @property (nonatomic, weak) id <WKScriptMessageHandler> weakScriptMessageHandler;
  33. @property (nonatomic, strong) CDVURLSchemeHandler * schemeHandler;
  34. @property (nonatomic, readwrite) NSString *CDV_ASSETS_URL;
  35. @property (nonatomic, readwrite) Boolean cdvIsFileScheme;
  36. @end
  37. // see forwardingTargetForSelector: selector comment for the reason for this pragma
  38. #pragma clang diagnostic ignored "-Wprotocol"
  39. @implementation CDVWebViewEngine
  40. @synthesize engineWebView = _engineWebView;
  41. - (instancetype)initWithFrame:(CGRect)frame
  42. {
  43. self = [super init];
  44. if (self) {
  45. if (NSClassFromString(@"WKWebView") == nil) {
  46. return nil;
  47. }
  48. self.engineWebView = [[WKWebView alloc] initWithFrame:frame];
  49. }
  50. return self;
  51. }
  52. - (WKWebViewConfiguration*) createConfigurationFromSettings:(NSDictionary*)settings
  53. {
  54. WKWebViewConfiguration* configuration = [[WKWebViewConfiguration alloc] init];
  55. configuration.processPool = [[CDVWebViewProcessPoolFactory sharedFactory] sharedProcessPool];
  56. if (settings == nil) {
  57. return configuration;
  58. }
  59. configuration.allowsInlineMediaPlayback = [settings cordovaBoolSettingForKey:@"AllowInlineMediaPlayback" defaultValue:NO];
  60. // Set the media types that are required for user action for playback
  61. WKAudiovisualMediaTypes mediaType = WKAudiovisualMediaTypeAll; // default
  62. // targetMediaType will always exist, either from user's "config.xml" or default ("defaults.xml").
  63. id targetMediaType = [settings cordovaSettingForKey:@"MediaTypesRequiringUserActionForPlayback"];
  64. if ([targetMediaType isEqualToString:@"none"]) {
  65. mediaType = WKAudiovisualMediaTypeNone;
  66. } else if ([targetMediaType isEqualToString:@"audio"]) {
  67. mediaType = WKAudiovisualMediaTypeAudio;
  68. } else if ([targetMediaType isEqualToString:@"video"]) {
  69. mediaType = WKAudiovisualMediaTypeVideo;
  70. } else if ([targetMediaType isEqualToString:@"all"]) {
  71. mediaType = WKAudiovisualMediaTypeAll;
  72. } else {
  73. NSLog(@"Invalid \"MediaTypesRequiringUserActionForPlayback\" was detected. Fallback to default value of \"all\" types.");
  74. }
  75. configuration.mediaTypesRequiringUserActionForPlayback = mediaType;
  76. configuration.suppressesIncrementalRendering = [settings cordovaBoolSettingForKey:@"SuppressesIncrementalRendering" defaultValue:NO];
  77. /*
  78. * If the old preference key "MediaPlaybackAllowsAirPlay" exists, use it or default to "YES".
  79. * Check if the new preference key "AllowsAirPlayForMediaPlayback" exists and overwrite the "MediaPlaybackAllowsAirPlay" value.
  80. */
  81. BOOL allowsAirPlayForMediaPlayback = [settings cordovaBoolSettingForKey:@"MediaPlaybackAllowsAirPlay" defaultValue:YES];
  82. if([settings cordovaSettingForKey:@"AllowsAirPlayForMediaPlayback"] != nil) {
  83. allowsAirPlayForMediaPlayback = [settings cordovaBoolSettingForKey:@"AllowsAirPlayForMediaPlayback" defaultValue:YES];
  84. }
  85. configuration.allowsAirPlayForMediaPlayback = allowsAirPlayForMediaPlayback;
  86. /*
  87. * Sets Custom User Agents
  88. * - (Default) "userAgent" is set the the clean user agent.
  89. * E.g.
  90. * UserAgent = "Mozilla/5.0 (iPhone; CPU iPhone OS 13_3 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148"
  91. *
  92. * - If "OverrideUserAgent" is set, it will overwrite the entire "userAgent" value. The "AppendUserAgent" will be iggnored if set.
  93. * Notice: The override logic is handled in the "pluginInitialize" method.
  94. * E.g.
  95. * OverrideUserAgent = "foobar"
  96. * UserAgent = "foobar"
  97. *
  98. * - If "AppendUserAgent" is set and "OverrideUserAgent" is not set, the user defined "AppendUserAgent" will be appended to the "userAgent"
  99. * E.g.
  100. * AppendUserAgent = "foobar"
  101. * UserAgent = "Mozilla/5.0 (iPhone; CPU iPhone OS 13_3 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148 foobar"
  102. */
  103. NSString *userAgent = configuration.applicationNameForUserAgent;
  104. if (
  105. [settings cordovaSettingForKey:@"OverrideUserAgent"] == nil &&
  106. [settings cordovaSettingForKey:@"AppendUserAgent"] != nil
  107. ) {
  108. userAgent = [NSString stringWithFormat:@"%@ %@", userAgent, [settings cordovaSettingForKey:@"AppendUserAgent"]];
  109. }
  110. configuration.applicationNameForUserAgent = userAgent;
  111. if (@available(iOS 13.0, *)) {
  112. NSString *contentMode = [settings cordovaSettingForKey:@"PreferredContentMode"];
  113. if ([contentMode isEqual: @"mobile"]) {
  114. configuration.defaultWebpagePreferences.preferredContentMode = WKContentModeMobile;
  115. } else if ([contentMode isEqual: @"desktop"]) {
  116. configuration.defaultWebpagePreferences.preferredContentMode = WKContentModeDesktop;
  117. }
  118. }
  119. return configuration;
  120. }
  121. - (void)pluginInitialize
  122. {
  123. // viewController would be available now. we attempt to set all possible delegates to it, by default
  124. CDVViewController* vc = (CDVViewController*)self.viewController;
  125. NSDictionary* settings = self.commandDelegate.settings;
  126. NSString *scheme = [settings cordovaSettingForKey:@"scheme"];
  127. // If scheme is file or nil, then default to file scheme
  128. self.cdvIsFileScheme = [scheme isEqualToString: @"file"] || scheme == nil;
  129. NSString *hostname = @"";
  130. if(!self.cdvIsFileScheme) {
  131. if(scheme == nil || [WKWebView handlesURLScheme:scheme]){
  132. scheme = @"app";
  133. }
  134. vc.appScheme = scheme;
  135. hostname = [settings cordovaSettingForKey:@"hostname"];
  136. if(hostname == nil){
  137. hostname = @"localhost";
  138. }
  139. self.CDV_ASSETS_URL = [NSString stringWithFormat:@"%@://%@", scheme, hostname];
  140. }
  141. CDVWebViewUIDelegate* uiDelegate = [[CDVWebViewUIDelegate alloc] initWithTitle:[[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleDisplayName"]];
  142. uiDelegate.allowNewWindows = [settings cordovaBoolSettingForKey:@"AllowNewWindows" defaultValue:NO];
  143. self.uiDelegate = uiDelegate;
  144. CDVWebViewWeakScriptMessageHandler *weakScriptMessageHandler = [[CDVWebViewWeakScriptMessageHandler alloc] initWithScriptMessageHandler:self];
  145. WKUserContentController* userContentController = [[WKUserContentController alloc] init];
  146. [userContentController addScriptMessageHandler:weakScriptMessageHandler name:CDV_BRIDGE_NAME];
  147. if(self.CDV_ASSETS_URL) {
  148. NSString *scriptCode = [NSString stringWithFormat:@"window.CDV_ASSETS_URL = '%@';", self.CDV_ASSETS_URL];
  149. WKUserScript *wkScript = [[WKUserScript alloc] initWithSource:scriptCode injectionTime:WKUserScriptInjectionTimeAtDocumentStart forMainFrameOnly:YES];
  150. if (wkScript) {
  151. [userContentController addUserScript:wkScript];
  152. }
  153. }
  154. WKWebViewConfiguration* configuration = [self createConfigurationFromSettings:settings];
  155. configuration.userContentController = userContentController;
  156. // Do not configure the scheme handler if the scheme is default (file)
  157. if(!self.cdvIsFileScheme) {
  158. self.schemeHandler = [[CDVURLSchemeHandler alloc] initWithVC:vc];
  159. [configuration setURLSchemeHandler:self.schemeHandler forURLScheme:scheme];
  160. }
  161. // re-create WKWebView, since we need to update configuration
  162. WKWebView* wkWebView = [[WKWebView alloc] initWithFrame:self.engineWebView.frame configuration:configuration];
  163. wkWebView.UIDelegate = self.uiDelegate;
  164. /*
  165. * This is where the "OverrideUserAgent" is handled. This will replace the entire UserAgent
  166. * with the user defined custom UserAgent.
  167. */
  168. if ([settings cordovaSettingForKey:@"OverrideUserAgent"] != nil) {
  169. wkWebView.customUserAgent = [settings cordovaSettingForKey:@"OverrideUserAgent"];
  170. }
  171. self.engineWebView = wkWebView;
  172. if ([self.viewController conformsToProtocol:@protocol(WKUIDelegate)]) {
  173. wkWebView.UIDelegate = (id <WKUIDelegate>)self.viewController;
  174. }
  175. if ([self.viewController conformsToProtocol:@protocol(WKNavigationDelegate)]) {
  176. wkWebView.navigationDelegate = (id <WKNavigationDelegate>)self.viewController;
  177. } else {
  178. wkWebView.navigationDelegate = (id <WKNavigationDelegate>)self;
  179. }
  180. if ([self.viewController conformsToProtocol:@protocol(WKScriptMessageHandler)]) {
  181. [wkWebView.configuration.userContentController addScriptMessageHandler:(id < WKScriptMessageHandler >)self.viewController name:CDV_BRIDGE_NAME];
  182. }
  183. [self updateSettings:settings];
  184. // check if content thread has died on resume
  185. NSLog(@"%@", @"CDVWebViewEngine will reload WKWebView if required on resume");
  186. [[NSNotificationCenter defaultCenter]
  187. addObserver:self
  188. selector:@selector(onAppWillEnterForeground:)
  189. name:UIApplicationWillEnterForegroundNotification object:nil];
  190. NSLog(@"Using WKWebView");
  191. [self addURLObserver];
  192. }
  193. - (void)onReset {
  194. [self addURLObserver];
  195. }
  196. static void * KVOContext = &KVOContext;
  197. - (void)addURLObserver {
  198. if(!IsAtLeastiOSVersion(@"9.0")){
  199. [self.webView addObserver:self forKeyPath:@"URL" options:0 context:KVOContext];
  200. }
  201. }
  202. - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context
  203. {
  204. if (context == KVOContext) {
  205. if (object == [self webView] && [keyPath isEqualToString: @"URL"] && [object valueForKeyPath:keyPath] == nil){
  206. NSLog(@"URL is nil. Reloading WKWebView");
  207. [(WKWebView*)_engineWebView reload];
  208. }
  209. } else {
  210. [super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
  211. }
  212. }
  213. - (void) onAppWillEnterForeground:(NSNotification*)notification {
  214. if ([self shouldReloadWebView]) {
  215. NSLog(@"%@", @"CDVWebViewEngine reloading!");
  216. [(WKWebView*)_engineWebView reload];
  217. }
  218. }
  219. - (BOOL)shouldReloadWebView
  220. {
  221. WKWebView* wkWebView = (WKWebView*)_engineWebView;
  222. return [self shouldReloadWebView:wkWebView.URL title:wkWebView.title];
  223. }
  224. - (BOOL)shouldReloadWebView:(NSURL*)location title:(NSString*)title
  225. {
  226. BOOL title_is_nil = (title == nil);
  227. BOOL location_is_blank = [[location absoluteString] isEqualToString:@"about:blank"];
  228. BOOL reload = (title_is_nil || location_is_blank);
  229. #ifdef DEBUG
  230. NSLog(@"%@", @"CDVWebViewEngine shouldReloadWebView::");
  231. NSLog(@"CDVWebViewEngine shouldReloadWebView title: %@", title);
  232. NSLog(@"CDVWebViewEngine shouldReloadWebView location: %@", [location absoluteString]);
  233. NSLog(@"CDVWebViewEngine shouldReloadWebView reload: %u", reload);
  234. #endif
  235. return reload;
  236. }
  237. - (id)loadRequest:(NSURLRequest*)request
  238. {
  239. if ([self canLoadRequest:request]) { // can load, differentiate between file urls and other schemes
  240. if(request.URL.fileURL && self.cdvIsFileScheme) {
  241. NSURL* readAccessUrl = [request.URL URLByDeletingLastPathComponent];
  242. return [(WKWebView*)_engineWebView loadFileURL:request.URL allowingReadAccessToURL:readAccessUrl];
  243. } else if (request.URL.fileURL) {
  244. NSURL* startURL = [NSURL URLWithString:((CDVViewController *)self.viewController).startPage];
  245. NSString* startFilePath = [self.commandDelegate pathForResource:[startURL path]];
  246. NSURL *url = [[NSURL URLWithString:self.CDV_ASSETS_URL] URLByAppendingPathComponent:request.URL.path];
  247. if ([request.URL.path isEqualToString:startFilePath]) {
  248. url = [NSURL URLWithString:[NSString stringWithFormat:@"%@/%@", self.CDV_ASSETS_URL, startURL]];
  249. }
  250. if(request.URL.query) {
  251. url = [NSURL URLWithString:[@"?" stringByAppendingString:request.URL.query] relativeToURL:url];
  252. }
  253. if(request.URL.fragment) {
  254. url = [NSURL URLWithString:[@"#" stringByAppendingString:request.URL.fragment] relativeToURL:url];
  255. }
  256. request = [NSURLRequest requestWithURL:url];
  257. }
  258. return [(WKWebView*)_engineWebView loadRequest:request];
  259. } else { // can't load, print out error
  260. NSString* errorHtml = [NSString stringWithFormat:
  261. @"<!doctype html>"
  262. @"<title>Error</title>"
  263. @"<div style='font-size:2em'>"
  264. @" <p>The WebView engine '%@' is unable to load the request: %@</p>"
  265. @" <p>Most likely the cause of the error is that the loading of file urls is not supported in iOS %@.</p>"
  266. @"</div>",
  267. NSStringFromClass([self class]),
  268. [request.URL description],
  269. [[UIDevice currentDevice] systemVersion]
  270. ];
  271. return [self loadHTMLString:errorHtml baseURL:nil];
  272. }
  273. }
  274. - (id)loadHTMLString:(NSString*)string baseURL:(NSURL*)baseURL
  275. {
  276. return [(WKWebView*)_engineWebView loadHTMLString:string baseURL:baseURL];
  277. }
  278. - (NSURL*) URL
  279. {
  280. return [(WKWebView*)_engineWebView URL];
  281. }
  282. - (BOOL) canLoadRequest:(NSURLRequest*)request
  283. {
  284. // See: https://issues.apache.org/jira/browse/CB-9636
  285. SEL wk_sel = NSSelectorFromString(CDV_WKWEBVIEW_FILE_URL_LOAD_SELECTOR);
  286. // if it's a file URL, check whether WKWebView has the selector (which is in iOS 9 and up only)
  287. if (request.URL.fileURL) {
  288. return [_engineWebView respondsToSelector:wk_sel];
  289. } else {
  290. return YES;
  291. }
  292. }
  293. - (void)updateSettings:(NSDictionary*)settings
  294. {
  295. WKWebView* wkWebView = (WKWebView*)_engineWebView;
  296. wkWebView.configuration.preferences.minimumFontSize = [settings cordovaFloatSettingForKey:@"MinimumFontSize" defaultValue:0.0];
  297. /*
  298. wkWebView.configuration.preferences.javaScriptEnabled = [settings cordovaBoolSettingForKey:@"JavaScriptEnabled" default:YES];
  299. wkWebView.configuration.preferences.javaScriptCanOpenWindowsAutomatically = [settings cordovaBoolSettingForKey:@"JavaScriptCanOpenWindowsAutomatically" default:NO];
  300. */
  301. // By default, DisallowOverscroll is false (thus bounce is allowed)
  302. BOOL bounceAllowed = !([settings cordovaBoolSettingForKey:@"DisallowOverscroll" defaultValue:NO]);
  303. // prevent webView from bouncing
  304. if (!bounceAllowed) {
  305. if ([wkWebView respondsToSelector:@selector(scrollView)]) {
  306. ((UIScrollView*)[wkWebView scrollView]).bounces = NO;
  307. } else {
  308. for (id subview in wkWebView.subviews) {
  309. if ([[subview class] isSubclassOfClass:[UIScrollView class]]) {
  310. ((UIScrollView*)subview).bounces = NO;
  311. }
  312. }
  313. }
  314. }
  315. NSString* decelerationSetting = [settings cordovaSettingForKey:@"WKWebViewDecelerationSpeed"];
  316. if (![@"fast" isEqualToString:decelerationSetting]) {
  317. [wkWebView.scrollView setDecelerationRate:UIScrollViewDecelerationRateNormal];
  318. } else {
  319. [wkWebView.scrollView setDecelerationRate:UIScrollViewDecelerationRateFast];
  320. }
  321. wkWebView.allowsBackForwardNavigationGestures = [settings cordovaBoolSettingForKey:@"AllowBackForwardNavigationGestures" defaultValue:NO];
  322. wkWebView.allowsLinkPreview = [settings cordovaBoolSettingForKey:@"Allow3DTouchLinkPreview" defaultValue:YES];
  323. }
  324. - (void)updateWithInfo:(NSDictionary*)info
  325. {
  326. NSDictionary* scriptMessageHandlers = [info objectForKey:kCDVWebViewEngineScriptMessageHandlers];
  327. NSDictionary* settings = [info objectForKey:kCDVWebViewEngineWebViewPreferences];
  328. id navigationDelegate = [info objectForKey:kCDVWebViewEngineWKNavigationDelegate];
  329. id uiDelegate = [info objectForKey:kCDVWebViewEngineWKUIDelegate];
  330. WKWebView* wkWebView = (WKWebView*)_engineWebView;
  331. if (scriptMessageHandlers && [scriptMessageHandlers isKindOfClass:[NSDictionary class]]) {
  332. NSArray* allKeys = [scriptMessageHandlers allKeys];
  333. for (NSString* key in allKeys) {
  334. id object = [scriptMessageHandlers objectForKey:key];
  335. if ([object conformsToProtocol:@protocol(WKScriptMessageHandler)]) {
  336. [wkWebView.configuration.userContentController addScriptMessageHandler:object name:key];
  337. }
  338. }
  339. }
  340. if (navigationDelegate && [navigationDelegate conformsToProtocol:@protocol(WKNavigationDelegate)]) {
  341. wkWebView.navigationDelegate = navigationDelegate;
  342. }
  343. if (uiDelegate && [uiDelegate conformsToProtocol:@protocol(WKUIDelegate)]) {
  344. wkWebView.UIDelegate = uiDelegate;
  345. }
  346. if (settings && [settings isKindOfClass:[NSDictionary class]]) {
  347. [self updateSettings:settings];
  348. }
  349. }
  350. // This forwards the methods that are in the header that are not implemented here.
  351. // Both WKWebView implement the below:
  352. // loadHTMLString:baseURL:
  353. // loadRequest:
  354. - (id)forwardingTargetForSelector:(SEL)aSelector
  355. {
  356. return _engineWebView;
  357. }
  358. - (UIView*)webView
  359. {
  360. return self.engineWebView;
  361. }
  362. #pragma mark WKScriptMessageHandler implementation
  363. - (void)userContentController:(WKUserContentController*)userContentController didReceiveScriptMessage:(WKScriptMessage*)message
  364. {
  365. if (![message.name isEqualToString:CDV_BRIDGE_NAME]) {
  366. return;
  367. }
  368. CDVViewController* vc = (CDVViewController*)self.viewController;
  369. NSArray* jsonEntry = message.body; // NSString:callbackId, NSString:service, NSString:action, NSArray:args
  370. CDVInvokedUrlCommand* command = [CDVInvokedUrlCommand commandFromJson:jsonEntry];
  371. CDV_EXEC_LOG(@"Exec(%@): Calling %@.%@", command.callbackId, command.className, command.methodName);
  372. if (![vc.commandQueue execute:command]) {
  373. #ifdef DEBUG
  374. NSError* error = nil;
  375. NSString* commandJson = nil;
  376. NSData* jsonData = [NSJSONSerialization dataWithJSONObject:jsonEntry
  377. options:0
  378. error:&error];
  379. if (error == nil) {
  380. commandJson = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
  381. }
  382. static NSUInteger maxLogLength = 1024;
  383. NSString* commandString = ([commandJson length] > maxLogLength) ?
  384. [NSString stringWithFormat : @"%@[...]", [commandJson substringToIndex:maxLogLength]] :
  385. commandJson;
  386. NSLog(@"FAILED pluginJSON = %@", commandString);
  387. #endif
  388. }
  389. }
  390. #pragma mark WKNavigationDelegate implementation
  391. - (void)webView:(WKWebView*)webView didStartProvisionalNavigation:(WKNavigation*)navigation
  392. {
  393. [[NSNotificationCenter defaultCenter] postNotification:[NSNotification notificationWithName:CDVPluginResetNotification object:webView]];
  394. }
  395. - (void)webView:(WKWebView*)webView didFinishNavigation:(WKNavigation*)navigation
  396. {
  397. [[NSNotificationCenter defaultCenter] postNotification:[NSNotification notificationWithName:CDVPageDidLoadNotification object:webView]];
  398. }
  399. - (void)webView:(WKWebView*)theWebView didFailProvisionalNavigation:(WKNavigation*)navigation withError:(NSError*)error
  400. {
  401. [self webView:theWebView didFailNavigation:navigation withError:error];
  402. }
  403. - (void)webView:(WKWebView*)theWebView didFailNavigation:(WKNavigation*)navigation withError:(NSError*)error
  404. {
  405. CDVViewController* vc = (CDVViewController*)self.viewController;
  406. NSString* message = [NSString stringWithFormat:@"Failed to load webpage with error: %@", [error localizedDescription]];
  407. NSLog(@"%@", message);
  408. NSURL* errorUrl = vc.errorURL;
  409. if (errorUrl) {
  410. NSCharacterSet *charSet = [NSCharacterSet URLFragmentAllowedCharacterSet];
  411. errorUrl = [NSURL URLWithString:[NSString stringWithFormat:@"?error=%@", [message stringByAddingPercentEncodingWithAllowedCharacters:charSet]] relativeToURL:errorUrl];
  412. NSLog(@"%@", [errorUrl absoluteString]);
  413. [theWebView loadRequest:[NSURLRequest requestWithURL:errorUrl]];
  414. }
  415. }
  416. - (void)webViewWebContentProcessDidTerminate:(WKWebView *)webView
  417. {
  418. [webView reload];
  419. }
  420. - (BOOL)defaultResourcePolicyForURL:(NSURL*)url
  421. {
  422. // all file:// urls are allowed
  423. if ([url isFileURL]) {
  424. return YES;
  425. }
  426. return NO;
  427. }
  428. - (void) webView: (WKWebView *) webView decidePolicyForNavigationAction: (WKNavigationAction*) navigationAction decisionHandler: (void (^)(WKNavigationActionPolicy)) decisionHandler
  429. {
  430. NSURL* url = [navigationAction.request URL];
  431. CDVViewController* vc = (CDVViewController*)self.viewController;
  432. /*
  433. * Give plugins the chance to handle the url
  434. */
  435. BOOL anyPluginsResponded = NO;
  436. BOOL shouldAllowRequest = NO;
  437. for (NSString* pluginName in vc.pluginObjects) {
  438. CDVPlugin* plugin = [vc.pluginObjects objectForKey:pluginName];
  439. SEL selector = NSSelectorFromString(@"shouldOverrideLoadWithRequest:navigationType:");
  440. if ([plugin respondsToSelector:selector]) {
  441. anyPluginsResponded = YES;
  442. // https://issues.apache.org/jira/browse/CB-12497
  443. int navType = (int)navigationAction.navigationType;
  444. shouldAllowRequest = (((BOOL (*)(id, SEL, id, int))objc_msgSend)(plugin, selector, navigationAction.request, navType));
  445. if (!shouldAllowRequest) {
  446. break;
  447. }
  448. }
  449. }
  450. if (anyPluginsResponded) {
  451. return decisionHandler(shouldAllowRequest);
  452. }
  453. /*
  454. * Handle all other types of urls (tel:, sms:), and requests to load a url in the main webview.
  455. */
  456. BOOL shouldAllowNavigation = [self defaultResourcePolicyForURL:url];
  457. if (shouldAllowNavigation) {
  458. return decisionHandler(YES);
  459. } else {
  460. [[NSNotificationCenter defaultCenter] postNotification:[NSNotification notificationWithName:CDVPluginHandleOpenURLNotification object:url]];
  461. }
  462. return decisionHandler(NO);
  463. }
  464. #pragma mark - Plugin interface
  465. - (void)allowsBackForwardNavigationGestures:(CDVInvokedUrlCommand*)command;
  466. {
  467. id value = [command argumentAtIndex:0];
  468. if (!([value isKindOfClass:[NSNumber class]])) {
  469. value = [NSNumber numberWithBool:NO];
  470. }
  471. WKWebView* wkWebView = (WKWebView*)_engineWebView;
  472. wkWebView.allowsBackForwardNavigationGestures = [value boolValue];
  473. }
  474. @end
  475. #pragma mark - CDVWebViewWeakScriptMessageHandler
  476. @implementation CDVWebViewWeakScriptMessageHandler
  477. - (instancetype)initWithScriptMessageHandler:(id<WKScriptMessageHandler>)scriptMessageHandler
  478. {
  479. self = [super init];
  480. if (self) {
  481. _scriptMessageHandler = scriptMessageHandler;
  482. }
  483. return self;
  484. }
  485. - (void)userContentController:(WKUserContentController *)userContentController didReceiveScriptMessage:(WKScriptMessage *)message
  486. {
  487. [self.scriptMessageHandler userContentController:userContentController didReceiveScriptMessage:message];
  488. }
  489. @end