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

Siv*_* Km 4 timeout objective-c in-app-purchase ios

是否可以为 SKProductsRequest 设置超时?有人知道 SKProductsRequest 的默认超时吗?

Ale*_*kov 6

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

@property (nonatomic, strong) SKProductsRequest *productsRequest;
Run Code Online (Sandbox Code Playgroud)

然后创建您的请求并使用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];
}
Run Code Online (Sandbox Code Playgroud)

并且不要忘记在您的代表中取消超时:

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

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

- (void)request:(SKRequest *)request didFailWithError:(NSError *)error {
    [self cancelTimeout];
}
Run Code Online (Sandbox Code Playgroud)