Repositorio del curso CCOM4030 el semestre B91 del proyecto kilometro0

CDVUIWebViewNavigationDelegate.m 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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 "CDVUIWebViewNavigationDelegate.h"
  18. #import <Cordova/CDVViewController.h>
  19. #import <Cordova/CDVCommandDelegateImpl.h>
  20. #import <Cordova/CDVUserAgentUtil.h>
  21. #import <objc/message.h>
  22. @implementation CDVUIWebViewNavigationDelegate
  23. - (instancetype)initWithEnginePlugin:(CDVPlugin*)theEnginePlugin
  24. {
  25. self = [super init];
  26. if (self) {
  27. self.enginePlugin = theEnginePlugin;
  28. }
  29. return self;
  30. }
  31. /**
  32. When web application loads Add stuff to the DOM, mainly the user-defined settings from the Settings.plist file, and
  33. the device's data such as device ID, platform version, etc.
  34. */
  35. - (void)webViewDidStartLoad:(UIWebView*)theWebView
  36. {
  37. NSLog(@"Resetting plugins due to page load.");
  38. CDVViewController* vc = (CDVViewController*)self.enginePlugin.viewController;
  39. [vc.commandQueue resetRequestId];
  40. [[NSNotificationCenter defaultCenter] postNotification:[NSNotification notificationWithName:CDVPluginResetNotification object:self.enginePlugin.webView]];
  41. }
  42. /**
  43. Called when the webview finishes loading. This stops the activity view.
  44. */
  45. - (void)webViewDidFinishLoad:(UIWebView*)theWebView
  46. {
  47. NSLog(@"Finished load of: %@", theWebView.request.URL);
  48. CDVViewController* vc = (CDVViewController*)self.enginePlugin.viewController;
  49. // It's safe to release the lock even if this is just a sub-frame that's finished loading.
  50. [CDVUserAgentUtil releaseLock:vc.userAgentLockToken];
  51. /*
  52. * Hide the Top Activity THROBBER in the Battery Bar
  53. */
  54. [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
  55. [[NSNotificationCenter defaultCenter] postNotification:[NSNotification notificationWithName:CDVPageDidLoadNotification object:self.enginePlugin.webView]];
  56. }
  57. - (void)webView:(UIWebView*)theWebView didFailLoadWithError:(NSError*)error
  58. {
  59. CDVViewController* vc = (CDVViewController*)self.enginePlugin.viewController;
  60. [CDVUserAgentUtil releaseLock:vc.userAgentLockToken];
  61. NSString* message = [NSString stringWithFormat:@"Failed to load webpage with error: %@", [error localizedDescription]];
  62. NSLog(@"%@", message);
  63. NSURL* errorUrl = vc.errorURL;
  64. if (errorUrl) {
  65. errorUrl = [NSURL URLWithString:[NSString stringWithFormat:@"?error=%@", [message stringByAddingPercentEncodingWithAllowedCharacters:NSCharacterSet.URLPathAllowedCharacterSet]] relativeToURL:errorUrl];
  66. NSLog(@"%@", [errorUrl absoluteString]);
  67. if(error.code != NSURLErrorCancelled) {
  68. [theWebView loadRequest:[NSURLRequest requestWithURL:errorUrl]];
  69. }
  70. }
  71. }
  72. - (BOOL)defaultResourcePolicyForURL:(NSURL*)url
  73. {
  74. /*
  75. * If a URL is being loaded that's a file url, just load it internally
  76. */
  77. if ([url isFileURL]) {
  78. return YES;
  79. }
  80. return NO;
  81. }
  82. - (BOOL)webView:(UIWebView*)theWebView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType
  83. {
  84. NSURL* url = [request URL];
  85. CDVViewController* vc = (CDVViewController*)self.enginePlugin.viewController;
  86. /*
  87. * Execute any commands queued with cordova.exec() on the JS side.
  88. * The part of the URL after gap:// is irrelevant.
  89. */
  90. if ([[url scheme] isEqualToString:@"gap"]) {
  91. [vc.commandQueue fetchCommandsFromJs];
  92. // The delegate is called asynchronously in this case, so we don't have to use
  93. // flushCommandQueueWithDelayedJs (setTimeout(0)) as we do with hash changes.
  94. [vc.commandQueue executePending];
  95. return NO;
  96. }
  97. /*
  98. * Give plugins the chance to handle the url
  99. */
  100. BOOL anyPluginsResponded = NO;
  101. BOOL shouldAllowRequest = NO;
  102. for (NSString* pluginName in vc.pluginObjects) {
  103. CDVPlugin* plugin = [vc.pluginObjects objectForKey:pluginName];
  104. SEL selector = NSSelectorFromString(@"shouldOverrideLoadWithRequest:navigationType:");
  105. if ([plugin respondsToSelector:selector]) {
  106. anyPluginsResponded = YES;
  107. shouldAllowRequest = (((BOOL (*)(id, SEL, id, int))objc_msgSend)(plugin, selector, request, navigationType));
  108. if (!shouldAllowRequest) {
  109. break;
  110. }
  111. }
  112. }
  113. if (anyPluginsResponded) {
  114. return shouldAllowRequest;
  115. }
  116. /*
  117. * Handle all other types of urls (tel:, sms:), and requests to load a url in the main webview.
  118. */
  119. BOOL shouldAllowNavigation = [self defaultResourcePolicyForURL:url];
  120. if (shouldAllowNavigation) {
  121. return YES;
  122. } else {
  123. [[NSNotificationCenter defaultCenter] postNotification:[NSNotification notificationWithName:CDVPluginHandleOpenURLNotification object:url]];
  124. }
  125. return NO;
  126. }
  127. @end