Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deprecated TransactionReceipt

I am using this code for in-app purchases, took it from RaywernderLich's tutorial.

// Encode the receiptData for the itms receipt verification POST request.
NSString *jsonObjectString = [self encodeBase64:(uint8_t *)transaction.transactionReceipt.bytes
                                         length:transaction.transactionReceipt.length];

Now Xcode is saying

'transactionReceipt' is deprecated: first deprecated in iOS 7.0

How to fix it?

like image 744
Ali Sufyan Avatar asked Oct 30 '13 12:10

Ali Sufyan


3 Answers

Regarding Deprecation

Since this question is technically asking how one should go about addressing the deprecated attribute its fair to assume that the OP is still deploying on an iOS version less than 7. Therefore you need to check for the availability of the newer API rather than calling it blindly:

Objective-C

Edit As pointed out in the comments you can't use the respondsToSelector on NSBundle since that API was private in previous iOS versions

NSData *receiptData;
if (NSFoundationVersionNumber >= NSFoundationVersionNumber_iOS_7_0) {
    receiptData = [NSData dataWithContentsOfURL:[[NSBundle mainBundle] appStoreReceiptURL]];
} else {
    receiptData = transaction.transactionReceipt;
}
//now you can convert receiptData into string using whichever encoding:)

Swift

Since Swift can only be deployed on iOS 7 and above we can use the appStoreReceiptURL safely

if let receiptData = NSData(contentsOfURL: NSBundle.mainBundle().appStoreReceiptURL!) {
    //we have a receipt
}

Regarding Receipt Validation

The newer API the receipt now contains the list of all transactions performed by the user. The documentation clearly outlines what a receipt looks like:

receipt outline

Meaning that if you really, really wanted to, you can iterate through all the items contained in the receipt to validate against each transaction.

For more on receipt validation you can read obc.io

like image 92
Daniel Galasko Avatar answered Nov 02 '22 08:11

Daniel Galasko


Replace with something like:

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

Convert NSData to NSString after that.....

like image 17
Nikos M. Avatar answered Nov 02 '22 08:11

Nikos M.


NSURL *receiptURL = [[NSBundle mainBundle] appStoreReceiptURL];
NSData *receipt = [NSData dataWithContentsOfURL:receiptURL];
if(!receipt) {
 /* No local receipt -- handle the error. */ 
}
NSString *jsonObjectString = [receipt base64EncodedString];
like image 10
Phil Avatar answered Nov 02 '22 07:11

Phil