Skip to content

Commit

Permalink
Fixed build warnings robotmedia#214
Browse files Browse the repository at this point in the history
fixed deprecation warnings for NSURLSessionDataTask vs NSURLConnection
  • Loading branch information
everappz committed Mar 24, 2024
1 parent 48281e2 commit 9cc8cf0
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 50 deletions.
100 changes: 51 additions & 49 deletions RMStore/Optional/RMStoreTransactionReceiptVerifier.m
Original file line number Diff line number Diff line change
Expand Up @@ -137,64 +137,66 @@ - (void)verifyRequestData:(NSData*)requestData
request.HTTPBody = requestData;
static NSString *requestMethod = @"POST";
request.HTTPMethod = requestMethod;

dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSError *error;
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:&error];
dispatch_async(dispatch_get_main_queue(), ^{
if (!data)
{
RMStoreLog(@"Server Connection Failed");
NSError *wrapperError = [NSError errorWithDomain:RMStoreErrorDomain code:RMStoreErrorCodeUnableToCompleteVerification userInfo:@{NSUnderlyingErrorKey : error, NSLocalizedDescriptionKey : NSLocalizedStringFromTable(@"Connection to Apple failed. Check the underlying error for more info.", @"RMStore", @"Error description")}];
if (failureBlock != nil)
NSURLSessionDataTask* task = [[NSURLSession sharedSession] dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
dispatch_async(dispatch_get_main_queue(), ^{
if (!data)
{
RMStoreLog(@"Server Connection Failed");
NSError *wrapperError = [NSError errorWithDomain:RMStoreErrorDomain code:RMStoreErrorCodeUnableToCompleteVerification userInfo:@{NSUnderlyingErrorKey : error, NSLocalizedDescriptionKey : NSLocalizedStringFromTable(@"Connection to Apple failed. Check the underlying error for more info.", @"RMStore", @"Error description")}];
if (failureBlock != nil)
{
failureBlock(wrapperError);
}
return;
}
NSError *jsonError;
NSDictionary *responseJSON = [NSJSONSerialization JSONObjectWithData:data options:0 error:&jsonError];
if (!responseJSON)
{
failureBlock(wrapperError);
RMStoreLog(@"Failed To Parse Server Response");
if (failureBlock != nil)
{
failureBlock(jsonError);
}
}
return;
}
NSError *jsonError;
NSDictionary *responseJSON = [NSJSONSerialization JSONObjectWithData:data options:0 error:&jsonError];
if (!responseJSON)
{
RMStoreLog(@"Failed To Parse Server Response");
if (failureBlock != nil)

static NSString *statusKey = @"status";
NSInteger statusCode = [responseJSON[statusKey] integerValue];

static NSInteger successCode = 0;
static NSInteger sandboxCode = 21007;
if (statusCode == successCode)
{
failureBlock(jsonError);
if (successBlock != nil)
{
successBlock();
}
}
}

static NSString *statusKey = @"status";
NSInteger statusCode = [responseJSON[statusKey] integerValue];

static NSInteger successCode = 0;
static NSInteger sandboxCode = 21007;
if (statusCode == successCode)
{
if (successBlock != nil)
else if (statusCode == sandboxCode)
{
successBlock();
RMStoreLog(@"Verifying Sandbox Receipt");
// From: https://developer.apple.com/library/ios/#technotes/tn2259/_index.html
// See also: http://stackoverflow.com/questions/9677193/ios-storekit-can-i-detect-when-im-in-the-sandbox
// Always verify your receipt first with the production URL; proceed to verify with the sandbox URL if you receive a 21007 status code. Following this approach ensures that you do not have to switch between URLs while your application is being tested or reviewed in the sandbox or is live in the App Store.

static NSString *sandboxURL = @"https://sandbox.itunes.apple.com/verifyReceipt";
[self verifyRequestData:requestData url:sandboxURL success:successBlock failure:failureBlock];
}
}
else if (statusCode == sandboxCode)
{
RMStoreLog(@"Verifying Sandbox Receipt");
// From: https://developer.apple.com/library/ios/#technotes/tn2259/_index.html
// See also: http://stackoverflow.com/questions/9677193/ios-storekit-can-i-detect-when-im-in-the-sandbox
// Always verify your receipt first with the production URL; proceed to verify with the sandbox URL if you receive a 21007 status code. Following this approach ensures that you do not have to switch between URLs while your application is being tested or reviewed in the sandbox or is live in the App Store.

static NSString *sandboxURL = @"https://sandbox.itunes.apple.com/verifyReceipt";
[self verifyRequestData:requestData url:sandboxURL success:successBlock failure:failureBlock];
}
else
{
RMStoreLog(@"Verification Failed With Code %ld", (long)statusCode);
NSError *serverError = [NSError errorWithDomain:RMStoreErrorDomain code:statusCode userInfo:nil];
if (failureBlock != nil)
else
{
failureBlock(serverError);
RMStoreLog(@"Verification Failed With Code %ld", (long)statusCode);
NSError *serverError = [NSError errorWithDomain:RMStoreErrorDomain code:statusCode userInfo:nil];
if (failureBlock != nil)
{
failureBlock(serverError);
}
}
}
});
});
}];

[task resume];
});
}

Expand Down
16 changes: 15 additions & 1 deletion RMStore/RMStore.m
Original file line number Diff line number Diff line change
Expand Up @@ -229,11 +229,19 @@ - (void)addPayment:(NSString*)productIdentifier
payment.applicationUsername = userIdentifier;
}

#ifdef DEBUG
if (@available(iOS 8.3, *)) {
payment.simulatesAskToBuyInSandbox = YES;
}
#endif

RMAddPaymentParameters *parameters = [[RMAddPaymentParameters alloc] init];
parameters.successBlock = successBlock;
parameters.failureBlock = failureBlock;
_addPaymentParameters[productIdentifier] = parameters;

NSLog(@"Begin store request: %@", NSStringFromClass([payment class]));

[[SKPaymentQueue defaultQueue] addPayment:payment];
}

Expand Down Expand Up @@ -271,6 +279,9 @@ - (void)restoreTransactionsOnSuccess:(void (^)(NSArray *transactions))successBlo
_restoredTransactions = [NSMutableArray array];
_restoreTransactionsSuccessBlock = successBlock;
_restoreTransactionsFailureBlock = failureBlock;

NSLog(@"Begin store request: %@", @"restoreCompletedTransactions");

[[SKPaymentQueue defaultQueue] restoreCompletedTransactions];
}

Expand Down Expand Up @@ -308,6 +319,9 @@ - (void)refreshReceiptOnSuccess:(RMStoreSuccessBlock)successBlock
_refreshReceiptSuccessBlock = successBlock;
_refreshReceiptRequest = [[SKReceiptRefreshRequest alloc] initWithReceiptProperties:@{}];
_refreshReceiptRequest.delegate = self;

NSLog(@"Begin store request: %@", NSStringFromClass([_refreshReceiptRequest class]));

[_refreshReceiptRequest start];
}

Expand Down Expand Up @@ -551,7 +565,7 @@ + (BOOL)hasPendingDownloadsInTransaction:(SKPaymentTransaction*)transaction
- (void)didPurchaseTransaction:(SKPaymentTransaction *)transaction queue:(SKPaymentQueue*)queue
{
RMStoreLog(@"transaction purchased with product %@", transaction.payment.productIdentifier);

if (self.receiptVerifier != nil)
{
[self.receiptVerifier verifyTransaction:transaction success:^{
Expand Down

0 comments on commit 9cc8cf0

Please sign in to comment.