Objective-C iPhone - Open URL in Safari immediately and exit app

15,567

Solution 1

Why don't you use Add to Home Screen at Mobile Safari?

enter image description here

However if you want to call Safari at the beginning of launching the app, try this:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    // Override point for customization after application launch.


    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://www.google.com"]];
        exit(0);
    });

    return YES;
}

Solution 2

Just one update regarding this question... After iOS 10, Apple recommends using following method instead:

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://www.google.com"] options:@{} completionHandler:nil];

You may also be able to inform options parameters and/or treat any return from completionHandler statement.

Share:
15,567
user3724252
Author by

user3724252

Updated on June 04, 2022

Comments

  • user3724252
    user3724252 about 2 years

    I am able to open a URL in Safari using:

    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"www.myurl.com"]];
    

    It works fine, but it takes more than 10 seconds in the Simulator. I've tried putting this code inside the app delegate's didFinishLauchingWithOptions method or my view controller's viewDidLoad method, but it still takes over 10 seconds to load. I get a black screen with nothing on it for that entire duration before it suddenly opens Safari. Is there any way I could decrease the load time? Is this only a Simulator bug?? I am still trying to get my devices registered to test on actual devices.

    The funny thing is that when I add it to a UIButton's action method, the app loads right away, but the user must tap the UIButton to launch the URL in Safari.

    My objective here is to have an app that upon startup, immediately launches a hard-coded URL in safari. The app should immediately exit so that it will be able to do this again when the user taps on the app icon. Any suggestions?

    Here's a snippet of what I attempted before in my ViewController.m that has the 10 second black screen if you want to try it yourself:

    - (void)viewDidLoad
    {
        [super viewDidLoad];
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"www.myurl.com"]];
        exit(0);
    }
    
    • danh
      danh about 10 years
      Is this just for fun, or do you plan on having the app approved for distribution by apple? If so, both (1) just launching the browser, and (2) calling exit() at any time, are show-stoppers for approval.
    • cahn
      cahn about 10 years
      Set “Application does not run in background” to YES in Info.plist.
    • user3724252
      user3724252 about 10 years
      This is an enterprise app store. This is just release 1 to pre-consolidate 2 'apps' into one. Release 2 will have a lot more to it and will enable users who already installed release 1 to update their app rather than download a second app.
    • user3724252
      user3724252 about 10 years
      Thanks, cahn. The exit(0) wasn't working very well. I did not know about the Key Property you mentioned. It is working great now!