iOS内购问题备忘录

#

1、连接不到apple store:注销appleID之后重启
2、服务器验证:存储部分待验证

直接上代码吧~

- (void)buyProdution:(UIButton *)sender{
    NSMutableArray *coinInfoArray=[[NSMutableArray alloc] initWithObjects:@"6.00",@"12.00",@"18.00",@"30.00",@"50.00", nil];
    NSMutableArray *coinTypeArray=[[NSMutableArray alloc] initWithObjects:@"***.00003",@"***.00004",@"***.00005",@"***.00006",@"***.00007", nil];
    CoinSelectView *view = [[CoinSelectView alloc] initWithFrame:CGRectMake(0, 0, 300, 80+coinInfoArray.count*50) withInfoArray:coinInfoArray];
    view.parentVC = self;

    [self lew_presentPopupView:view animation:[LewPopupViewAnimationFade new] dismissed:^{
        if (![view.tap isEqualToString:@"-1"]) {
            self.productIdent=[coinTypeArray objectAtIndex:[view.tap intValue]];
            if ([SKPaymentQueue canMakePayments]) {
                [self getProductInfo:self.productIdent];
            } else {
                [self showMessage:@"用户禁止应用内付费购买"];
            }
        }
    }];
}

//从Apple查询用户点击购买的产品的信息
- (void)getProductInfo:(NSString *)productIdentifier {
    NSArray *product = [[NSArray alloc] initWithObjects:productIdentifier, nil];
    NSSet *set = [NSSet setWithArray:product];
    SKProductsRequest * request = [[SKProductsRequest alloc] initWithProductIdentifiers:set];
    request.delegate = self;
    [request start];
    HUD=[MBProgressHUD showHUDAddedTo:self.view animated:YES];
    HUD.label.text=@"正在购买,请稍后";
}

// 查询成功后的回调
- (void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response {
    [HUD hideAnimated:YES];
    NSArray *myProduct = response.products;
    if (myProduct.count == 0) {
        [self showMessage:@"无法获取产品信息,请重试"];
        return;
    }
    SKPayment * payment = [SKPayment paymentWithProduct:myProduct[0]];
    [[SKPaymentQueue defaultQueue] addPayment:payment];
}

//查询失败后的回调
- (void)request:(SKRequest *)request didFailWithError:(NSError *)error {
    [HUD hideAnimated:YES];
    [self showMessage:[error localizedDescription]];
}

//购买操作后的回调
- (void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions {
    [HUD hideAnimated:YES];
    for (SKPaymentTransaction *transaction in transactions)
    {
        switch (transaction.transactionState)
        {
            case SKPaymentTransactionStatePurchased://交易完成
            {
                self.receipt = [GTMBase64 stringByEncodingData:[NSData dataWithContentsOfURL:[[NSBundle mainBundle] appStoreReceiptURL]]];
                [self checkReceiptIsValid];//把self.receipt发送到服务器验证是否有效
                [self completeTransaction:transaction];
                break;
            }

            case SKPaymentTransactionStateFailed://交易失败
                [self failedTransaction:transaction];
                break;

            case SKPaymentTransactionStateRestored://已经购买过该商品
                [self showMessage:@"恢复购买成功"];
                [self restoreTransaction:transaction];
                break;

            case SKPaymentTransactionStatePurchasing://商品添加进列表
                [self showMessage:@"正在请求付费信息,请稍后"];
                break;

            default:
                break;
        }
    }

}

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


- (void)failedTransaction:(SKPaymentTransaction *)transaction {
    if(transaction.error.code != SKErrorPaymentCancelled) {
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:nil message:@"购买失败,请重试"delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"重试", nil];
        [alertView show];
    } else {
        [self showMessage:@"用户取消交易"];
    }

    [[SKPaymentQueue defaultQueue] finishTransaction: transaction];
}


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

- (void)showMessage:(NSString *)string {
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"" message:string delegate:nil cancelButtonTitle:nil otherButtonTitles:nil, nil];
    [alert show];
    [self performSelector:@selector(dimissAlert:)withObject:alert afterDelay:1.5];
}

- (void) dimissAlert:(UIAlertView *)alert {
    if(alert){
        [alert dismissWithClickedButtonIndex:[alert cancelButtonIndex] animated:YES];
    }

}

- (void)checkReceiptIsValid {
    /*
    AFHTTPSessionManager manager]GET:@"后台服务器地址"  parameters::@"发送的参数(必须包括购买凭证)"
success:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject) {
    if(凭证有效){
        你要做的事
    }else{//凭证无效
        你要做的事
    }

} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:nil message:@"购买失败,请重试"delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"重试", nil];
    [alertView show];
} */
}

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 0)
    {
        [self saveReceipt];
    }
    else
    {
        [self checkReceiptIsValid];
    }
}


//持久化存储用户购买凭证(这里最好还要存储当前日期,用户id等信息,用于区分不同的凭证)
-(void)saveReceipt{
//    NSString *fileName = [APPUtils getUUIDString];
//    NSString *savedPath = [NSString stringWithFormat:@"%@%@.plist", AppStoreInfoLocalFilePath, fileName];
    NSString* date;
    NSDateFormatter* formatter = [[NSDateFormatter alloc]init];
    [formatter setDateFormat:@"YYYY-MM-dd hh:mm:ss"];
    date = [formatter stringFromDate:[NSDate date]];

    NSString *savedPath = [NSString stringWithFormat:@"%@.plist", AppStoreInfoLocalFilePath];

    NSDictionary *dic =[ NSDictionary dictionaryWithObjectsAndKeys:
                        self.receipt,                                                  @"Request_transactionReceipt",
                        date,                                                          @"DATE",
                        [[NSUserDefaults standardUserDefaults] objectForKey:@"userId"],@"USERID",
                        nil];

    [dic writeToFile:savedPath atomically:YES];
}

- (void)buyBtnSelected:(id)sender {
    CoinSelectView *view = [[CoinSelectView alloc] initWithFrame:CGRectMake(0, 0, 260, 400) withInfoArray:coinTmpArray];
    view.parentVC = self;

    [self lew_presentPopupView:view animation:[LewPopupViewAnimationFade new] dismissed:^{

     }];
}

-(void)dealloc
{
    [[SKPaymentQueue defaultQueue] removeTransactionObserver:self];
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值