In App Purchase - TaehoonADS/Study_iOS GitHub Wiki
์ฐธ๊ณ :+1: ์ธ์ฑ๊ฒฐ์ ์ ๋ํ ๋ธ๋ก๊ทธ_kr
์ฌ์ฉ๋๋ ํ๋ ์ ์ํฌ
- 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];