zoukankan      html  css  js  c++  java
  • iOS子线程操作检测版本更新,有新版本通知用户更新, CheckVersion

    iOS子线程操作检测版本更新,有新版本通知用户更新 CheckVersion

    一:如何使用:

    #import "CheckVersion.h"
    
        //输入你的app在appStore的 id
        [CheckVersion check_APP_UPDATE_WITH_APPID:@"350962117"];

    上述代码写完就可以了,当用户打开app检测到新版本时,为通知用户,更新,并显示最新版本的更新内容;

                       

    二:CheckVersion 类

    //
    //  CheckVersion.h
    //  TopProgressView
    //
    //  Created by cocoajin on 14-1-20.
    //  Copyright (c) 2014年 www.zhgu.net. All rights reserved.
    //
    
    #import <Foundation/Foundation.h>
    
    extern NSString const *iTnuesApi;
    
    
    
    @interface CheckVersion : NSObject
    
    //+ (instancetype)check;
    
    + (NSString *)check_LocalApp_Version;
    
    + (void )check_APP_UPDATE_WITH_APPID:(NSString *)appid;
    
    @end
    View Code
    //
    //  CheckVersion.m
    //  TopProgressView
    //
    //  Created by cocoajin on 14-1-20.
    //  Copyright (c) 2014年 www.zhgu.net. All rights reserved.
    //
    
    
    #import "CheckVersion.h"
    
    NSString const *iTnuesApi = @"http://itunes.apple.com/lookup";
    
    #define kTestApp @"http://itunes.apple.com/lookup?id=350962117"  //新浪微博 app测试
    
    
    @implementation CheckVersion
    
    + (instancetype)check
    {
        static CheckVersion *check = nil;
        static dispatch_once_t onceToken;
        dispatch_once(&onceToken, ^{
            check = [[CheckVersion alloc]init];
        });
        
        return check;
    }
    
    + (NSString *)check_LocalApp_Version;
    {
        NSString *localVersion = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"];
        
        return localVersion;
    }
    
    + (void )check_APP_UPDATE_WITH_APPID:(NSString *)appid
    {
        __block id JSON = nil;
        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
            NSError *dataError = nil;
            NSString *appURLAPI = [NSString stringWithFormat:@"%@?id=%@",iTnuesApi,appid];
            NSData *appData = [NSData dataWithContentsOfURL:[NSURL URLWithString:appURLAPI] options:0 error:&dataError];
            if (dataError) {
                //NSLog(@"appStore app版本信息请求错误!请重新尝试");
                [self showAlertWithMessage:@"appStore app版本信息请求错误!请重新尝试"];
                return ;
            }
            JSON = [NSJSONSerialization JSONObjectWithData:appData options:0 error:nil];
            //NSLog(@"ddd : %@",JSON);
            
            if ([[JSON objectForKey:@"resultCount"] intValue] > 0) {
                NSString *remoteVersion = [[[JSON objectForKey:@"results"] objectAtIndex:0] objectForKey:@"version"];
                NSString *releaseNotes = [[[JSON objectForKey:@"results"] objectAtIndex:0] objectForKey:@"releaseNotes"];
                NSString *trackURL = [[[JSON objectForKey:@"results"] objectAtIndex:0] objectForKey:@"trackViewUrl"];
                [[NSUserDefaults standardUserDefaults] setObject:trackURL forKey:@"KK_THE_APP_UPDATE_URL"];
                //NSLog(@"%@ %@ %@",remoteVersion,releaseNotes,trackURL);
                
                NSString *localVersion = [self check_LocalApp_Version];
                
                if ([remoteVersion floatValue] > [localVersion floatValue]) {
                    [[CheckVersion check] newVersionUpdate:remoteVersion notes:releaseNotes];
                }
                else
                {
                    return;
                }
                
            }
            else
            {
                //NSLog(@"appStore 无app信息,请检查您的 app id");
                [self showAlertWithMessage:@"appStore 无此app信息,请检查您的 app id"];
                return ;
            }
            
            
        });
        
    }
    
    
    
    + (void)showAlertWithMessage:(NSString *)messages
    {
        dispatch_async(dispatch_get_main_queue(), ^{
            UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"版本更新提示" message:messages delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
            [alert show];
            
    #if !__has_feature(objc_arc)
            [alert release];
    #endif
        });
        
    }
    
    - (void)newVersionUpdate:(NSString *)version notes:(NSString *)releaseNotes
    {
        dispatch_async(dispatch_get_main_queue(), ^{
            UIAlertView *alert = [[UIAlertView alloc]initWithTitle:[NSString stringWithFormat:@"新版本 %@",version] message:releaseNotes delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"更新", nil];
            [alert show];
            
    #if !__has_feature(objc_arc)
            [alert release];
    #endif
        });
    }
    
    - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
    {
        if (buttonIndex==1) {
            //NSString *apiUrl = @"https://itunes.apple.com/us/app/wei-bo/id350962117?mt=8&uo=4";
            //apiUrl = @"itms-apps://itunes.apple.com/cn/app/wei-bo/id350962117?mt=8";
            NSString *theAppURL = [[NSUserDefaults standardUserDefaults] objectForKey:@"KK_THE_APP_UPDATE_URL"];
            NSURL *appStoreURL = [NSURL URLWithString:theAppURL];
            [[UIApplication sharedApplication] openURL:appStoreURL];
        }
    }
    @end
    View Code
  • 相关阅读:
    九度oj 题目1525:子串逆序打印
    九度oj 题目1516:调整数组顺序使奇数位于偶数前面
    九度oj 题目1490:字符串链接
    九度oj 题目1438:最小公倍数
    九度oj 题目1181:遍历链表
    九度oj 题目1179:阶乘
    九度oj 题目1077:最大序列和
    九度oj 题目1075:斐波那契数列
    九度oj 题目1074:对称平方数
    九度oj 题目1073:杨辉三角形
  • 原文地址:https://www.cnblogs.com/cocoajin/p/3527979.html
Copyright © 2011-2022 走看看