【In-App Purchase】自分なりにアプリ内課金備忘録の2 リストア

自分用備忘録。
自分なりに整頓。

アプリ内課金のリストアについてはこちらが参考になった。
AT-Sphere: In-App Purchaseにハマる (リストア編)

冗長な感じだがまずはリストアボタンを押されてAlertで確認。

#pragma mark リストア
- (IBAction)restoreButton:(id)sender
{
    NSLog(@"リストアボタン押した");
    // コントローラを生成
    UIAlertController * ac =
    [UIAlertController alertControllerWithTitle:@"Restore"
                                        message:@"Do you want to start the restoration ?"
                                 preferredStyle:UIAlertControllerStyleAlert];
    
    // Cancel用のアクションを生成
    UIAlertAction * cancelAction =
    [UIAlertAction actionWithTitle:@"Cancel"
                             style:UIAlertActionStyleCancel
                           handler:^(UIAlertAction * action) {
                               // ボタンタップ時の処理
                               NSLog(@"Cancel button tapped.");
                           }];
    
    // OK用のアクションを生成
    UIAlertAction * okAction =
    [UIAlertAction actionWithTitle:@"OK"
                             style:UIAlertActionStyleDefault
                           handler:^(UIAlertAction * action) {
                               // ボタンタップ時の処理
                               [self startRestore];
                               NSLog(@"OK button tapped.");
                           }];
    
    // コントローラにアクションを追加
    [ac addAction:cancelAction];
    [ac addAction:okAction];
    
    // アラート表示処理
    [self presentViewController:ac animated:YES completion:nil];   
}

OKを押すと[self startRestore]でstartRestoreメソッドへ。
ココではプロダクトIDをセットして購入の確認に投げる。

- (void)startRestore
{
    NSLog(@"リストアスタート");
    
    [[SKPaymentQueue defaultQueue] addTransactionObserver:self];
    
    //プロダクトIDをセット
    NSSet *set = [NSSet setWithObjects:@"プロダクトID2",@"プロダクトID3",@"プロダクトID4", nil];
    SKProductsRequest *prductsRequest = [[SKProductsRequest alloc] initWithProductIdentifiers:set];
    prductsRequest.delegate = self;
    // 購入履歴チェック
    [[SKPaymentQueue defaultQueue] restoreCompletedTransactions];
}

次のメソッドで購入済みプロダクトIDを確認してそれぞれ処理。

- (void) paymentQueueRestoreCompletedTransactionsFinished:(SKPaymentQueue *)queue
{
    NSLog(@"ペイメントキューりすとあ");
    BOOL restore = NO;
    
    for (SKPaymentTransaction *transaction in queue.transactions)
    {
        // プロダクトIDが一致した場合
        if ([transaction.payment.productIdentifier isEqualToString:@"プロダクトID2"]) {
            restore = YES;
            NSLog(@"Restore1 OK!");
            
            [queue finishTransaction:transaction];
        }
        
        if ([transaction.payment.productIdentifier isEqualToString:@"プロダクトID3"]) {
            restore = YES;
            NSLog(@"Restore2 OK!");
            
            [queue finishTransaction:transaction];
        }
        
        if ([transaction.payment.productIdentifier isEqualToString:@"プロダクトID4"]) {
            restore = YES;
            NSLog(@"Restore3 OK!");
            
            [queue finishTransaction:transaction];
        }
    }
    
    // 一致するものがなかった場合
    if (restore == NO)
    {
        for (SKPaymentTransaction *transaction in queue.transactions) {
            [queue finishTransaction:transaction];
        }
        // コントローラを生成
        UIAlertController * ac =
        [UIAlertController alertControllerWithTitle:@"Error"
                                            message:@"Restre failed."//リストア失敗。
                                     preferredStyle:UIAlertControllerStyleAlert];
        
        // OK用のアクションを生成
        UIAlertAction * okAction =
        [UIAlertAction actionWithTitle:@"OK"
                                 style:UIAlertActionStyleDefault
                               handler:^(UIAlertAction * action) {
                                   // ボタンタップ時の処理
                                   NSLog(@"OK button tapped.");
                               }];
        
        // コントローラにアクションを追加
        [ac addAction:okAction];
        // アラート表示処理
        [self presentViewController:ac animated:YES completion:nil];
    }else{
        [self restoreSuccessAlart];
    }
}

最後にユーザーにリストア成功を伝えて完了。
あ、これはメソッド分けなくても良かったか。

-(void)restoreSuccessAlart
{
    UIAlertController * ac =
    [UIAlertController alertControllerWithTitle:@"Success"
                                        message:@"It succeeded to restore."
                                 preferredStyle:UIAlertControllerStyleAlert];
    
    // OK用のアクションを生成
    UIAlertAction * okAction =
    [UIAlertAction actionWithTitle:@"OK"
                             style:UIAlertActionStyleDefault
                           handler:^(UIAlertAction * action) {
                               // ボタンタップ時の処理
                               NSLog(@"OK button tapped.");
                           }];
    
    // コントローラにアクションを追加
    [ac addAction:okAction];
    // アラート表示処理
    [self presentViewController:ac animated:YES completion:nil];

}

あ、あと何らかの理由で失敗したら飛ぶらしいメソッド。よくわからん。

// リストアに失敗した場合
- (void) paymentQueue:(SKPaymentQueue *)queue restoreCompletedTransactionsFailedWithError:(NSError *)error {
    
    // コントローラを生成
    UIAlertController * ac =
    [UIAlertController alertControllerWithTitle:@"Error"
                                        message:@"Restre failed."//リストア失敗。
                                 preferredStyle:UIAlertControllerStyleAlert];
    
    // OK用のアクションを生成
    UIAlertAction * okAction =
    [UIAlertAction actionWithTitle:@"OK"
                             style:UIAlertActionStyleDefault
                           handler:^(UIAlertAction * action) {
                               // ボタンタップ時の処理
                               NSLog(@"OK button tapped.");
                           }];
    
    // コントローラにアクションを追加
    [ac addAction:okAction];
    // アラート表示処理
    [self presentViewController:ac animated:YES completion:nil];

}

こんな感じでどうですかね。
うじゃうじゃ。

コメントを残す

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です