有关appStoreReceiptURL的问题

时间:2015-01-07 08:57:56

标签: ios objective-c iphone xcode ipad

我正在尝试将我的付费应用转换为带有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",是的,我真的买了我的应用程序! ,我要确保一切都好。 这个过程是否应该在每次更新中发生?

1 个答案:

答案 0 :(得分:1)

这是一个简单的解决方案

#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应用程序,它不适用于兑换代码