How to use appStoreReceiptURL

20.1k views Asked by At

transactionReceipt is deprecated. But I am not able to use,

[[NSBundle mainBundle] appStoreReceiptURL].

This is supposed to return a url to a receipt if there is one. But for me there isn't one, as this value is nil, and as far as I can tell it shouldn't be. I'm running on iOS 7 and have done a few in-app purchases (sandbox on the device).

Can anyone help .

3

There are 3 answers

0
Kundan On

This will give you the receipt as the contents of the mainBundle's appStoreReceiptURL :-

[NSData dataWithContentsOfURL:[[NSBundle mainBundle] appStoreReceiptURL]];

Once you get that convert NSData to NSString.

For more details, see this :-

https://developer.apple.com/library/ios/releasenotes/General/ValidateAppStoreReceipt/Chapters/ValidateRemotely.html#//apple_ref/doc/uid/TP40010573-CH104-SW1

0
Sazzadhusen Iproliya On
 - (void) someMethod {
      NSURL *receiptUrl = [[NSBundle mainBundle] appStoreReceiptURL];
      if ([[NSFileManager defaultManager] fileExistsAtPath:[receiptUrl path]]) {
       NSData *ios7ReceiptData = [NSData dataWithContentsOfURL:receiptUrl];
       //Do stuff

      } else {
         NSLog(@"iOS 7 AppReceipt not found %@, refreshing...",iapID);
         SKReceiptRefreshRequest *refreshReceiptRequest = [[SKReceiptRefreshRequest alloc] initWithReceiptProperties:@{}];
         refreshReceiptRequest.delegate = self;
         [refreshReceiptRequest start];
      }
 }

 - (void)requestDidFinish:(SKRequest *)request {
    if([request isKindOfClass:[SKReceiptRefreshRequest class]])
    {
      //SKReceiptRefreshRequest
      NSURL *receiptUrl = [[NSBundle mainBundle] appStoreReceiptURL];
      if ([[NSFileManager defaultManager] fileExistsAtPath:[receiptUrl path]]) {
        NSLog(@"App Receipt exists");
        //Do stuff
    } else {
        NSLog(@"Receipt request done but there is no receipt");

        // This can happen if the user cancels the login screen for the store.
        // If we get here it means there is no receipt and an attempt to get it failed because the user cancelled the login.
        //[self trackFailedAttempt];
     }
   }
 }
1
thomartisan On

try below:

NSData *dataReceipt = [NSData dataWithContentsOfURL:[[NSBundle mainBundle] appStoreReceiptURL]];
NSString *receipt = [dataReceipt base64EncodedStringWithOptions:0];