UIWebView links opening in Safari

2019-04-28 05:06发布

I want to open an iTunes link in my webView, but when the webView launches the page, it redirects to the Safari browser. There, the url is getting opened, but I want it to open in my webView.

- (void)viewDidLoad {

    NSString *urlAddress = @"http://itunes.apple.com/us/app/foodcheck-traffic-light-nutrition/id386368933?mt=8";

    //Create a URL object.
    NSURL *url = [NSURL URLWithString:urlAddress];

    //URL Requst Object
    NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];

    //Load the request in the UIWebView.
    [webView loadRequest:requestObj];

    [super viewDidLoad];
}

Please suggest a way to sort out this problem.

3条回答
▲ chillily
2楼-- · 2019-04-28 05:28

Not sure if you ever got it working, but this works well for me:

NSString *responseString = [NSString stringWithContentsOfURL:myURL];
[self.webView loadHTMLString:responseString baseURL: nil)];
查看更多
太酷不给撩
3楼-- · 2019-04-28 05:31

You may want to try logging the load requests when you run the app. It may be that apple is automatically changing http:// to itms-apps or http://phobos or something along these lines. If so, then you can block the load when it's call using something like this:

- (BOOL)webView:(UIWebView *)webView 
shouldStartLoadWithRequest:(NSURLRequest *)request 
 navigationType:(UIWebViewNavigationType)navigationType;
{   
    NSURL *loadURL = [[request URL] retain];
    NSLog(@"%@",loadURL);
    if([[loadURL absoluteString] hasPrefix:@"http://"])
    {
        [loadURL release]; 
        return TRUE;
    }
    [loadURL release];
    return FALSE;
}

Good luck. I'm curious to know what finally works.

查看更多
▲ chillily
4楼-- · 2019-04-28 05:33

A note from the Apple reference documents- Q: How do I launch the App Store from my iPhone application? Also, how do I link to my application on the store?

Note: If you have iTunes links inside a UIWebView, you can use this technique after intercepting the links with the -[UIWebViewDelegate webView:shouldStartLoadWithRequest:navigationType:] delegate method.

查看更多
登录 后发表回答