SKPayment验证已购买订购项目(目标c)

时间:2017-11-05 02:53:06

标签: ios objective-c skproduct skpaymenttransaction

我有这个代码,所有流程都可以,并进入此状态:SKPaymentTransactionStatePurchased

但我不知道订阅试用期结束后会发生什么。这个实现是正确的吗?

另一方面,当用户再次运行应用程序时,会发出警报以确认已购买产品。当用户在购买应用程序时再次打开应用程序时,如何避免此警报?

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    [self requestPurchasableRequest];
    [[SKPaymentQueue defaultQueue] addTransactionObserver:self];
}
-(void) requestPurchasableRequest{

    NSString *identifier = @"com.xxx.xxx";
    NSSet *productIdentifierSet = [[NSSet alloc]initWithObjects:identifier, nil];
    SKProductsRequest *productRequest = [[SKProductsRequest alloc] initWithProductIdentifiers:productIdentifierSet];
    productRequest.delegate = self;
    [productRequest start];
}

-(void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response{

    if (response.invalidProductIdentifiers.count>0) {
        UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"Warning"
                                                                       message:@"This product is not available at this moment please try again later."
                                                                preferredStyle:UIAlertControllerStyleAlert];

        UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault
                                                              handler:^(UIAlertAction * action) {}];

        [alert addAction:defaultAction];
        [self presentViewController:alert animated:YES completion:nil];
    }else {

         products = response.products;
         SKPayment *payment = [SKPayment paymentWithProduct:products[0]];
         [[SKPaymentQueue defaultQueue]addPayment:payment];
    }

-(void) paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray<SKPaymentTransaction *> *)transactions{

    for (SKPaymentTransaction *transaction in transactions)
    {
        switch (transaction.transactionState)
        {
            case SKPaymentTransactionStateDeferred:
                break;

            case SKPaymentTransactionStatePurchased: 
                [[SKPaymentQueue defaultQueue] finishTransaction:transaction];
                break;

            case SKPaymentTransactionStateFailed:
                [[SKPaymentQueue defaultQueue] finishTransaction:transaction];
                break;
            case SKPaymentTransactionStateRestored:
                [[SKPaymentQueue defaultQueue] finishTransaction:transaction];
            default:
                break;
        }
    }

}

0 个答案:

没有答案