22

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 .

Janmenjaya
  • 4,149
  • 1
  • 23
  • 43
  • the `appStoreReceiptURL` is a method on an instance of NSBundle, not a class method as you've shown in your question. Are you sure you're calling it on the main bundle as described the documentation? `[[NSBundle mainBundle] appStoreReceiptURL]` – Jasarien Dec 18 '14 at 10:33
  • @Jasarien, but where would i get this url? If i will get the url from the apple server, will it be automatically saved in the main bundle of the project? Should I directly call "[[NSBundle mainBundle] appStoreReceiptURL]" inside the "updatedTransactions" delegate method? I am sorry, as it may be common question, but i am new here. Can you please elaborate in detail? –  Dec 18 '14 at 10:57
  • The URL will be a file URL to the location of the receipt data within your application's sandbox. If the receipt file is there that method will return you the file URL to it, if it is not there then you won't get any URL. – Jasarien Dec 18 '14 at 10:58

3 Answers3

15

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

Kundan
  • 3,084
  • 2
  • 28
  • 65
8

try below:

NSData *dataReceipt = [NSData dataWithContentsOfURL:[[NSBundle mainBundle] appStoreReceiptURL]];
NSString *receipt = [dataReceipt base64EncodedStringWithOptions:0];
Tunaki
  • 132,869
  • 46
  • 340
  • 423
tangkunyin
  • 1,383
  • 1
  • 8
  • 9
0
 - (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];
     }
   }
 }