开发者

Getting user specified information from URL in Objective C iOS

开发者 https://www.devze.com 2023-04-09 06:28 出处:网络
Currently I am working with a UIWebview which shows web content provided from a user. In this content there will be links to other web content and I need to decide whether to open links in safari or i

Currently I am working with a UIWebview which shows web content provided from a user. In this content there will be links to other web content and I need to decide whether to open links in safari or in the UIWebview.

My current solution is to add a queryString to the URL in the html e.g. <a href=http://www.w3schools.com?StayInApp target=_blank>Visit W3Schools</a>

When the user presses the link the following method in the UIViewController decides how to open the link.

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
  NSURL *url = [request URL];
  NSLog(@"%@",url);
  NSLog(@"%@",[url scheme]);
  NSLog(@"%@",[url user]);
  NSLog(@"%@",[url query]);

  if (navigationType == UIWebViewNavigationTypeLinkClicked) {


    if ([[url query]isEqualToString:@"OutOfAppApp"]) {
        NSLog(@"go out");
        [[UIApplication sharedApplication] openURL:url];

        return NO;
    }
    else if([[url query]isEqualToString:@"StayInApp"])
    {
        return YES;
    }
  }

  return YES;
}

I do not have very much experience within web development and can therefore not see the consequences of using this approach. Is this a valid approach or should I use another way?

Regards

EDIT: I have now come up with another approach using custom URL schemes, but I would still like comments on whether this is a valid solution :)

I have added a prefix to the links such as <a href=InAPP:http://www.w3schools.com target=_blank>Visit W3Schools</a> When a user presses a link the app examines the prefix and decides whether to launch the rest of the url in the app or in safari. This is done with the following methods:

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
        NSURL *url = [request URL];

    if (navigationType == UIWebViewNavigationTypeLinkClicked) {

        if ([[url scheme]isEqualToString:@"InAPP"]) {
            NSLog(@"go in");
            NSURLRequest *req=[NSURLRequest requestWithURL:[self removeExtensionFromURL:url]];
            [[self browserView] loadRequest:req];
            return NO;
        }
        else if([[url scheme]isEqualToString:@"OutAPP"])
        {
             NSLog开发者_JAVA百科(@"go out");
           [[UIApplication sharedApplication] openURL:[self removeExtensionFromURL:url]];

            return NO;
        }
    }
    return YES;
}

    -(NSURL*)removeExtensionFromURL:(NSURL*)url
{
    NSMutableString *urlString=[NSString stringWithFormat:@"%@",url];
    NSString *newURLString;

    if ([[url scheme]isEqualToString:@"InAPP"]) {
       newURLString = [urlString stringByReplacingOccurrencesOfString:@"InAPP:" withString:@""];
    }
    else if([[url scheme]isEqualToString:@"OutAPP"])
    {
    newURLString = [urlString stringByReplacingOccurrencesOfString:@"OutAPP:" withString:@""];
    }
    return [NSURL URLWithString:newURLString];
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号