SHARE
TWEET

webviewios

a guest Aug 20th, 2019 129 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. @implementation ViewController
  2. NSString *fullURL = @"https://www.human-element.com";
  3. NSString *deviceType = @"?d=iphone";
  4. /*NEVER open any url, containing this string, INSIDE the app.
  5.  ONLY open outside the app. iOS cannot accept donations directly
  6.  within the app as it violates Apple's terms*/
  7. NSString *outsideAppFlag = @"mydonationssite-notarealsite.com";
  8. - (void)viewDidLoad {
  9.     //load webview url
  10.     NSString *urlWithDeviceType = [fullURL stringByAppendingString:deviceType];
  11.     NSURL *url = [NSURL URLWithString:urlWithDeviceType];
  12.     NSURLRequest *Request = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
  13.     [_mainWebView loadRequest:Request];
  14.     [_mainWebView setDelegate:self];
  15.     [super viewDidLoad];
  16.     // Do any additional setup after loading the view, typically from a nib.
  17. }
  18. - (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
  19. {
  20.     UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Connection"
  21.                                                     message: @"Having trouble reaching HE Demo? Please check your internet connection and restart the app."
  22.                                                    delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
  23.     _loadingLabel.hidden = TRUE;
  24.     //should an error message popup for this error?
  25.     BOOL ignoreError = FALSE;
  26.     if (error.domain == NSURLErrorDomain) {
  27.         if (error.code == NSURLErrorCancelled) {
  28.             //ignore this one, interrupted load
  29.             ignoreError = TRUE;
  30.         }
  31.     }
  32.     //for some reason, twitter triggers a url request to about:blank in order to load.
  33.     //This is necessary for twitter to load, but it triggers a webView error... just ignore about:blank errors...
  34.     //NSString *theURLString = [webView.request.URL absoluteString]; this won't work - it just returns the last successful url
  35.     NSString *urlStr = [error.userInfo objectForKey:@"NSErrorFailingURLStringKey"]; //this works
  36.     if([urlStr hasPrefix:@"about:blank"]){
  37.         //ignore about:blank error
  38.         ignoreError = TRUE;
  39.     }
  40.     //if this error shouldn't be ignored
  41.     if(!ignoreError){
  42.         //show the error message
  43.         [alert show];
  44.     }
  45. }
  46. - (void)webViewDidStartLoad:(UIWebView *)webView
  47. {
  48.     _loadingLabel.hidden = FALSE;
  49. }
  50. - (void)webViewDidFinishLoad:(UIWebView *)webView
  51. {
  52.     _loadingLabel.hidden = TRUE;
  53.     //add "newtab:" before each <a> tag href that fits criteria: 1) must have target="_blank", 2) must NOT have onclick attribute
  54.     NSString *JSInjection = @"javascript: var allLinks = document.getElementsByTagName('a'); if (allLinks) {var i;for (i=0; i<allLinks.length; i++) {var link = allLinks[i];var oncli = link.getAttribute('onclick');if(oncli==undefined||oncli.length<1){var target = link.getAttribute('target'); if (target && target == '_blank') {link.setAttribute('target','_self');link.href = 'newtab:'+link.href;}}}}";
  55.     [webView stringByEvaluatingJavaScriptFromString:JSInjection];
  56. }
  57. - (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request
  58.  navigationType:(UIWebViewNavigationType)navigationType
  59. {
  60.     BOOL isExternal = FALSE;
  61.     BOOL isAllow = TRUE;
  62.     NSString *urlStr = request.URL.absoluteString;
  63.     //if the url begins with 'newtab:'
  64.     if([urlStr hasPrefix:@"newtab:"]){
  65.         isExternal = TRUE;
  66.         //while newtab: is at the start of the URL
  67.         while([urlStr hasPrefix:@"newtab:"]){
  68.             //remove the 'newtab:' from the start of the url
  69.             urlStr = [urlStr substringFromIndex:7];
  70.         }
  71.     }else{
  72.         //url does NOT begin with 'newtab:'...
  73.         //if the url request doesn't start with anchor tag
  74.         if (![urlStr hasPrefix:@"#"]){
  75.             //if the url request doesn't start with the webview url
  76.             if(![urlStr hasPrefix:fullURL]){
  77.                 //if the link is not a phone number
  78.                 if(![urlStr hasPrefix:@"tel:"]){
  79.                     //if the link is not a mailto link
  80.                     if(![urlStr hasPrefix:@"mailto:"]){
  81.                         //then this link should be opened in a separate view
  82.                         isExternal = TRUE;
  83.                     }
  84.                 }
  85.             }
  86.         }
  87.     }
  88.     //if this is an external url
  89.     if(isExternal){
  90.         //if this link isn't blank
  91.         if(![urlStr hasPrefix:@"about:blank"]){
  92.             //load the external page
  93.             [self loadExternalView:urlStr];
  94.             isAllow = FALSE; //ignore the url request (let the other "sharedApplication" handle it, instead, outside of the webview)
  95.         }
  96.     }
  97.     return isAllow;
  98. }
  99. //private function to load the external view
  100. - (void)loadExternalView:(NSString *)externalUrl{
  101.     //if the URL does NOT contain the cachnet donations url string
  102.     if ([externalUrl rangeOfString:outsideAppFlag].location == NSNotFound) {
  103.         //if(![externalUrl containsString:outsideAppFlag]){
  104.         //get the alternate ExternalView that holds the external webview
  105.         UIStoryboard* mainStoryboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
  106.         ExternalView *ev = [mainStoryboard instantiateViewControllerWithIdentifier:@"ExternalView"];
  107.         //set the external url that needs to load
  108.         ev.loadUrl = externalUrl;
  109.         ev.outsideAppFlag = outsideAppFlag;
  110.         // Create the navigation controller and present it without unloading the current home view
  111.         UINavigationController *navCtl = [[UINavigationController alloc]initWithRootViewController:ev];
  112.         [self presentViewController:navCtl animated:YES completion: nil];
  113.     }else{
  114.         //if the URL DOES contain the cachnet donations url string...
  115.         //open the url in a SEPARATE browser outside of the app's webview
  116.         NSURL *url = [NSURL URLWithString:externalUrl];
  117.         [[UIApplication sharedApplication] openURL:url];
  118.     }
  119. }
  120. - (void)didReceiveMemoryWarning {
  121.     [super didReceiveMemoryWarning];
  122.     // Dispose of any resources that can be recreated.
  123. }
  124. @end
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top