应用程序启动后立即使用openURL时出现延迟

时间:2013-11-12 09:15:24

标签: ios objective-c uiapplication openurl

我正在尝试在我自己的应用启动时使用 url处理程序启动另一个应用程序。

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

它基本上可以正常工作,但是看到我的应用程序看起来实际打开网址会有大约7秒的延迟。

怎么延迟?如何在启动自己的应用程序时立即启动URL /应用程序或减少此延迟?

4 个答案:

答案 0 :(得分:4)

您可以使用以下任何示例进行解决。

使用diapatch_async

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

使用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]];
}

使用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]];
}

答案 1 :(得分:1)

尝试使用目标的NSTimer ..

仅供参考:

[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;
 }

答案 2 :(得分:0)

- (void)applicationDidFinishLaunching:(UIApplication *)application中添加您的代码,因为在- (void)applicationDidBecomeActive:(UIApplication *)application被调用后将调用applicationDidFinishLaunching

答案 3 :(得分:0)

尝试在主线程上运行的块中调用-openURL方法。这将导致它在加载其他所有内容后执行:

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