In App Purchase - TaehoonADS/Study_iOS GitHub Wiki

์ฐธ๊ณ  :+1: ์ธ์•ฑ๊ฒฐ์ œ์— ๋Œ€ํ•œ ๋ธ”๋กœ๊ทธ_kr

์‚ฌ์šฉ๋˜๋Š” ํ”„๋ ˆ์ž„ ์›Œํฌ

  1. StoreKit

์‚ฌ์šฉ๋˜๋Š” ๊ฐ์ฒด 1.SKPaymentQueue

+canMakePayments() ; ์Šคํ† ์–ด๊ฐ€ ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•œ๊ฐ€? +defaultQueue() ; ์ดˆ๊ธฐ Queue ๋ฐ˜ํ™˜

addTransactionObsever:object ;์˜ต์ ธ๋ฒ„๋ฅผ ๋“ฑ๋กํ•œ๋‹ค. ์ถ”ํ›„์—

์˜ต์ ธ๋ฒ„๋กœ ๋“ฑ๋ก๋œ ํด๋ž˜์Šค๋Š” SKPaymentTransactionObserver Protocol ๊ตฌํ˜„ํ•ด์•ผํ•จ.

#import <StoreKit/SKProductsRequest.h>
#import <StoreKit/SKProduct.h>
#import <StoreKit/SKPaymentQueue.h>
#import <StoreKit/SKPaymentTransaction.h>

-(void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions{
    for(SKPaymentTransaction *transaction in transactions){
        switch(transaction.transactionState){
        case SKPaymentTransactionStatePurchased:
            [self completeTransaction:transaction];
            break;
        case SKPaymentTransactionStateFailed:
            [self failedTransaction:transaction];
            break;
        case SKPaymentTransactionStateRestored:
           [self resotreTransaction:transaction];
        default:
            break;
        }
    }
} 

transaction ์ƒํƒœ์— ๋”ฐ๋ผ ์„ฑ๊ณต์ธ์ง€, ์‹คํŒจ์ธ์ง€, ๋ณต๊ตฌ๋˜์—ˆ๋Š”์ง€์— ๋”ฐ๋ผ ์ฒ˜๋ฆฌํ•ด ์ฃผ์–ด์•ผํ•œ๋‹ค.


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

-(void)failedTransaction:(SKPaymentTransaction*)transaction{
    NSLog(@"SKPaymentTransactionStatFailed");
    [SKPaymentQueue defaultQueue](/TaehoonADS/Study_iOS/wiki/SKPaymentQueue-defaultQueue) finishTransaction:transaction];
}

-(void)completeTransaction:(SKPaymentTransaction*)transaction{
    NSLog(@"SKPaymentTransactionStatePurchased");

    NSLog(@"Transaction Identifier : %@", transaction.transactionIdentifier);
    NSLog(@"Transaction Date : %@", transaction.transactionDate);
    
    [[SKPaymentQueue defaultQueue] finishTransaction:transaction];
}

์•„์ดํ…œ ์ •๋ณด๋ฅผ ๊ฐ€์ ธ์˜ค๋ ค๋ฉด SKProductRequest ๊ฐ์ฒด๊ฐ€ ์š”์ฒญ์„ ํ•˜๊ณ  ๊ทธ ๊ฒฐ๊ณผ์— ๋”ฐ๋ผ delegate์˜ productRequest:didReceiveResponse ๋ฉ”์†Œ๋“œ๋ฅผ ํ˜ธ์ถœํ•œ๋‹ค.


SKProductsRequest *productRequest = [SKProductsRequest alloc] initWithProductIdentifiers:[NSSet setWithObject:@"testitem_01"](/TaehoonADS/Study_iOS/wiki/SKProductsRequest-alloc]-initWithProductIdentifiers:[NSSet-setWithObject:@"testitem_01");

productRequest.delegate = self;

[productRequest start];

delegate ๋Š” SKProductRequestDelegate Protocol ๊ตฌํ˜„์„ ํ•˜๋ฉด ๋œ๋‹ค.


-(void)productsRequest:(SKProductsRequest*)request didReceiveResponse:(SKProductsResponse*)response{
    NSLog(@"SKProductRequest got response");
    if([response.products count]) > 0 ) {
        NSString *invalidString = [response.invalidProductIdentifiers objectAtIndex:0];
        NSLog(@"Invalid Identifiers : %@", invalidString);
    }
}

์›ํ•˜๋Š” ์•„์ดํ…œ ๊ฒฐ์ œ๋ฅผ ์š”์ฒญํ•˜๋ ค๋ฉด ์–ด๋–ป๊ฒŒ ํ•ด์•ผํ•˜๋Š”๊ฐ€?


SKPayment *payment = [SKPayment paymentWithProduct:product];
[[SKPaymentQueue defaultQueue] addPayment:payment];