4

I am trying to launch another application using its url handler right on my own app launch.

- (void)applicationDidBecomeActive:(UIApplication *)application
{        
     NSURL *actionURL = [NSURL URLWithString:@"fantastical2://"];
     [[UIApplication sharedApplication] openURL:actionURL];
}

It basically works, however there is a significant delay of about 7 seconds from seeing my app appear to actually opening the URL.

How come the delay? How can I launch open a URL/app immediately when launching my own app or reduce this delay?

Bernd
  • 11,133
  • 11
  • 65
  • 98

4 Answers4

4

You can resolve using any of the examples below.

Using diapatch_async

dispatch_async(dispatch_get_main_queue(), ^{
    [[UIApplication sharedApplication] openURL:urlString];
});

Using perfomSelector

- (void)applicationDidBecomeActive:(UIApplication *)application
{
  ...

  //hangs for 10 seconds
  //[[UIApplication sharedApplication] openURL:[NSURL URLWithString: url]];

  //Fix: use threads!
  [self performSelector:@selector(redirectToURL:)
      withObject:url afterDelay:0.0];

  ...
}

- (void)redirectToURL:(NSString *)url
{
  [[UIApplication sharedApplication] openURL:[NSURL URLWithString: url]];
}

Using NSThread

- (void)applicationDidBecomeActive:(UIApplication *)application
{
  ...

  //hangs for 10 seconds
  //[[UIApplication sharedApplication] openURL:[NSURL URLWithString: url]];

  //Fix: use threads!
  [NSThread detachNewThreadSelector:@selector(openBrowserInBackground:)
      toTarget:self withObject:url];

  ...
}

- (void)openBrowserInBackground:(NSString *)url
{
  [[UIApplication sharedApplication] openURL:[NSURL URLWithString: url]];
}
hooni
  • 249
  • 3
  • 9
1

Try with the NSTimer with target ..

For just Reference :

[NSTimer scheduledTimerWithTimeInterval:7.0
    target:self
    selector:@selector(targetMethod:)
    userInfo:nil
    repeats:NO];


 -(void) targetMethod{
    // Call Here ...
     NSURL *actionURL = [NSURL URLWithString:@"fantastical2://"];
     [[UIApplication sharedApplication] openURL:actionURL];

    //Invalidate the time
    [myTimer invalidate];
    myTimer = nil;
 }
Kumar KL
  • 15,315
  • 9
  • 38
  • 60
  • 1
    I also noticed that the time interval shouldn't be 0.0 even when I want to call the method immediately. – Bernd Nov 12 '13 at 10:24
0

Add your code in - (void)applicationDidFinishLaunching:(UIApplication *)application because - (void)applicationDidBecomeActive:(UIApplication *)application will be called after applicationDidFinishLaunching is called.

kaar3k
  • 994
  • 7
  • 15
0

Try calling the -openURL method within a block that runs on the main thread. This will cause it to execute once everything else is loaded:

-(void)applicationDidBecomeActive:(UIApplication *)application {
    dispatch_async(dispatch_get_main_queue(), ^{
        NSURL *actionURL = [NSURL URLWithString:@"fantastical2://"];
        [[UIApplication sharedApplication] openURL:actionURL];
    });
}
chris
  • 16,324
  • 9
  • 37
  • 40