Should you do this? No.
Can you do this? (Probably) Not
I wrote a little test app to try opening a URL which I know to work (an Apple Maps URL which will open the maps app). I put in some logging code to see what was happening.
- (void)applicationWillResignActive:(UIApplication *)application
{
NSURL *url = [NSURL URLWithString:@"http://maps.apple.com/?daddr=San+Francisco,+CA&saddr=cupertino"];
if ([[UIApplication sharedApplication] canOpenURL:url]) {
NSLog(@"Can Open URL");
} else {
NSLog(@"Cannot Open URL");
}
BOOL success = [[UIApplication sharedApplication] openURL:url];
NSLog(@"Success is %@", [NSNumber numberWithBool:success]);
NSLog(@"Shared app: %@", [UIApplication sharedApplication]);
NSLog(@"applicationWillResignActive");
}
- (void)applicationDidEnterBackground:(UIApplication *)application
{
NSURL *url = [NSURL URLWithString:@"http://maps.apple.com/?daddr=San+Francisco,+CA&saddr=cupertino"];
[[UIApplication sharedApplication] openURL:url];
NSLog(@"Shared app: %@", [UIApplication sharedApplication]);
NSLog(@"Entered Background");
}
All NSLogs
logged something. The app returns YES
for canOpenURL:
, but returns NO
as the return value when you try -openURL:
.
Here's the log:
Flipper[18946:907] Can Open URL
Flipper[18946:907] Success is 0
Flipper[18946:907] Shared app: <UIApplication: 0x1f0413f0>
Flipper[18946:907] applicationWillResignActive
Flipper[18946:907] Shared app: <UIApplication: 0x1f0413f0>
Flipper[18946:907] Entered Background
If you want to allow your user to open another app, you should provide a button for them to do so (or an action sheet, or an alert). Re-purposing the Home button sounds like it might get your app rejected.
Note that if you put the same code into application:didFinishLaunchingWithOptions:
, it works find and immediately bumps you to the maps app. It's possible that the behaviour is being prevented in applicationWillResignActive:
. If you're really set on doing this (and you have an app with only one view) you could use viewWillDisappear
.