3

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);
}
rmaddy
  • 314,917
  • 42
  • 532
  • 579
user3724252
  • 31
  • 1
  • 1
  • 2
  • 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. – danh Jun 10 '14 at 01:45
  • Set “Application does not run in background” to YES in Info.plist. – cahn Jun 10 '14 at 05:11
  • 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 Jun 10 '14 at 14:02
  • 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! – user3724252 Jun 10 '14 at 15:42

3 Answers3

5

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;
}
Joey
  • 2,912
  • 2
  • 27
  • 32
3

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.

Luiz Durães
  • 744
  • 9
  • 17
-1

The url from (link -> Application - > safari) and (Safari banner -> application) both are different.

Check the URL contains to stop redirecting again, When the application opens using safari banner.

EX : First time it contains ==

if([url.absoluteString containsString:@"=="]) {

[[UIApplication sharedApplication] openURL:@"URL to load" options:[NSDictionary dictionary] completionHandler:nil];

}

When click the safari banner to open application URL not contains ==

Hope this helps!

Mithun R
  • 19
  • 2