如何设置SKProductsRequest超时秒? SKProductsRequest的默认超时秒是多少?

时间:2016-06-04 14:09:22

标签: ios objective-c timeout in-app-purchase

是否可以为SKProductsRequest设置超时? 任何人都知道SKProductsRequest的默认超时?

1 个答案:

答案 0 :(得分:4)

没有open apple的api直接设置SKProductRequests超时, 但您可以实现一些简单的方法来手动设置它。在这种情况下,您需要对SKProductsRequest实例保持强引用:

@property (nonatomic, strong) SKProductsRequest *productsRequest;

然后创建您的请求并使用performSelector:withObject:afterDelay: 在所需的时间间隔内停止它的方法:

self.productsRequest = [[SKProductsRequest alloc] initWithProductIdentifiers:[StoreKitManager productsIDSet]];
self.productsRequest .delegate = self;
[self.productsRequest  start];
[self setupTimeout];

- (void)cancelTimeout {
    [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(stopRequest) object:nil];

}

- (void)setupTimeout {
    [self cancelTimeout];
    [self performSelector:@selector(stopRequest) withObject:self afterDelay:30.0f];
}

- (void)stopRequest {
    [self.productsRequest cancel];
}

不要忘记在代表中取消超时:

- (void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response {
    [self cancelTimeout];
}

- (void)requestDidFinish:(SKRequest *)request {
    [self cancelTimeout];
}

- (void)request:(SKRequest *)request didFailWithError:(NSError *)error {
    [self cancelTimeout];
}