68
69

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.

ユーザが古いVerのアプリを使用してた場合、AppStoreに遷移させるアラートを表示する

Posted at

//******を自分のアプリのIDに変更して下さい。
#define APP_ID ******



/**
 * 画面初回表示時の処理
 * 初期化処理
 */
- (void)viewDidLoad {
    [super viewDidLoad];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(checkVersionNotification:) name:@"CheckVersion" object:nil];
    
}



/**
 * バージョン判定
 * ユーザのバージョンが前のバージョンの場合はアラートを表示
 */
- (void)checkVersionNotification:(NSNotification *)notification{
    NSString *url = [NSString stringWithFormat:@"http://itunes.apple.com/lookup?id=%@",APP_ID];
    
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:url]
                             
                                             cachePolicy:NSURLRequestUseProtocolCachePolicy
                                         timeoutInterval:60.0];
    
    NSURLResponse *response;
    NSError *error;
    NSData *data = [NSURLConnection sendSynchronousRequest:request
                                         returningResponse:&response
                                                     error:&error];
    
    
    
    NSDictionary *dataDic  = [NSJSONSerialization JSONObjectWithData:data
                                                             options:NSJSONReadingAllowFragments
                                                               error:&error];
    
    
    NSDictionary *results = [[dataDic objectForKey:@"results"] objectAtIndex:0];
    NSString *latestVersion = [results objectForKey:@"version"];
    NSString *currentVersion = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleShortVersionString"];
    
    if (![currentVersion isEqualToString:latestVersion]) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"お知らせ"
                                                        message:@"最新バージョンが入手可能です。アップデートしますか?"
                                                       delegate:self
                                              cancelButtonTitle:@"キャンセル"
                                              otherButtonTitles:@"アップデート", nil];
        [alert show];
    }
}


/**
 * AlertViewで"アップデート"を選択した際の処理
 * AppStoreに遷移
 */
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    if (buttonIndex == 1) {
        
        NSString* urlString;
        
		//iOSのバージョンでAppStoreに遷移するURLスキームの変更
        if( [self isIOS7]){
			//iOS7以上
            urlString = [NSString stringWithFormat:@"itms-apps://itunes.apple.com/app/id%@",APP_ID];
        }else{
            urlString = [NSString stringWithFormat:@"itms-apps://itunes.apple.com/WebObjects/MZStore.woa/wa/viewContentsUserReviews?id=%@&onlyLatestVersion=true&pageNumber=0&sortOrdering=1&type=Purple+Software",APP_ID];
        }
        
        NSURL* url= [NSURL URLWithString:urlString];
        [[UIApplication sharedApplication] openURL:url];
    }
}

/**
 *  実行中の環境がiOS7以上かどうかを判定する
 *  ios7以上ならTRUEを返す
 */
- (BOOL)isIOS7
{
    NSArray  *aOsVersions = [[[UIDevice currentDevice]systemVersion] componentsSeparatedByString:@"."];
    NSInteger iOsVersionMajor  = [[aOsVersions objectAtIndex:0] intValue];
    return (iOsVersionMajor <= 7);
}

あとは、バージョンを確認したいところに


[[NSNotificationCenter defaultCenter] postNotification:[NSNotification notificationWithName:@"CheckVersion" object:nil]];

を書いておきます。

アプリ起動時に毎回確認したければ、
AppDelegateの

  • (void)applicationDidBecomeActive:(UIApplication *)application
    に書いておくと良いと思います。

参考:
http://yoshiminu.tumblr.com/post/30161327220/iphone

68
69
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
68
69

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?