IOS How to exit current app after using UIApplication openURL to launch a browser

iosiphoneopenurluiapplication

I am very new to iOS development. The app I need is very simple: just open a web browser and go to a specific URL when my app is launched. So in the delegate file, in didFinishLaunchingWithOptions, I add a line of code:

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://www.google.com"]];

The app can open safari and go to google, however when I press the home button and try to open the app again, I got a write screen. I know I must release the resource after I call the browser, but I don't know how to do it.

Thanks a lot.

Ray

Best Answer

As others have answered, your app likely won't be approved if this is all that your app is going to do, but to answer your question:

Your problem is that the app is multitasking on iOS 4, and -[UIApplicationDelegate didFinishLaunchingWithOptions:] doesn't get called again when the app comes back into focus from the background. (More on how the app delegate methods work with multitasking here.)

If this is all your app is going to do, then the easiest fix is to simply opt out of multitasking.

If you don't opt out, then you need to put that same 'openURL' logic in one of the app delegate methods that gets called when multitasking is used, for instance -[UIApplicationDelegate applicationDidBecomeActive:].