Develop
2013.11.20 13:16
[ios] iOS In App Purchase 코드 부분 샘플 2
조회 수 11286 댓글 0
쿠드 구현 부분..
이 샘플이 설명이 더 자세한 듯..
storekit.framework를 추가 후 소스코드(.h/.m)에 대한 설명임.
# 헤더 파일 내용
#import <UIKit/UIKit.h> #import <StoreKit/StoreKit.h> //스토어킷 프레임워크 추가해주시구요. /* 아래 delegate를 지정해야 함. SKProductsRequestDelegate : 상품정보 획득에 사용 SKPaymentTransactionObserver : 상품 구매에 관련(구입성공,실패,재구입 등 구매 트랜잭션) */ @interface iAPTest : UIViewController <SKProductsRequestDelegate, SKPaymentTransactionObserver> { } @end
# 코드 내용
#import "iAPTest.h" #import <StoreKit/StoreKit.h> // 각자의 판매아이템들에 대한 고유한 값이며 // Manage In App Purchase에서 등록한 값으로 설정하면 됨. #define kProductId1 @"com.iAPtest.item1" #define kProductId2 @"com.iAPtest.item2" #define kProductId3 @"com.iAPtest.item3" @implementation iAPTest - (void)viewDidLoad { //아래의 코드를 추가하여 구매 관련 이벤트를 자신이 처리하겠다고 지정. [[SKPaymentQueue defaultQueue] addTransactionObserver:self]; } //선택된 아이템에 대한 정보를 획득하여 표시해줄 때 사용. -(void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response{ for(SKProduct *product in response.products){ /* localizedDescription //아이템 설명 localizedTitle //아이템 이름 price //아이템 가격 priceLocale //지역별 가격표시 ex) $,원 표시등.. productIdentifier //제품식별코드 아래와 같이 product.xxxxx 로 접근해서 화면에 뿌려주면 될 듯. product.localizedTitle */ } [request release]; } //아래 매소드는 해당 아이템의 버튼을 클릭했을 시 연결되는 부분. //각각의 버튼에 구매될 아이템의 정보를 서버로 부터 얻어와서 해당 제품ID 순으로 정렬하고 //버튼 이벤트를 아래 메소드에 연결해서 0번째는 0번째 아이템, 1번째는 1번째 아이템 순으로 연결 //여기에 대한 내용은 각자의 입맛에 맛게^_^; -(void)buyitem:(id)sender{ SKPayment *payment; SKProductsRequest *request; UIButton *button = (UIButton *)sender; NSInteger tags = button.tag; if(tags==0){ // 선택된 아이템의 구매정보를 입력합니다. payment = [SKPayment paymentWithProductIdentifier:kProductId1]; /* // 선택된 아이템의 정보를 요청합니다. request = [[SKProductsRequest alloc] initWithProductIdentifiers:[NSSet setWithObject:kProductId1]]; */ }else if(tags==1){ payment = [SKPayment paymentWithProductIdentifier:kProductId2]; }else{ payment = [SKPayment paymentWithProductIdentifier:kProductId3]; } request.delegate=self; //아래의 코드에서 선택된 제품의 정보를 요청하고, 구매요청을 합니다. //[request start]; [[SKPaymentQueue defaultQueue] addPayment:payment]; } -(void)purchasedTransaction:(SKPaymentTransaction *)transaction{ /* . . 원하는 기능 추가. . */ [[SKPaymentQueue defaultQueue] finishTransaction:transaction]; } -(void)restoreTransaction:(SKPaymentTransaction *)transaction{ /* . . 원하는 기능 추가. . */ [[SKPaymentQueue defaultQueue] finishTransaction:transaction]; } -(void)failedTransaction:(SKPaymentTransaction *)transaction{ /* . . 원하는 기능 추가. . */ [[SKPaymentQueue defaultQueue] finishTransaction:transaction]; } - (void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions{ for(SKPaymentTransaction *transaction in transactions){ switch(transaction.transactionState) { case SKPaymentTransactionStatePurchased: //구매완료. [self purchasedTransaction:transaction]; break; case SKPaymentTransactionStateFailed: //구매실패. [self failedTransaction:transaction]; break; case SKPaymentTransactionStateRestored: //재구매, [self restoreTransaction:transaction]; break; default: break; } } } @end
[출처] http://cafe.naver.com/mcbugi/75067
번호 | 분류 | 제목 | 글쓴이 | 날짜 | 조회 수 |
---|---|---|---|---|---|
909 | Develop | '2014 모바일 개발 트렌드' 발표자료입니다. | hooni | 2014.10.02 | 990 |
908 | Develop | [ios] iOS 8 개발자가 우선 알아야 할 3가지 | hooni | 2014.10.02 | 911 |
907 | Develop | [js] jQuery 셀 병합 1 | hooni | 2014.09.23 | 3503 |
906 | Etc | IoT가 만드는 미래와 플랫폼 경쟁력 | hooni | 2014.09.23 | 0 |
905 | System/OS | SVN(Subversion) 설치와 설정 (sasl 인증 적용 포함) | hooni | 2014.09.11 | 5681 |
904 | System/OS | [linux] yum 업데이트 시 커널 제외하기 | hooni | 2014.09.11 | 1268 |
903 | Develop | [ios] Xcode cannot run using the selected device | hooni | 2014.08.14 | 1800 |
902 | Develop | [ios] Objective-C 에서 자주 사용하는 수학 함수와 유용한 Define | hooni | 2014.08.08 | 1869 |
901 | Develop | [ios] 카테고리 확장 메소드를 찾지 못하는 경우 | hooni | 2014.08.08 | 2004 |
900 | Develop | [ios] @property의 속성 (strong, weak, copy) 사용 경우 | hooni | 2014.08.08 | 1667 |
899 | Algorithm | OCB5 Injection 앗싸뵹! ㅋㅋ | hooni | 2014.07.01 | 811 |
898 | Develop | [ios] TextField 특정 문자만 사용하도록 하기 | hooni | 2014.06.30 | 2687 |