0

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.

jscs
  • 63,694
  • 13
  • 151
  • 195
Ashutosh
  • 5,614
  • 13
  • 52
  • 84

2 Answers2

4

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.

nevan king
  • 112,709
  • 45
  • 203
  • 241
  • Unfortunately, that's not the case. – Ashutosh Feb 11 '13 at 20:47
  • What's your reason for wanting this? If you want to restrict access there might be better ways. – nevan king Feb 11 '13 at 20:58
  • So i have a container App and it will have link to couple of different apps. But once the user is in some other app and closes it then he might have to go to the container app again to open some other app.This is just for demo purpose so please do not worry about the concept and usability, – Ashutosh Feb 11 '13 at 21:01
  • There might be a way to do it, but allowing the user to choose (i.e. using a button to switch) seems better. I'd say that Apple disabled it in `applicationWillResignActive:` to prevent having an app that was impossible to quit (calls itself to open on quit). – nevan king Feb 11 '13 at 21:20
2

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.

Community
  • 1
  • 1
Emil
  • 7,220
  • 17
  • 76
  • 135
  • As i said above. I do not have to put this on Appstore this will be only for demo and internal use. So i am not worried about all that. – Ashutosh Feb 11 '13 at 20:17
  • You should put that in the original question, not everyone reads through the comments of a question :) – Emil Feb 11 '13 at 20:20