我正在尝试使用 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 登录,登录后它告诉我是的,您购买了这个应用程序“
,是的,我真的购买了我的应用程序!,我要确保一切正常。
这个过程是否应该在每次更新中发生?
最佳答案
这是一个简单的解决方案
#import <StoreKit/StoreKit.h>
不要忘记添加它的委托(delegate)
<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
应用程序时有效,它不适用于兑换代码
关于ios - 关于 appStoreReceiptURL 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27815668/