jsherk jsherk - 3 months ago 18
iOS Question

iPhone: After UIWebView opens external browser, then retruning to app opens it a second time

I have a

UIWebView
which loads an HTML page which has some links to the App Store (
<a herf="http://itunes/bla/bla/bla">
type of links).

Clicking a link, opens external Safari, then opens external App Store app and successfully goes to correct page.

If you then click Home button and return to springboard and click on app again, it will briefly open to the the
UIWebView
that was displayed and then jump to external Safari and external App Store app again.

If you go back to app one more time it stays in the app.

So what is happening is that returning to app after opening external browser link from
UIWebView
HTML page, will then jump back to the same link a second time.

Anybody have any ideas what might be causing this?

Am using Xcode 4.2.1 and it happens in simulator as well as on an actual device (iPad 1 with iOS 4.3).

EDIT - SOLUTION:

Ok, here is what I had to do to solve the problem (Thanks to Ben's response below who got me looking at the right areas):

This was my original method:

- (BOOL)webView:(UIWebView *)_webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
if (didLoad) { //Do not jump to URL when view first loads
return YES;
}

[[UIApplication sharedApplication] openURL:[request URL]];
return YES;
}


And this is my fixed method that does not keep jumping to URL:

- (BOOL)webView:(UIWebView *)_webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
if (didShowURL) { //Do not jump to URL when returning to app
didShowURL = 0;
return NO;
}

if (didLoad) { //Do not jump to URL when view first loads
return YES;
}

didShowURL = 1;
[[UIApplication sharedApplication] openURL:[request URL]];
return YES;
}

Answer

Your webview is being reloaded to its last page (the app store page) and is automatically redirecting the user away again. You just need to make sure that when the app is reloaded it is not trying to load the app store link again.

Depending on your app setup you could use

viewWillAppear

And reload the original HTML page with the links every time the view is brought to the front.

Comments