美文网首页
iOS:Notify user when a new versi

iOS:Notify user when a new versi

作者: 不是谢志伟 | 来源:发表于2014-10-15 11:17 被阅读74次

    When you have a new version of your app available in the AppStore, and you want to notify your user to update the app. It's very simple to do this.

    First, we need to get the current version of your app. You can easily get the string by the following code:

      NSDictionary *infoDic = [[NSBundle mainBundle] infoDictionary];
      NSString *appVersion = [infoDic objectForKey:@"CFBundleVersion"];
    

    Next , what about the newest version string of your app in the AppStore ? Apple provide a Search API which we can get our app information.

    Here is how we get the newest version string of the app:

        NSString *URL = @"http://itunes.apple.com/lookup?id=921110xxx";//'id' is the apple id of your app in iTunes Connect
        
        NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
        [request setURL:[NSURL URLWithString:URL]];
        [request setHTTPMethod:@"POST"];
    
        NSError *error = nil;
        NSOperationQueue *queue = [[NSOperationQueue alloc] init];
        [NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
            if (data.length && !error) {
                 NSString *currentVersion = [self getCurrentVersion];
        
        id json = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
          if ([json isKindOfClass:[NSDictionary class]]) {
              NSArray *infoArr = [json objectForKey:@"results"];
              if (infoArr.count) {
                  NSDictionary *releaseInfo = [infoArr objectAtIndex:0];
                  NSString *newestVersion = [releaseInfo objectForKey:@"version"];
                  if (newestVersion.length && ![currentVersion isEqualToString:newestVersion]) {
                  _trackViewURL = [releaseInfo objectForKey:@"trackViewUrl"];
    
                  }
              }
          }
            }
        }];
    

    Finally, it's time to show an alert to user and guide them to the AppStore page.

    //1.show update alert view
    
    //2. in UIAlertViewDelegate method, we go to the AppStore page.
            UIApplication *application = [UIApplication sharedApplication];
            [application openURL:[NSURL URLWithString:_trackViewURL]];
    

    You can check app version in appDelegate didFinishLaunchingWithOptions: method.

    相关文章

      网友评论

          本文标题:iOS:Notify user when a new versi

          本文链接:https://www.haomeiwen.com/subject/wxsptttx.html