saturngod / IAPHelper

No more maintenance for this repo. Please use the https://github.com/bizz84/SwiftyStoreKit
MIT License
1.55k stars 280 forks source link

App Crash if Apple's Receipt Servers return nil #51

Open AlbertRenshaw opened 6 years ago

AlbertRenshaw commented 6 years ago

When validating receipt, Apple's response can be nil, which causes the app to crash when it tries to convert nil to JSON via NSDictionary* rec = [IAPShare toJSON:response];

I propose a modification simply in the read-me doc's example code, to now be as follows:

[[IAPShare sharedHelper].iap checkReceipt:[NSData dataWithContentsOfURL:[[NSBundle mainBundle] appStoreReceiptURL]] AndSharedSecret:@"your sharesecret" onCompletion:^(NSString *response, NSError *error) {

    if (response)
    {

        //Convert JSON String to NSDictionary
        NSDictionary* rec = [IAPShare toJSON:response];

        if([rec[@"status"] integerValue]==0)
        {

          [[IAPShare sharedHelper].iap provideContentWithTransaction:trans];
            NSLog(@"SUCCESS %@",response);
            NSLog(@"Purchases %@",[IAPShare sharedHelper].iap.purchasedProducts);
        }
        else {
            NSLog(@"Fail");
        }

    }
    else {
        NSLog(@"Fail (Apple server malfunction or connection interruption)");
    }

}];