Question

In my App, I would like user to see another app when he closes the current App. Is it possible in any way? I tried writing code and open the app through custom url schema right from the method:

- (void)applicationWillResignActive:(UIApplication *)application
{
    NSString * string = @"InnovationLab://";
    NSURL * url = [NSURL URLWithString:string];
    [[UIApplication sharedApplication] openURL:url];
}

But it did not work. I put a breakpoint in here and once I close the app it stops here but nothing happens. This will not go to the AppStore. Only for the demo and internal usage.

Was it helpful?

Solution

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.

OTHER TIPS

You are free to open any other app using their URL scheme and the code you provided – just make sure it doesn't violate the HIG (Human Interface Guidelines). It won't work in your case though.

This question and answer provides more detailed information on this, but if you run the application on a jailbroken phone, you can use this little snippet instead:

[[UIApplication sharedApplication] launchApplicationWithIdentifier:@"com.apple.Preferences" suspended:NO];

or this snippet from this answer:

system("/usr/bin/open com.mycompany.MyAppName");

That said, bad idea. Apple will reject your app with 99% certainty. The applicationWillResignActive-method is for saving your work and preparing for close, and you sould not, and cannot, open another app in that method. (not applicable as the application won't be uploaded to the App Store.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top