关于 appStoreReceiptURL 的问题

Issue about appStoreReceiptURL

我正在尝试使用 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 应用程序时有效,它不适用于兑换代码