Develop
2013.11.20 11:50
[ios] iOS In App Purchase 코드 부분 샘플 1
조회 수 11770 댓글 0
실제 코드에 IAP 삽입 방법.
일단 cocos2d cclayer로 store.h/m을 만든다.
store.h 파일 내용
#import <StoreKit/StoreKit.h> /* 아래 delegate를 지정해야 함. SKProductsRequestDelegate : 상품정보 획득에 사용 SKPaymentTransactionObserver : 상품 구매에 관련(구입성공,실패,재구입 등 구매 트랜잭션) */ //@interface store : CCLayer @interface store : UIViewController <SKProductsRequestDelegate, SKPaymentTransactionObserver> { SKProductsRequest *request; SKPayment *payment; NSMutableArray *productDetailsList; NSMutableArray *productIdentifierList; } ...
store.m 파일 내용
-(id) init{ if( (self=[super init])) { [[SKPaymentQueue defaultQueue] addTransactionObserver:self]; //이게 없으면 작동안함 productDetailsList = [[NSMutableArray alloc] init]; productIdentifierList = [[NSMutableArray alloc] init]; [productIdentifierList addObject:@"인앱ID1"]; [productIdentifierList addObject:@"인앱ID2"]; [productIdentifierList addObject:@"인앱ID3"]; [productIdentifierList addObject:@"인앱ID4"]; [productIdentifierList addObject:@"인앱ID5"]; [productIdentifierList addObject:@"인앱ID6"]; //인앱ID는 등록하신 ID 그냥 갔다쓰면 됨(그대로쓰면 됨) //이부분은 솔직히 넣야되는지 없어도 되는지 잘모르겠.. if ([SKPaymentQueue canMakePayments]) { NSLog(@"GOOD"); } else { NSLog(@"NOPE"); } //이부분은 인앱 구매사용가능한 상태면 GOOD, 아니면 NOPE //그다음에 메뉴 버튼 만들고 셀렉터를 원하는 것으로 골라서하면 됨. //셀렉터를 buy라고 그냥 정하고 진행하겠음. } return self; } -(void)buy{ request = [[SKProductsRequest alloc] initWithProductIdentifiers: [NSSet setWithArray:productIdentifierList]]; request.delegate = self; [request start]; payment = [SKPayment paymentWithProductIdentifier:@"인앱ID1"]; [[SKPaymentQueue defaultQueue] addPayment:payment]; [request release]; //이렇게 하면 인앱ID1에 대하여 결제가 이루어짐. } //요기부터 아래는 그냥 넣으세요 -(void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response { { [productDetailsList addObjectsFromArray: response.products]; //[productDisplayTableView reloadData]; NSLog(@"products count = %d", [response.products count]); NSLog(@"SKProductRequest got response"); for (SKProduct *product in response.products) { NSLog(@"Title : %@", product.localizedTitle); NSLog(@"Description : %@", product.localizedDescription); NSLog(@"Price : %@", product.price); } NSLog(@"response.invalidProductIdentifiers count = %d", [response.invalidProductIdentifiers count]); for (int i=0; i<[response.invalidProductIdentifiers count]; i++) { NSString *invalidString = [response.invalidProductIdentifiers objectAtIndex:i]; NSLog(@"Invalid Identifiers : %@", invalidString); //이건뭐 제대로 안된경우에 로그로 알려줌 } } } -(void)purchasedTransaction:(SKPaymentTransaction *)transaction{ NSLog(@"구매성공"); //구매성공이면 로그로 구매성공이라고 뜨구요 [[SKPaymentQueue defaultQueue] finishTransaction:transaction]; //이 부분에 원하는 코드를 넣으면 구매되고 나서 적용되니까 알아서 사용 } -(void)restoreTransaction:(SKPaymentTransaction *)transaction{ NSLog(@"재구매"); [[SKPaymentQueue defaultQueue] finishTransaction:transaction]; //이 부분은 재구매 할 때라고 하는데 제대로 작용하는지는 모름 } -(void)failedTransaction:(SKPaymentTransaction *)transaction{ NSLog(@"구매실패"); [[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; } } } - (void) dealloc//디얼록하실때 제대로 안해주시면 누수현상나요~ { [[SKPaymentQueue defaultQueue] removeTransactionObserver:self]; //이부분부터 꼭 넣어야 함. [productDetailsList release];//요것도 [productIdentifierList release];//요것도 [super dealloc]; }
위에 productDetailsList , productIdentifierList 이런 것들은 넣지 않아도 되겠지만 혹시 모르니까..
[출처] http://cafe.naver.com/mcbugi/200626
번호 | 분류 | 제목 | 글쓴이 | 날짜 | 조회 수 |
---|---|---|---|---|---|
573 | PPT | [ppt] 시스템보안 연구실 ppt 템플릿.. (CIST) | hooni | 2013.04.23 | 20873 |
572 | Develop | [c++] BHO 프로그램 동작 원리.. 간단하게.. | hooni | 2013.04.23 | 43108 |
571 | Develop | [c++] 현승이가 준 P2P 프로그램 소스 ㅋㅋ | hooni | 2013.04.23 | 12074 |
570 | PPT | [ppt] 뚜레쥬르 온라인 마케팅 제안서 | hooni | 2013.04.23 | 23501 |
569 | Develop | [php] 심플한 게시판 ㅋㅋ 1 | hooni | 2013.04.23 | 12995 |
568 | System/OS | [windows] 최근 열어본 문서목록 안나오게 하는 방법 | hooni | 2013.04.23 | 25487 |
567 | Develop | [jsp] RSS(xml) 파싱해서 보여주기 ㅎㅎ | hooni | 2013.04.23 | 13217 |
566 | Develop | [php] 한샘 전자발주 시스템.. | hooni | 2013.04.23 | 11989 |
565 | Develop | [c#] 파일(File) 쓰기 ㅎㅎ | hooni | 2013.04.23 | 13300 |
564 | Develop | [js] 자바스크립트를 동적으로 로딩하기 | hooni | 2013.04.23 | 13579 |
563 | PPT | [php] 프레임워크 발표자료 및 샘플 구현 소스 | hooni | 2013.04.23 | 17503 |
562 | Develop | [unix] 날짜 관련 쉘 명령어 (특정일 또는 +-시간, 날짜 계산된 값) | hooni | 2013.04.23 | 14828 |