Ios 是否可以为SKStoreProductViewController loadProductWithParameters设置超时?

Ios 是否可以为SKStoreProductViewController loadProductWithParameters设置超时?,ios,asynchronous,ios6,app-store,Ios,Asynchronous,Ios6,App Store,我目前正在通过dispatch\u async调用storeViewController loadProductWithParameters。是否可以设置一个超时值,使其仅尝试获取X秒的结果,然后放弃 我通过使用下面的类方法实现了自己的超时,而不是直接调用loadProductWithParameters。由于dispatch\u after和\u块变量,它会超时 + (void)loadProductViewControllerWithTimeout:(NSTimeInterval)timeo

我目前正在通过dispatch\u async调用storeViewController loadProductWithParameters。是否可以设置一个超时值,使其仅尝试获取X秒的结果,然后放弃

我通过使用下面的类方法实现了自己的超时,而不是直接调用loadProductWithParameters。由于
dispatch\u after
\u块
变量,它会超时

+ (void)loadProductViewControllerWithTimeout:(NSTimeInterval)timeout
                      storeKitViewController:(SKStoreProductViewController *)storeKitViewController
                                  parameters:(NSDictionary *)parameters
                           completionHandler:(void (^)(BOOL result, NSError *error))completionHandler {

  __block BOOL hasReturnedOrTimedOut = NO;

  [storeKitViewController loadProductWithParameters:parameters completionBlock:^(BOOL result, NSError *error) {
    dispatch_async(dispatch_get_main_queue(), ^{
      if (!hasReturnedOrTimedOut) {
        hasReturnedOrTimedOut = YES;
        if (completionHandler) completionHandler(result, error);
      }
    });
  }];

  dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(timeout * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
    if (!hasReturnedOrTimedOut) {
      hasReturnedOrTimedOut = YES;
      if (completionHandler) completionHandler(NO, nil); // Or add your own error instead of |nil|.
    }
  });
}

我的最新应用程序更新被苹果拒绝,因为loadProductWithParameters从未调用其completionBlock,并阻止我的用户在iTunes上购买歌曲。。。希望这能有所帮助。

我已经完成了如下工作:

  __block BOOL timeoutOrFinish = NO;

  dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(30 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
    if(!timeoutOrFinish) {
      timeoutOrFinish = YES;
      [self dismissAndShowError];
    }
  });

  [storeViewController loadProductWithParameters:parameters completionBlock:^(BOOL result, NSError * _Nullable error) {
    if(timeoutOrFinish) {
      return;
    }

    timeoutOrFinish = YES;
    //[[NetworkManager sharedManager] showNetworkActivityIndicator:NO];
    if(error) {
      [self dismissAndShowError];
    }
  }];

  [self.view.window.rootViewController presentViewController:storeViewController animated:YES completion:nil];
其中
dismissAndShowError
方法运行
dismissViewControllerAnimated
并显示错误警报


基本上,您有一个单独的计时器(在我的例子中是30秒)来切换标志。在那个之后,若存储还并没有加载,我关闭它并显示一个错误。否则,将调用完成(在“取消”、“完成”和“错误”时),并根据状态处理所有操作。

找到解决方案了吗?@Leonardo-我没有,但如果您想测试它,Alexis Pribula有一个可能的解决方案。谢谢您的回复。我没有做太多的iOS开发,但如果我有机会测试它,我会的。