Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @implementation ViewController
- NSString *fullURL = @"https://www.human-element.com";
- NSString *deviceType = @"?d=iphone";
- /*NEVER open any url, containing this string, INSIDE the app.
- ONLY open outside the app. iOS cannot accept donations directly
- within the app as it violates Apple's terms*/
- NSString *outsideAppFlag = @"mydonationssite-notarealsite.com";
- - (void)viewDidLoad {
- //load webview url
- NSString *urlWithDeviceType = [fullURL stringByAppendingString:deviceType];
- NSURL *url = [NSURL URLWithString:urlWithDeviceType];
- NSURLRequest *Request = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
- [_mainWebView loadRequest:Request];
- [_mainWebView setDelegate:self];
- [super viewDidLoad];
- // Do any additional setup after loading the view, typically from a nib.
- }
- - (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
- {
- UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Connection"
- message: @"Having trouble reaching HE Demo? Please check your internet connection and restart the app."
- delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
- _loadingLabel.hidden = TRUE;
- //should an error message popup for this error?
- BOOL ignoreError = FALSE;
- if (error.domain == NSURLErrorDomain) {
- if (error.code == NSURLErrorCancelled) {
- //ignore this one, interrupted load
- ignoreError = TRUE;
- }
- }
- //for some reason, twitter triggers a url request to about:blank in order to load.
- //This is necessary for twitter to load, but it triggers a webView error... just ignore about:blank errors...
- //NSString *theURLString = [webView.request.URL absoluteString]; this won't work - it just returns the last successful url
- NSString *urlStr = [error.userInfo objectForKey:@"NSErrorFailingURLStringKey"]; //this works
- if([urlStr hasPrefix:@"about:blank"]){
- //ignore about:blank error
- ignoreError = TRUE;
- }
- //if this error shouldn't be ignored
- if(!ignoreError){
- //show the error message
- [alert show];
- }
- }
- - (void)webViewDidStartLoad:(UIWebView *)webView
- {
- _loadingLabel.hidden = FALSE;
- }
- - (void)webViewDidFinishLoad:(UIWebView *)webView
- {
- _loadingLabel.hidden = TRUE;
- //add "newtab:" before each <a> tag href that fits criteria: 1) must have target="_blank", 2) must NOT have onclick attribute
- 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;}}}}";
- [webView stringByEvaluatingJavaScriptFromString:JSInjection];
- }
- - (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request
- navigationType:(UIWebViewNavigationType)navigationType
- {
- BOOL isExternal = FALSE;
- BOOL isAllow = TRUE;
- NSString *urlStr = request.URL.absoluteString;
- //if the url begins with 'newtab:'
- if([urlStr hasPrefix:@"newtab:"]){
- isExternal = TRUE;
- //while newtab: is at the start of the URL
- while([urlStr hasPrefix:@"newtab:"]){
- //remove the 'newtab:' from the start of the url
- urlStr = [urlStr substringFromIndex:7];
- }
- }else{
- //url does NOT begin with 'newtab:'...
- //if the url request doesn't start with anchor tag
- if (![urlStr hasPrefix:@"#"]){
- //if the url request doesn't start with the webview url
- if(![urlStr hasPrefix:fullURL]){
- //if the link is not a phone number
- if(![urlStr hasPrefix:@"tel:"]){
- //if the link is not a mailto link
- if(![urlStr hasPrefix:@"mailto:"]){
- //then this link should be opened in a separate view
- isExternal = TRUE;
- }
- }
- }
- }
- }
- //if this is an external url
- if(isExternal){
- //if this link isn't blank
- if(![urlStr hasPrefix:@"about:blank"]){
- //load the external page
- [self loadExternalView:urlStr];
- isAllow = FALSE; //ignore the url request (let the other "sharedApplication" handle it, instead, outside of the webview)
- }
- }
- return isAllow;
- }
- //private function to load the external view
- - (void)loadExternalView:(NSString *)externalUrl{
- //if the URL does NOT contain the cachnet donations url string
- if ([externalUrl rangeOfString:outsideAppFlag].location == NSNotFound) {
- //if(![externalUrl containsString:outsideAppFlag]){
- //get the alternate ExternalView that holds the external webview
- UIStoryboard* mainStoryboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
- ExternalView *ev = [mainStoryboard instantiateViewControllerWithIdentifier:@"ExternalView"];
- //set the external url that needs to load
- ev.loadUrl = externalUrl;
- ev.outsideAppFlag = outsideAppFlag;
- // Create the navigation controller and present it without unloading the current home view
- UINavigationController *navCtl = [[UINavigationController alloc]initWithRootViewController:ev];
- [self presentViewController:navCtl animated:YES completion: nil];
- }else{
- //if the URL DOES contain the cachnet donations url string...
- //open the url in a SEPARATE browser outside of the app's webview
- NSURL *url = [NSURL URLWithString:externalUrl];
- [[UIApplication sharedApplication] openURL:url];
- }
- }
- - (void)didReceiveMemoryWarning {
- [super didReceiveMemoryWarning];
- // Dispose of any resources that can be recreated.
- }
- @end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement