[英]StoreKit delegate method not getting called
我有個問題。 永遠不會調用SKProductsRequest委托方法。 這是先前被問到的,但它沒有答案。 StoreKit委托函數未被調用
我不是母語為英語的人,有時聽起來很滑稽:P :(
我想在我的iOS應用程序中實現StoreKit。 我創建了一個類來處理所有StoreKit通信。 這是代碼:
@implementation VRStoreController
- (void) requestProducts
{
SKProductsRequest *request= [[SKProductsRequest alloc]
initWithProductIdentifiers:
[NSSet setWithObject: @"myProductID"]];
request.delegate = self;
[request start];
[[UIApplication sharedApplication]setNetworkActivityIndicatorVisible:YES];
}
- (void) productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response
{
NSLog(@"F7U12");
[[UIApplication sharedApplication]setNetworkActivityIndicatorVisible:NO];
NSArray *myProducts = response.products;
NSLog(@"%@", myProducts);
}
我在我的app委托中創建了一個實例
@interface VRAppDelegate
@property (strong, nonatomic) VRStoreController *store;
@end
奇怪的是,當我在appDidFinishLaunching:方法中運行[store requestProducts]時,此代碼可以正常工作。
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
//app initialization...
self.store = [[VRStoreController alloc]init];
[store requestProducts]; //This works ok, delegate method gets called.
return YES;
}
但是當我在設置tableView:didSelectRowAtIndexPath中運行代碼時:委托方法永遠不會被調用。
//VRAppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
//app initialization...
self.store = [[VRStoreController alloc]init];//initialize store
return YES;
}
//VRSettingsViewController.m
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
switch (indexPath.section) {
//case 0, 1...
case 2:
{
VRStoreController *store = ((VRAppDelegate *)[UIApplication sharedApplication].delegate).store;
[store requestProducts]; //calls SKProductRequest start, but never calls delegate method.
NSLog(@"GO PRO");
}
default:
break;
}
}
我不知道我做得不好。 幾乎嘗試了一切,沒有錯誤,沒有崩潰,但委托從未被調用。 任何幫助將非常感激。
非常感謝提前!
SKProductsRequestDelegate
協議也符合SKRequestDelegate
協議,該協議具有以下方法:
- (void)request:(SKRequest *)request didFailWithError:(NSError *)error;
- (void)requestDidFinish:(SKRequest *)request;
實現它們並查看是否正在調用它們中的任何一個。 您可能會收到錯誤或其他意外的響應。
我終於找到了問題。 我沒有保留SKProductRequest,因此請求已啟動,然后被轉儲。 我通過這樣做來修復它
//VRStoreController.m
@interface VRStoreController ()
@property (strong, nonatomic) SKProductRequest *request; //Store the request as a property
@end
@implementation VRStoreController
@synthesize request;
- (void) requestProducts
{
self.request = [[SKProductsRequest alloc] //save the request in the property
initWithProductIdentifiers:
[NSSet setWithObject: @"myProductID"]];
self.request.delegate = self;
[self.request start];
[[UIApplication sharedApplication]setNetworkActivityIndicatorVisible:YES];
}
雖然問題已得到解決,但我認為不需要保留請求,因為我已經調用了start方法並設置了它的委托,它應該自己做其他所有事情,並且應該保留自己直到它收到響應(或錯誤),然后解除分配。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.