开发者

detecting URL webview has left the your iPhone app

i open a url within a Webview and whilst loading i show a spinner in a alert view.

If the url tapped opens an internal app such as iTunes how can i detec开发者_如何学Ct the web view has left my app so that when the user returns the dialog has been dismissed.

i have used didFailLoadWithError and this didn't work?

Any ideas?

Thanks dan

FIXED- i forgot to set the delegate Doh!


UIWebViewDelegate method webView:shouldStartLoadWithRequest:navigationType: will ask your webview's delegate if it is allowed to open every single url before trying to open it. You could check for url type in that method and return NO if it's not http or https, then present an alert for the user and let them chose if they want to open it or stay within your app, or just log that app was left to open another one and return YES;

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
    NSString *urlString = [NSString stringWithFormat:@"%@", request.URL];
    NSArray *urlComponents = [urlString componentsSeparatedByString:@"://"];
    NSString *urlType = [urlComponents objectAtIndex:0];
    if ([urlType isEqualToString:@"http"] || [urlType isEqualToString:@"https"]) {
        // present an alert or do whatever you want with this url...
        return NO;
    }
    return YES;
}


First of all, using an alert view to display loading progress in a web view is probably a bad idea, because it blocks all user interaction until it finishes loading.

You already have code that allows the web view to handle certain URLs with the built-in apps, such as iTunes (it doesn't do that on its own), so I guess when you use [[UIApplication sharedApplication] openURL:...] to open the external URL, you could easily hide the spinner there as well.


You could use applicationWillResignActive to detect when the app goes into an inactive state. It goes in your app delegate:

- (void)applicationWillResignActive:(UIApplication *)application {
    //close your UIWebView here
}

If you can't access your UIWebView from the delegate, you could register for the UIApplicationDidEnterBackgroundNotification notification from your UIViewController. Make sure you un-register at some point.

//register
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(closeWebView) name:UIApplicationDidEnterBackgroundNotification object:nil];

//un-register
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationDidEnterBackgroundNotification object:nil];
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜