Skip to content

Commit

Permalink
Merge pull request Purus#17 from w4-jeongwookpark/master
Browse files Browse the repository at this point in the history
Fetch app id using bundle id when app id is not given
  • Loading branch information
Purus authored Nov 27, 2019
2 parents 6f43a21 + 2aaa606 commit 8f2648f
Showing 1 changed file with 38 additions and 1 deletion.
39 changes: 38 additions & 1 deletion ios/Classes/LaunchReviewPlugin.m
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ + (void)registerWithRegistrar:(NSObject<FlutterPluginRegistrar>*)registrar {

- (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result {
if ([@"launch" isEqualToString:call.method]) {
NSString *appId = call.arguments[@"ios_id"];
NSString *appId = call.arguments[@"ios_id"] ? [self fetchAppIdFromBundleId] : @"";

if (appId == (NSString *)[NSNull null]) {
result([FlutterError errorWithCode:@"ERROR"
Expand Down Expand Up @@ -58,4 +58,41 @@ - (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result {
}
}

- (NSString *)fetchAppIdFromBundleId
{
NSString* bundleId = [NSBundle mainBundle].bundleIdentifier;
NSString* iTunesServiceURL = [NSString stringWithFormat:@"http://itunes.apple.com/lookup?bundleId=%@", bundleId];

NSString* errorMsg = nil;
NSNumber* appStoreId = nil;
NSError *error = nil;
NSURLResponse *response = nil;
NSURL *url = [NSURL URLWithString:iTunesServiceURL];
NSURLRequest *request = [NSURLRequest requestWithURL:url];

NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
NSDictionary *jsonData = [NSJSONSerialization JSONObjectWithData:data options: NSJSONReadingMutableContainers error: &error];
NSInteger resultCount = [jsonData[@"resultCount"] intValue];
NSInteger statusCode = ((NSHTTPURLResponse *)response).statusCode;

if (resultCount > 0 && statusCode == 200){
if (!error){
appStoreId = jsonData[@"results"][0][@"trackId"];
}else{
errorMsg = [error localizedDescription];
}
}else if (statusCode >= 400){
//http error
errorMsg = [NSString stringWithFormat:@"The server returned a %@ error", @(statusCode)];
}else{
errorMsg = @"The application could not be found on the App Store.";
}

if(errorMsg != nil){
NSLog(@"%@", errorMsg);
}

return [appStoreId stringValue];
}

@end

0 comments on commit 8f2648f

Please sign in to comment.