|
| 1 | +// |
| 2 | +// RNAppStoreReview.m |
| 3 | +// |
| 4 | +// Created by Jérémy Magrin on 24/04/2017. |
| 5 | +// Copyright © 2017 Jérémy Magrin. All rights reserved. |
| 6 | +// |
| 7 | + |
| 8 | +#import "RNAppStoreReview.h" |
| 9 | + |
| 10 | +#import <Foundation/Foundation.h> |
| 11 | + |
| 12 | +#define SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending) |
| 13 | + |
| 14 | +@implementation RNAppStoreReview |
| 15 | + |
| 16 | +RCT_EXPORT_MODULE(); |
| 17 | + |
| 18 | +- (dispatch_queue_t) methodQueue |
| 19 | +{ |
| 20 | + return dispatch_get_main_queue(); |
| 21 | +} |
| 22 | + |
| 23 | +- (UIViewController*) getRootVC { |
| 24 | + UIViewController *root = [[[[UIApplication sharedApplication] delegate] window] rootViewController]; |
| 25 | + while (root.presentedViewController != nil) { |
| 26 | + root = root.presentedViewController; |
| 27 | + } |
| 28 | + |
| 29 | + return root; |
| 30 | +} |
| 31 | + |
| 32 | +- (void) openStoreProductWithiTunesItemIdentifierWithinApp:(NSString *) appIdentifier { |
| 33 | + UIViewController *root = [self getRootVC]; |
| 34 | + |
| 35 | + SKStoreProductViewController *storeViewController = [[SKStoreProductViewController alloc] init]; |
| 36 | + storeViewController.delegate = self; |
| 37 | + |
| 38 | + NSDictionary *parameters = @{ SKStoreProductParameterITunesItemIdentifier: appIdentifier }; |
| 39 | + |
| 40 | + [storeViewController loadProductWithParameters:parameters completionBlock:^(BOOL result, NSError *error) { |
| 41 | + if (result) { |
| 42 | + [root presentViewController:storeViewController animated:YES completion:nil]; |
| 43 | + } else { |
| 44 | + NSLog(@"ERROR WITH STORE CONTROLLER %@\n", error.description); |
| 45 | + //redirect to app store |
| 46 | + [self openStoreProductWithiTunesItemIdentifier:appIdentifier]; |
| 47 | + } |
| 48 | + }]; |
| 49 | +} |
| 50 | + |
| 51 | +- (void) openStoreProductWithiTunesItemIdentifier:(NSString *) appIdentifier { |
| 52 | + NSString *strUrl = [NSString stringWithFormat:@"%@%@%@", @"itms-apps://itunes.apple.com/app/id", appIdentifier, @"?action=write-review"]; |
| 53 | + NSURL *url = [NSURL URLWithString: strUrl]; |
| 54 | + |
| 55 | + if ([[UIApplication sharedApplication] canOpenURL:url]) { |
| 56 | + [[UIApplication sharedApplication] openURL:url]; |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +- (void) productViewControllerDidFinish:(SKStoreProductViewController*)viewController { |
| 61 | + [viewController dismissViewControllerAnimated:YES completion:nil]; |
| 62 | +} |
| 63 | + |
| 64 | +RCT_EXPORT_METHOD(requestReview:(NSString *) appIdentifier) |
| 65 | +{ |
| 66 | + if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"10.3")) { |
| 67 | + [SKStoreReviewController requestReview]; |
| 68 | + } else if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"8.0")) { |
| 69 | + [self openStoreProductWithiTunesItemIdentifierWithinApp:appIdentifier]; |
| 70 | + } else { |
| 71 | + [self openStoreProductWithiTunesItemIdentifier:appIdentifier]; |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +@end |
0 commit comments