IOS中的IAP參考教程

生成productID的步驟,參考這篇教程:http://mobile.51cto.com/iphone-389249.htm ,附加一篇關於cocos2dx在IOS上實現IAP的教程:http://www.tairan.com/archives/5515  ,後者生成productID的步驟思路較清晰容易理解,建議參考後者的步驟app

給出的生成productID教程中的信息已經和如今的developer.apple.com中有些出入,請靈活變通運用,生成可用的productIDiphone


如下僅僅是參考的實現代碼,若有須要請酌情變動相應的內容學習


.h文件測試

導入storekit.frameworkui

文件中很簡單,創建一個簡單的繼承於NSObject的子類,只須要引入兩個協議spa

#import <Foundation/Foundation.h>.net

#import <StoreKit/StoreKit.h>代理


@interface IapDelegate : NSObject<SKProductsRequestDelegate, SKPaymentTransactionObserver,UIAlertViewDelegate>rest


- (void)methodOfShopBtn;code


@end


-------------------------------------------------------------------------------------------------------------分隔線


.m文件

這個文件中僅僅是實現兩個協議的一些方法,我添加觸發方法,,,,若是測試僅僅是測試,學習,能夠直接把getProductInfo這個方法中紅色標出的productID改爲讀者所生成的productID應該就能夠直接運行


#import "IapDelegate.h"

#include "cocos2d.h"


@implementation IapDelegate


#pragma mark - 方法

- (void)methodOfShopBtn(整個IAP的進口,觸發方法)

{

    

    if ([SKPaymentQueue canMakePayments]) {

        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"當即開啓提示功能" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"購買", nil];

        [alertView show];

    }

    else

    {

        NSLog(@"用戶禁止輸入");

    }

}


- (void)getProductInfo

{

    //IOS中原生storekit調用代碼

    [[SKPaymentQueue defaultQueue] addTransactionObserver:self];

        NSSet *set = [NSSet setWithArray:@[@"你的productID"]];

        SKProductsRequest *request = [[SKProductsRequest alloc] initWithProductIdentifiers:set];

        request.delegate = self;

        [request start];

}


- (void)completeTransaction:(SKPaymentTransaction *)transaction

{

    NSString *productIdentifier = transaction.payment.productIdentifier;

    NSString *receipt = [transaction.transactionReceipt base64Encoding];

    

    if ([productIdentifier length] > 0) {

        //

    }

    [[SKPaymentQueue defaultQueue] finishTransaction:transaction];

}


- (void)failedTransaction:(SKPaymentTransaction *)transaction

{

    if (transaction.error.code != SKErrorPaymentCancelled) {

        NSLog(@"購買失敗");

    }

    else{

        NSLog(@"用戶取消交易");

    }

    [[SKPaymentQueue defaultQueue] finishTransaction:transaction];

}


- (void)restoreTransaction:(SKPaymentTransaction *)transaction

{

    [[SKPaymentQueue defaultQueue] finishTransaction:transaction];

}


#pragma mark - SKProductsRequestDelegate方法

-(void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response

{

    NSArray *myProduct = response.products;

    if (myProduct.count == 0) {

        NSLog(@"沒法獲取產品信息,購買失效");

        return ;

    }

    SKPayment *payment = [SKPayment paymentWithProduct:myProduct[0]];

    NSLog(@"測試:%@", payment.productIdentifier);

    [[SKPaymentQueue defaultQueue] addPayment:payment];

}

#pragma mark - skpaymentObserver方法

-(void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions

{

    for (SKPaymentTransaction *transaction in transactions) {

        switch (transaction.transactionState) {

            case SKPaymentTransactionStatePurchased:{

                NSLog(@"transactionIdentifier = %@", transaction.transactionIdentifier);

                cocos2d::UserDefault *userDefaults = cocos2d::UserDefault::getInstance();

                userDefaults -> setBoolForKey("ShowSolution", YES);

                [self completeTransaction:transaction];

                break;

            }

            case SKPaymentTransactionStateFailed:

                [self failedTransaction:transaction];

                break;

            case SKPaymentTransactionStateRestored:

                [self restoreTransaction:transaction];

                break;

            case SKPaymentTransactionStatePurchasing:

                NSLog(@"商品添加進列表");

                break;

            default:

                break;

        }

    }

}


#pragma mark - uialertview代理方法

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex

{

    NSLog(@"按鈕%d",buttonIndex);

    if (buttonIndex == 1) {

        [self getProductInfo];

    }

}


@end

相關文章
相關標籤/搜索