Bersaelor
Bersaelor

Reputation: 2577

"[[UIApplication sharedApplication] openURL" after Link is tapped in UIWebView - Multitasking

I show an UIWebView inside an Application which sends it delegate methods calls, after it receives taps on links. In the Delegate Method

- (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType

I catch those calls, process the link-urls and run

[[UIApplication sharedApplication] openURL:processedURL];

This works as intended, I end up at the right place.

Problem Multitasking: if I get back into my app, which is still running in the background after it closed, it will again call the "webView shouldStartLoadWithRequest" with the same link, so I end up sending the user to the page twice. Is there a preferred way to avoid this?

Solution:

You guys are totally right, I did a quite elongated if-else to analyze the given URL, but not in all branches the decision could end up in there existed a "return no"... duh, totally stupid error ;)

Upvotes: 1

Views: 1987

Answers (2)

Rob Napier
Rob Napier

Reputation: 299265

Clear the UIWebView so that when it reloads itself, it doesn't have a request pending. This is probably best done this way:

[webView loadRequest:nil];

But if that doesn't work, you can use:

[webView loadHTMLString:@"" baseURL:nil];

And of course you should be returning NO from this delegate method since you don't want the webview to try to load this content.

Upvotes: 1

DarkDust
DarkDust

Reputation: 92316

Not tested, but I'd suggest doing this:

- (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType
{
    [[UIApplication sharedApplication] performSelector:@selector(openURL:) 
                                            withObject:processedURL 
                                             afterDelay:0];
    return NO;
}

This way the callback can return as desired, and in the very next runloop iteration it will call openURL:.

Upvotes: 1

Related Questions