ios内购

流程和参阅:
1,官方(详细、权威)
2,iOS应用内付费(IAP)开发步骤列表(唐巧大神的)

代码大致有3个步骤:

  1. 请求产品信息。
  2. 发送购买请求。
  3. 处理购买回调。
#import 

-(void)getProductInfo {
    
    NSSet * set = [NSSet setWithArray:@[ProductID]]; // 这个就是产品ID,在iTunes后台创建
    
    SKProductsRequest * request = [[SKProductsRequest alloc] initWithProductIdentifiers: set];
    
    request.delegate = self; // 遵守SKProductsRequestDelegate代理
    
    [request start];
    
    [SVProgressHUD showWithStatus:@"正在获取产品信息"];
}

- (void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response {
    
    
    NSArray *myProduct = response.products;
    
    if (myProduct.count == 0) {
        
        [SVProgressHUD showInfoWithStatus:@"无法获取产品信息,请重试"];
        return;
    }
    
    self.payment = [SKPayment paymentWithProduct:myProduct[0]];
    
    [[SKPaymentQueue defaultQueue] addPayment:self.payment];
    [[SKPaymentQueue defaultQueue] addTransactionObserver:self];
    
    [SVProgressHUD showWithStatus:@"正在请求支付"];
}

//查询失败后的回调
- (void)request:(SKRequest *)request didFailWithError:(NSError *)error {
    
    [SVProgressHUD showInfoWithStatus:[error localizedDescription]];
}

//购买操作后的回调
- (void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions {
    
    for (SKPaymentTransaction *transaction in transactions)
    {
        if ([transaction.payment.productIdentifier  isEqualToString: self.payment.productIdentifier]) {
            
            switch (transaction.transactionState)
            {
                case SKPaymentTransactionStatePurchased://交易完成
                {
                    [SVProgressHUD showSuccessWithStatus:@"购买成功"];
                    
                    [self completeTransaction:transaction];
                    
                    [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"PaySuccess"];
                    [[NSUserDefaults standardUserDefaults] setValue:[NSString stringWithFormat:@"%.0f", [[NSDate date] timeIntervalSince1970]] forKey:@"PaySuccessTime"];
                    
                    [self setTime];
                    
                    
                } break;
                    
                case SKPaymentTransactionStateFailed://交易失败
                    [self failedTransaction:transaction];
                    break;
                    
                case SKPaymentTransactionStateRestored://已经购买过该商品
                    
                    [SVProgressHUD showInfoWithStatus:@"恢复购买成功"];
                    [self restoreTransaction:transaction];
                    break;
                    
                case SKPaymentTransactionStatePurchasing://商品添加进列表
                    [SVProgressHUD showWithStatus:@"正在购买中"];
                    break;
                    
                case SKPaymentTransactionStateDeferred://最终状态未确定
                    [SVProgressHUD showWithStatus:@"正在请求付费信息,请稍后"];
                    [[SKPaymentQueue defaultQueue] finishTransaction: transaction];
                    break;
                    
                default:
                    
                    break;
            }
        }
        
    }
    
}



- (void)completeTransaction:(SKPaymentTransaction *)transaction {
    
    [[SKPaymentQueue defaultQueue] finishTransaction:transaction];
}


- (void)failedTransaction:(SKPaymentTransaction *)transaction {
    
    if(transaction.error.code != SKErrorPaymentCancelled) {
 
        [SVProgressHUD showInfoWithStatus:[transaction.error localizedDescription]];
        
    } else {
        
        [SVProgressHUD showInfoWithStatus:@"用户取消交易"];
    }
    
    [[SKPaymentQueue defaultQueue] finishTransaction: transaction];
}


- (void)restoreTransaction:(SKPaymentTransaction *)transaction {
    
    [[SKPaymentQueue defaultQueue] finishTransaction: transaction];
}

你可能感兴趣的:(ios内购)