본문 바로가기
프로그래밍/iOS

인엡 재구매 복구 로직(restore button)

by 백룡화검 2012. 7. 21.
출처 : http://cafe.naver.com/mcbugi/219432
원문(리스토어와 인엡 풀소스) - http://www.changwoo.net/bbs/bbsDetail.do?&num=545

애플의 정책이 바뀌어 non-consume in-app은 무조건 restore(복구) UI가 있어야 합니다, 안그러면 리젝사유가 되어 가슴아픈 경험을 하게 됩니다.

그에 따른 로직을 올립니다.

버튼에 이벤트로
- (void) onClickRestore:(id)sender 를 호출하면
checkPurchasedItems ->
 상황1 - 로그인 취소- >
(void)paymentQueue:(SKPaymentQueue *)queue restoreCompletedTransactionsFailedWithError:(NSError *)error 
 상황2  - 로그인 성공 -> (void)paymentQueueRestoreCompletedTransactionsFinished:(SKPaymentQueue *)queue

요런 로직입니당
로그인이 성공하면
paymentQueueRestoreCompletedTransactionsFinished함수에서 그동안 구입한 인엡에 대한 목록(product id)을 얻어옵니다
그러면 콜백받은 인엡목록과 본어플의 인엡의 코드(product id)를 검사해서 있는지 확인한후 처리 하시면 되겠습니다.




//복구 버튼 이벤트
- (
void) onClickRestore:(id)sender
{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSArray *languages = [defaults objectForKey:@"AppleLanguages"];
    NSString *currentLanguage = [languages objectAtIndex:0];
     
    NSString *requestString = @"";
    //Language process
    if ([currentLanguage isEqualToString:@"ko"]) {
        //        requestString = @"복구 요청중";
    }else{
        //        requestString = @"Restore requesting";
    }
     
    HUD = [[MBProgressHUD alloc] initWithWindow:[[UIApplication sharedApplication] keyWindow]];
    //    HUD.center = CGPointMake(10, 110);
    [[[UIApplication sharedApplication] keyWindow] addSubview:HUD];
    HUD.delegate = self;
    HUD.labelText = requestString;
    [HUD show:YES];   
     
    [self checkPurchasedItems];
}
 
- (void) checkPurchasedItems
{
     
    [[SKPaymentQueue defaultQueue] addTransactionObserver:self];
    [[SKPaymentQueue defaultQueue] restoreCompletedTransactions];
    [HUD show:NO];
     
}// Call This Function
 
- (void)paymentQueue:(SKPaymentQueue *)queue removedTransactions:(NSArray *)transactions
{
    NSLog(@"- (void)paymentQueue:(SKPaymentQueue *)queue removedTransactions:(NSArray *)transactions ");
}
// Sent when an error is encountered while adding transactions from the user's purchase history back to the queue.
- (void)paymentQueue:(SKPaymentQueue *)queue restoreCompletedTransactionsFailedWithError:(NSError *)error
{
    NSLog(@"- (void)paymentQueue:(SKPaymentQueue *)queue restoreCompletedTransactionsFailedWithError:(NSError *)error ");   
    [HUD hide:YES];
}
// Then this is called
- (void)paymentQueueRestoreCompletedTransactionsFinished:(SKPaymentQueue *)queue
{
    NSLog(@"%@",queue );
    NSLog(@"Restored Transactions are once again in Queue for purchasing %@",[queue transactions]); 
     
    NSMutableArray *purchasedItemIDs = [[NSMutableArray alloc] init];
    NSLog(@"received restored transactions: %i", queue.transactions.count);
     
     
    //결재 기록이 없을때 alert 뛰우기
    if(queue.transactions.count==0){
        //        NSString *fileMessage = NSLocalizedString(@"NOTRESTORE", @"restore");
         
        NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
        NSArray *languages = [defaults objectForKey:@"AppleLanguages"];
        NSString *currentLanguage = [languages objectAtIndex:0];
         
        NSString *failMessage;
        //Language process
        if ([currentLanguage isEqualToString:@"ko"]) {
            failMessage = @"구매 기록이 없습니다.";
        }else{
            failMessage = @"There is no record of your purchase.";
        }
         
        UIAlertView *resultView = [[UIAlertView alloc] initWithTitle:@"Failed"
                                                             message:failMessage
                                                            delegate:self
                                                   cancelButtonTitle:nil
                                                   otherButtonTitles:@"OK", nil];
        [resultView show];
    }
     
    for (SKPaymentTransaction *transaction in queue.transactions)
    {
        NSString *productID = transaction.payment.productIdentifier;
        [purchasedItemIDs addObject:productID];
        NSLog (@"product id is %@" , productID);
        // here put an if/then statement to write files based on previously purchased items
        // example if ([productID isequaltostring: @"youruniqueproductidentifier]){write files} else { nslog sorry}
         
        if([productID isEqualToString:KONGLISH_PRODUCTID])
        {
            //재구입확인dh
            NSLog(@"already buy");
        }
    
     
    [HUD hide:YES];
     
}