iOS 的更新提醒教程

小毅哥哥發表於2017-09-26

1.為 APPDelegate新增 一個 VersonUpdate 分類

2. 在.m 檔案中實現方法

//網路請求app的資訊
-(void)VersonUpdate{
    //定義的app的地址
    NSString *urld = [NSString stringWithFormat:@"http://itunes.apple.com/lookup?id=%@",APPIDForThisApp];

    //網路請求app的資訊,主要是取得我說需要的Version
    NSURL *url = [NSURL URLWithString:urld];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url
                                                           cachePolicy:NSURLRequestReloadIgnoringCacheData
                                                       timeoutInterval:10];
    [request setHTTPMethod:@"POST"];

    NSURLSession *session = [NSURLSession sharedSession];

    NSURLSessionDataTask *task = [session dataTaskWithURL:url completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        NSMutableDictionary *receiveStatusDic=[[NSMutableDictionary alloc]init];
        if (data) {

            //data是有關於App所有的資訊
            NSDictionary *receiveDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
            if ([[receiveDic valueForKey:@"resultCount"] intValue]>0) {

                [receiveStatusDic setValue:@"1" forKey:@"status"];
                [receiveStatusDic setValue:[[[receiveDic valueForKey:@"results"] objectAtIndex:0] valueForKey:@"version"]   forKey:@"version"];

                //請求的有資料,進行版本比較
                [self performSelectorOnMainThread:@selector(receiveData:) withObject:receiveStatusDic waitUntilDone:NO];
            }else{

                [receiveStatusDic setValue:@"-1" forKey:@"status"];
            }
        }else{
            [receiveStatusDic setValue:@"-1" forKey:@"status"];
        }
    }];

    [task resume];
}


//獲取自身的版本號並與AppStore比較
-(void)receiveData:(id)sender
{
    //獲取APP自身版本號
    NSString *localVersion = [[[NSBundle mainBundle]infoDictionary]objectForKey:@"CFBundleShortVersionString"];

    NSArray *localArray = [localVersion componentsSeparatedByString:@"."];
    NSArray *versionArray = [sender[@"version"] componentsSeparatedByString:@"."];


    if ((versionArray.count == 3) && (localArray.count == versionArray.count)) {

        if ([localArray[0] intValue] <  [versionArray[0] intValue]) {
            [self updateVersion];
        }else if ([localArray[0] intValue]  ==  [versionArray[0] intValue]){
            if ([localArray[1] intValue] <  [versionArray[1] intValue]) {
                [self updateVersion];
            }else if ([localArray[1] intValue] ==  [versionArray[1] intValue]){
                if ([localArray[2] intValue] <  [versionArray[2] intValue]) {
                    [self updateVersion];
                }
            }
        }
    }
}
//根據比較的結果,實現彈窗
-(void)updateVersion{
    NSString *msg = [NSString stringWithFormat:@"更新最新版本,優惠資訊提前知"];
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"升級提示" message:msg preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction * cancleAction = [UIAlertAction actionWithTitle:@"取消"style:UIAlertActionStyleCancel handler:^(UIAlertAction*action) {

    }];
    UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"現在升級"style:UIAlertActionStyleDestructive handler:^(UIAlertAction*action) {

        NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"itms://itunes.apple.com/us/app/ 開發者名字(一定要英文)/idAPPID?l=zh&ls=1&mt=8"]];
        [[UIApplication sharedApplication]openURL:url];
    }];
    [alertController addAction:cancleAction];
    [alertController addAction:okAction];
    [self.window.rootViewController presentViewController:alertController animated:YES completion:nil];

}

3. 在.h 檔案中暴露介面

//網路請求app的資訊
-(void)VersonUpdate;

4. 在 AppDelegate 的呼叫

-application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions  方法中呼叫




獲取自身的版本號

這裡寫圖片描述

appStore的版本號

這裡寫圖片描述

/**/. 跳轉 App Store 沒有反應

這裡寫圖片描述

替換中文:
iOS 的更新提醒教程
這裡寫圖片描述

相關文章