填写所有的信息
设计APP购买界面(因为创建商品ID的时候,要上传截图)
必须回到itunesconnect,找到对应的版本,添加刚才的内购项目,必须点击保存,如图
添加沙箱测试员
最后,才是编码阶段
程序一启动的时候,必须请求所有的可用的内购项目
// 内购相关
// 请求有效的产品
// 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