繁体   English   中英

iOS应用内购买:Objective-c

[英]iOS In App Purchases: Objective-c

我已经设置了应用内购买功能,并且在buySubscriptionAction方法后出现以下错误:

***由于未捕获的异常“ NSInvalidArgumentException”而终止应用程序,原因:“无效的产品标识符:(空)”

这是我的应用内购买代码:

在.h文件中

SKProductsRequest *productsRequest;
NSArray *validProducts;
@property (nonatomic, strong) SKProduct *product;
@property (nonatomic, strong) NSString *productID;

在.m文件中

-(void) startInAppPurchase {
    self.productID = @"1year";
    [self getProductID];
    [self buySubscriptionAction];
}

// called FIRST
-(void) getProductID {

    if ([SKPaymentQueue canMakePayments]) {
        SKProductsRequest *request = [[SKProductsRequest alloc] initWithProductIdentifiers:[NSSet setWithObject:self.productID]];
        request.delegate = self;
        [request start];
    } else {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Please enable in app purchases in your settings" delegate:self cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
        [alert show];
    }
}

// called SECOND
-(void) buySubscriptionAction {

    SKPayment *payment = [SKPayment paymentWithProduct:self.product];
    [[SKPaymentQueue defaultQueue] addPayment:payment];
}

buySubscriptionAction方法之后,应用程序崩溃。

我尝试使用self.productID作为com.companyName.appname.year1和仅year1 有谁知道是什么原因导致此崩溃和错误消息?

注意:

永远不会调用此方法:

- (void) productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response {
    NSArray *products = response.products;
    NSLog(@"products: %@",products);

    if (products.count != 0) {
        self.product = products[0];
        productName = self.product.localizedTitle;
        productDescription = self.product.localizedDescription;
    } else {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Product Not Found" delegate:self cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
        [alert show];
    }

    products = response.invalidProductIdentifiers;

    for (SKProduct *prod in products) {
        NSLog(@"Product not found: %@",prod);
    }
}

因此,self.products在buySubscriptionAction为null。

在.h中,我包含了<SKProductsRequestDelegate, SKPaymentTransactionObserver>

即使您分配了proxy = self,也不会调用您的委托方法。

想想为什么会发生这种情况。 SKProductsRequest承诺在请求完成或超时时调用单个委托方法。 既然没有发生,您可以假设1)系统错误2) SKProductsRequest以某种方式消失了。

1)绝不是框架错误。

2)因此, SKProductsRequest消失了。 想一想。 您的方法结束了,您希望SKProductsRequest实例继续运行。 但是,您现在已经离开方法范围,而没有告诉编译器您想保持SKProductsRequest活动状态。 系统假定您已完成SKProductsRequest实例并将其销毁。

您的问题是,离开方法范围后,您无法确保SKProductsRequest实例继续存在。

创建一个strong属性并将其用于存储请求。 这将明确告诉编译器在离开类上下文之前不要处置SKProductsRequest对象。 问题应该解决。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM