Objective-C
iPhone
iOS

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

More than 5 years have passed since last update.

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