我正在尝试使用IAP将付费应用程序转换为免费版本,因此基本上我需要检查用户是否购买了以前的版本,然后解锁了IAP项目,我不确定我是否在这里做!甚至有可能在开发过程中检查和跟踪“appStoreReceiptURL”?这是我的代码:
NSURL* url = [[NSBundle mainBundle] appStoreReceiptURL];
NSLog(@"receiptUrl %@",[url path]);
NSError* err = nil;
if (![url checkResourceIsReachableAndReturnError:&err]){
SKReceiptRefreshRequest* request = [[SKReceiptRefreshRequest alloc] initWithReceiptProperties:nil];
request.delegate = self;
[request start];
}
-(void)requestDidFinish:(SKRequest*)request{
if([request isKindOfClass:[SKReceiptRefreshRequest class]]){
NSLog(@"YES, You purchased this app");
}
}
-(void)request:(SKRequest*)request didFailWithError:(NSError *)error{
NSLog(@"NO, you need to buy it ");
}
现在我可以使用我的Apple ID登录,登录后告诉我
YES, You purchased this app"
,是的,我真的买了我的应用程序! ,我将确保一切正常。此过程是否应在每次更新中进行?
最佳答案
这是一个简单的解决方案
#import <StoreKit/StoreKit.h>
不要忘记添加其代表
<SKPaymentTransactionObserver, SKProductsRequestDelegate>
付款验证
- (IBAction)boughtIt:(id)sender {
NSURL* url = [[NSBundle mainBundle] appStoreReceiptURL];
NSLog(@"receiptUrl %@",[url path]);
NSError* err = nil;
if (![url checkResourceIsReachableAndReturnError:&err]){
SKReceiptRefreshRequest* request = [[SKReceiptRefreshRequest alloc] initWithReceiptProperties:nil];
request.delegate = self;
[request start];
_activity.hidden = NO;
}
}
-(void)requestDidFinish:(SKRequest*)request{
if([request isKindOfClass:[SKReceiptRefreshRequest class]]){
NSLog(@"YES, You purchased this app");
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Congrats !" message:@"Welcome To The World Of Dinosaurs" delegate:self
cancelButtonTitle:@"Cancel" otherButtonTitles:nil, nil];
[alert show];
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"isPurchased"];
[[NSUserDefaults standardUserDefaults] synchronize];
_activity.hidden = YES;
[self dismissViewControllerAnimated:YES completion:nil];
}
}
- (void)request:(SKRequest*)request didFailWithError:(NSError *)error{
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Sorry !" message:@"It seems you did not purchase this app before, if you like to unlock all content and features please purchase Paleontologist Pack" delegate:self
cancelButtonTitle:@"Cancel" otherButtonTitles:nil, nil];
[alert show];
_activity.hidden = YES;
// [self dismissViewControllerAnimated:YES completion:nil];
NSLog(@"NO, you need to buy it ");
}
仅当用户使用
PURCHASED
应用程序时,此方法才有效,但对于兑换代码不起作用