天天看點

iOS_内購IAP

iOS_内購IAP
iOS_内購IAP
iOS_内購IAP
iOS_内購IAP

填寫所有的資訊

iOS_内購IAP
iOS_内購IAP
iOS_内購IAP
iOS_内購IAP

設計APP購買界面(因為建立商品ID的時候,要上傳截圖)

iOS_内購IAP

必須回到itunesconnect,找到對應的版本,添加剛才的内購項目,必須點選儲存,如圖

iOS_内購IAP

添加沙箱測試員

iOS_内購IAP

最後,才是編碼階段

iOS_内購IAP

程式一啟動的時候,必須請求所有的可用的内購項目

// 内購相關
    // 請求有效的産品
    // 1) 建立自由的産品集合(可以從自己的伺服器擷取,或者從本地沙箱加載)
    NSSet *productSet = [NSSet setWithObjects:kHanYuProduct_vip, nil];
    [[IAPHelper sharedIAPHelper]requestProducts:productSet];
           
#import <UIKit/UIKit.h>

#import "IAPHelper.h"

#define kHanYuProduct_vip               @"com.beyond.xxx.vip"


// 定義一個枚舉
typedef enum {
    //  xxx元解鎖vip
    ProductType_HanYu_vip,
} ProductType;




@interface SGIAPTool : NSObject


// 是不是 已經買了
+ (BOOL)buy_HanYu_isAlreadyBuy;


// 非消耗品  解鎖(VIP)
+ (void)buy_HanYu_Unlock;
+ (void)reBuy_HanYu_Unlock;



@end
           
#import "SGIAPTool.h"


#import "IAPHelper.h"




@implementation SGIAPTool

+ (void)buy_HanYu_Unlock
{
    if([[IAPHelper sharedIAPHelper] canBuyIAP]){
        
        
        [self purchaseProduct];
    }else{
        [self showMessage:@"不允許程式内付費" duration:6];
        DLog(@"不允許程式内付費");
    }
}

// 購買或者回複産品成功之後,通常需要根據使用者的購買内容,設定界面或者更新使用者屬性
// 提示:如果不需要通過網絡伺服器記錄使用者的購買資訊,
// 可以将使用者的購買情況,儲存在"系統偏好"中
// 有很多的破解軟體,直接上傳修改過的plist檔案,達到破解的目的
// 是以,如果要儲存到系統偏好,一定要做加密!這個加密,一定要想辦法記住使用者手機的唯一标示!
+(void)purchaseProduct
{
    [self showMessage:@"正在購買,請稍後" duration:5];
    
    [[IAPHelper sharedIAPHelper]buyProduct:kHanYuProduct_vip completion:^(NSString *identifier) {
        [self showMessage:@"購買成功,學習愉快" duration:5];
        DLog(@"購買商品标示: %@", identifier);
        
        [self abstract_saveSuccessFlag];
        // 發送通知,關閉控制器
        [[NSNotificationCenter defaultCenter] postNotificationName:@"notification_unlockXXXSuccess" object:nil];
        
    } failed:^(NSString *reason) {
        [self showMessage:reason duration:5];
        DLog(@"購買失敗 %@", reason);
    }];
}




#pragma mark - 恢複購買
+(void)reBuy_HanYu_Unlock
{
    [self showMessage:@"正在恢複購買,請稍後" duration:5];
    
    
    
    
    [[IAPHelper sharedIAPHelper]restorePurchase:^(NSArray *products) {
        DLog(@"恢複産品數組: %@", products);
        [self abstract_saveSuccessFlag];
        [self showMessage:@"恢複購買成功" duration:3];
        
        
    } failed:^(NSString *reason) {
        [self showMessage:@"恢複購買失敗,請稍後再試" duration:3];
        DLog(@"恢複失敗 %@", reason);
    } ];
}





#pragma mark - 判斷是不是已經買了
+ (BOOL)buy_HanYu_isAlreadyBuy
{
    
    return 0;

    
    
}

+ (void)abstract_saveSuccessFlag
{
    do something
}

#pragma mark - 工具方法
+(void)showMessage:(NSString *)message duration:(NSInteger)duration
{
    UIWindow * window = [UIApplication sharedApplication].keyWindow;
    UIView *showview =  [[UIView alloc]init];
    showview.backgroundColor = [UIColor blackColor];
    showview.frame = CGRectMake(1, 1, 1, 1);
    showview.alpha = 1.0f;
    showview.layer.cornerRadius = 5.0f;
    showview.layer.masksToBounds = YES;
    [window addSubview:showview];
    
    UILabel *label = [[UILabel alloc]init];
    CGSize LabelSize = [message sizeWithFont:[UIFont systemFontOfSize:17] constrainedToSize:CGSizeMake(290, 9000)];
    label.frame = CGRectMake(10, 5, LabelSize.width, LabelSize.height);
    label.text = message;
    label.textColor = [UIColor whiteColor];
    label.textAlignment = 1;
    label.backgroundColor = [UIColor clearColor];
    label.font = [UIFont boldSystemFontOfSize:15];
    [showview addSubview:label];
    CGFloat SCREEN_WIDTH= window.frame.size.width;
    CGFloat SCREEN_HEIGHT= window.frame.size.height;
    showview.frame = CGRectMake((SCREEN_WIDTH - LabelSize.width - 20)/2, SCREEN_HEIGHT - 100, LabelSize.width+20, LabelSize.height+10);
    [UIView animateWithDuration:duration animations:^{
        showview.alpha = 0;
    } completion:^(BOOL finished) {
        [showview removeFromSuperview];
    }];
}


@end