iOS:Notify user when a new version of your app is available

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.

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

友情链接更多精彩内容