最近的项目需要用到一种包月机制,所以和公司的大神进行了研究,现在记录一下我们的方式
为了方便阅读,还是从头开始,首先需要建立一个产品,和一个测试账号
打开iTunesConnect网站:https://itunesconnect.apple.com/WebObjects/iTunesConnect.woa (注意:企业级的用户必须使用公司主开发者账号登陆才可!)
创建一个测试账号
在这里说明一下几个比较需要注意的选项
Email Address:这就代表的账号
Password:密码必须有一个是大写
Select iTunes Store:这个尤其要注意,注意选择好自己的商店区域
然后点击save 就可以创建好一个测试账号了!
当然还要创建一个商品,否则我们的一切行为就没有意义了
点击,然后选择你相应的app产品,点击,然后再点击create new 就可以创建产品了.这里会让你选择产品的类型,我们选择
点击save 就可以保存产品了
然后进入代码阶段,这里需要用到的第三方是EBPurchase,MBProgress,PDKeychainBindings
//判断是否可以购买
if ([self.purchase requestProduct:@"ultimate_plan_over"] == TRUE)
{
[MBProgressHUD showHUDAddedTo:self.navigationController.view animated:TRUE];
}
else
{
UIAlertView* alertView = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Notice", nil)
message:NSLocalizedString(@"Your app store do not support IAP feature", nil)
delegate:self
cancelButtonTitle:nil
otherButtonTitles:NSLocalizedString(@"Ok", nil), nil];
[alertView show];
}
有关于购买的详细内容,就不多介绍了,可以参考这篇文章:IAP
在交易完成以后,添加代理
case SKPaymentTransactionStatePurchased:
// Item was successfully purchased!
// Return transaction data. App should provide user with purchased product.
if ([transaction.payment.productIdentifier isEqualToString:@"ultimate_plan_over"]) {
NSLog(@"11");
if ([delegate respondsToSelector:@selector(addUltimatePlanD:)]) {
[delegate addUltimatePlanD:transaction.payment.productIdentifier];
}
}else if([transaction.payment.productIdentifier isEqualToString:@"basic_plan_over"]){
if ([delegate respondsToSelector:@selector(didBasicPlan:)])
[delegate didBasicPlan:transaction.payment.productIdentifier];
}else{
if ([delegate respondsToSelector:@selector(successfulPurchase:restored:identifier:receipt:)])
[delegate successfulPurchase:self restored:NO identifier:transaction.payment.productIdentifier receipt:transaction.transactionReceipt];
}
/**
完成包月的代理方法
@param productId : 产品标识
*/
- (void)addUltimatePlanD:(NSString *)productId{
self.btn02.enabled = NO;
self.pay2.hidden = NO;
[[DataCenter sharedDataCenter] addUltimatePlan:productId];
}
/**
包月保存到本地的方法
@param identify : 产品标识
*/
- (void)addUltimatePlan:(NSString *)identify{
NSDate *datenow = [NSDate date];//现在时间
NSString *timeSp = [NSString stringWithFormat:@"%ld", (long)[datenow timeIntervalSince1970]];
//[[NSUserDefaults standardUserDefaults]setObject:timeSp forKey:@"UltimatePlan"];
//[[NSUserDefaults standardUserDefaults]synchronize];
[self removeBannerView];
if (![self isBasicPlan]) {
[[PDKeychainBindings sharedKeychainBindings] setString:@"basic_plan_over" forKey:@"themeAndSticker"];
}
[[PDKeychainBindings sharedKeychainBindings] setString:jsonStr forKey:@"UltimatePlan"];
}
/**
判断是否包月
@returns BOOL yesORno
*/
- (BOOL)isUltimatePlan
{
NSString *info = [[PDKeychainBindings sharedKeychainBindings] stringForKey:@"UltimatePlan"];
//NSString *info = [[NSUserDefaults standardUserDefaults]objectForKey:@"UltimatePlan"];
NSDate *datenow = [NSDate date];
long now = (long)[datenow timeIntervalSince1970];
long before = [info longLongValue];
BOOL isUltimatePlan = (now - before)>3*60?0:1;
return isUltimatePlan;
}