Appstore Update

 //appstore update option
    
    [self needsUpdate:^(NSDictionary *dictionary) {
        // you can use the dictionary here
        
        NSDictionary* infoDictionary = [[NSBundle mainBundle] infoDictionary];
        
        if ([dictionary[@"resultCount"] integerValue] == 1){
            NSString* appStoreVersion = dictionary[@"results"][0][@"version"];
            NSString* currentVersion = infoDictionary[@"CFBundleShortVersionString"];
            if (![appStoreVersion isEqualToString:currentVersion]){
                NSLog(@"Need to update [%@ != %@]", appStoreVersion, currentVersion);
                [self showAlert];
            }
            
            
        }
        
        // if you want to update UI or model, dispatch this to the main queue:
        // dispatch_async(dispatch_get_main_queue(), {
        // do your UI stuff here
        //    do nothing
        // });

    }];



//appstore update
-(void)needsUpdate:(void (^)(NSDictionary * dictionary))completionHandler{
    
    NSDictionary* infoDictionary = [[NSBundle mainBundle] infoDictionary];
    NSString* appID = infoDictionary[@"CFBundleIdentifier"];
    NSURL* url = [NSURL URLWithString:[NSString stringWithFormat:@"http://itunes.apple.com/lookup?bundleId=%@", appID]];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    
    NSURLSession *session = [NSURLSession sharedSession];
    NSURLSessionDataTask *task = [session dataTaskWithRequest:request
                                            completionHandler:
                                  ^(NSData *data, NSURLResponse *response, NSError *error) {
                                      
                                      NSDictionary* lookup = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
                                      
                                      if (completionHandler) {
                                          completionHandler(lookup);
                                      }
                                      
                                      
                                      
                                  }];
    
    [task resume];
    
    
    
}

-(void)showAlert
{
    UIAlertController *alertController = [UIAlertController  alertControllerWithTitle:@"please update app"  message:nil  preferredStyle:UIAlertControllerStyleAlert];
    [alertController addAction:[UIAlertAction actionWithTitle:@"Okay!" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action)
            {
             
                NSURL *url = [NSURL URLWithString:@"https://itunes.apple.com/in/mt=8"];
                [[UIApplication sharedApplication] openURL:url];
             }]];
    [alertController addAction:[UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action)
                                {
                                 
                                }]];
    UIWindow* topWindow = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    topWindow.rootViewController = [UIViewController new];
    topWindow.windowLevel = UIWindowLevelAlert + 1;
    [topWindow makeKeyAndVisible];
    [topWindow.rootViewController presentViewController:alertController animated:YES completion:nil];

    
}
Previous
Next Post »

1 comments:

Write comments
Unknown
AUTHOR
13 October 2017 at 05:27 delete This comment has been removed by the author.
avatar