71
73

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

iOSアプリのアップデート通知メソッド

Posted at

AppStoreから最新バージョンを検索してユーザーに通知するメソッドを作ってみました。
ユーザーに使ってもらうアプリを常に最新版にして欲しい場合などに、使えるかと思います。

UpdateChecker.h
#import <Foundation/Foundation.h>

@interface UpdateChecker : NSObject

+ (void)checkApplicationNewVersionWithAppStoreId:(NSString *)storeId foundBlock:(void (^)(NSURL *url, NSString *version))block;

@end
UpdateChecker.m
#import "UpdateChecker.h"

#define kAppStoreAPIURL @"http://ax.itunes.apple.com/WebObjects/MZStoreServices.woa/wa/wsLookup?id=%@&country=jp"
#define kAppStoreLink @"https://itunes.apple.com/app/id%@"

@implementation UpdateChecker

+ (void)checkApplicationNewVersionWithAppStoreId:(NSString *)storeId foundBlock:(void (^)(NSURL *, NSString *))block
{
    NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:kAppStoreAPIURL, storeId]];

    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    NSOperationQueue *queue = [[NSOperationQueue alloc] init];

    [NSURLConnection sendAsynchronousRequest:request
                                       queue:queue
                           completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
                               if ([data length] > 0 && !connectionError) {
                                   dispatch_async(dispatch_get_main_queue(), ^{
                                       NSDictionary *appData = [NSJSONSerialization JSONObjectWithData:data
                                                                                               options:NSJSONReadingAllowFragments
                                                                                                 error:nil];
                                       NSArray *versionsInAppStore = [appData valueForKeyPath:@"results.version"];
                                       if ([versionsInAppStore count]) {
                                           NSString *appStoreVersion = versionsInAppStore[0];
                                           NSString *currentVersion = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleShortVersionString"];
                                           if ([currentVersion compare:appStoreVersion options:NSNumericSearch] == NSOrderedAscending) {
                                               //最新バージョンが見つかった
                                               if (block) {
                                                   NSURL *storeURL = [NSURL URLWithString:[NSString stringWithFormat:kAppStoreLink, storeId]];
                                                   block(storeURL, appStoreVersion);
                                               }
                                           } else {
                                               //最新バージョンが見つからなかった
                                               return;
                                           }
                                       }
                                   });
                               }
                           }];
}

@end
71
73
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
71
73

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?