iOS内购代码实现

开通付费协议后,再配置上内购商品和测试人员,我们就开始撸代码了。
github上Htain Lin Shwe大神写了一个IAPHelper这个框架,如果不放心也有时间的话可以自己看一下逻辑再实现一遍。
如何引用就直接在github上看吧,这就不多说了,直接上代码。

- (void)appleBuy {
    if (!self.viewModel.address && ![self.viewModel.data isIgnoreAddressBuy]) {
        [Common showToast:@"请选择收货地址!"];
        return;
    }
    if(![IAPShare sharedHelper].iap) {
        NSString *pid = [NSString stringWithFormat:@"%d",10000 + self.viewModel.data.dataId.intValue];
        NSSet* dataSet = [[NSSet alloc] initWithObjects:pid, nil];
        [IAPShare sharedHelper].iap = [[IAPHelper alloc] initWithProductIdentifiers:dataSet];
    }
    [IAPShare sharedHelper].iap.production = [k_user.phone isEqualToString:@"xxxx"] ? NO : YES;
    weak_self
    IAPHelper *iap = [IAPShare sharedHelper].iap;
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:[k_delegate window] animated:YES];
    hud.mode = MBProgressHUDModeIndeterminate;
    hud.animationType = MBProgressHUDAnimationZoomIn;
    [self setHud:hud];
    [iap requestProductsWithCompletion:^(SKProductsRequest* request,SKProductsResponse *response) {
        [weakSelf handelProductsWithCompletion:response];
    }];
}

- (void)handelProductsWithCompletion:(SKProductsResponse *)response {
    weak_self
    if([IAPShare sharedHelper].iap.products.count > 0) {
        SKProduct *product = [[IAPShare sharedHelper].iap.products objectAtIndex:0];
        DLog(@"Price: %@",[[IAPShare sharedHelper].iap getLocalePrice:product]);
        DLog(@"Title: %@",product.localizedTitle);
        [[IAPShare sharedHelper].iap buyProduct:product onCompletion:^(SKPaymentTransaction* trans) {
            if (trans.error) {
                DLog(@"trans.error %@",[trans.error localizedDescription]);
            } else if (trans.transactionState == SKPaymentTransactionStatePurchased) {
                DLog(@"SKPaymentTransactionStatePurchased");
                [[IAPShare sharedHelper].iap checkReceipt:[NSData dataWithContentsOfURL:[[NSBundle mainBundle] appStoreReceiptURL]] AndSharedSecret:@"App 专用共享密钥" onCompletion:^(NSString *response, NSError *error) {
                    //Convert JSON String to NSDictionary
                    NSDictionary* rec = [IAPShare toJSON:response];
                    if ([rec[@"status"] integerValue] == 0) {
                        [[IAPShare sharedHelper].iap provideContentWithTransaction:trans];
                        DLog(@"SUCCESS %@",response);
                        DLog(@"Pruchases %@",[IAPShare sharedHelper].iap.purchasedProducts);
                        [weakSelf paySuccess];// 走支付成功的逻辑
                    } else {
                        DLog(@"check Fail",response);
                    }
                }];
            } else if(trans.transactionState == SKPaymentTransactionStateFailed) {
                DLog(@"SKPaymentTransactionStateFailed");
            }
            dispatch_async(dispatch_get_main_queue(), ^{
                [weakSelf.hud hideAnimated:YES];
            });
        }];//end of buy product
    } else {
        dispatch_async(dispatch_get_main_queue(), ^{
            [weakSelf.hud hideAnimated:YES];
            [Common showToast:@"没有获取到商品"];
        });
    }
}

你可能感兴趣的:(iOS内购代码实现)