4

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

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

它基本上可以工作,但是从看到我的应用程序出现到实际打开 URL 有大约 7 秒的显着延迟。

延迟怎么来的?如何在启动我自己的应用程序时立即启动打开 URL/应用程序或减少这种延迟?

4

4 回答 4

4

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

使用 diapatch_async

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

使用性能选择器

- (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]];
}
于 2017-02-27T07:33:28.273 回答
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;
 }
于 2013-11-12T10:17:59.917 回答
0

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

于 2013-11-12T09:22:43.920 回答
0

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

-(void)applicationDidBecomeActive:(UIApplication *)application {
    dispatch_async(dispatch_get_main_queue(), ^{
        NSURL *actionURL = [NSURL URLWithString:@"fantastical2://"];
        [[UIApplication sharedApplication] openURL:actionURL];
    });
}
于 2017-01-10T11:50:06.413 回答