ios 檢查版本更新

來源:互聯網
上載者:User

標籤:des   style   blog   http   io   color   ar   os   sp   

////  AppCheckVersion.h//  MobileBusiness////  Created by kevin on 14-7-4.////#import <Foundation/Foundation.h>#import "Reachability.h"@class AppCheckVersion;@protocol AppCheckVersionDelegate <NSObject>@optional-(void)checkVersionResult:(AppCheckVersion*)checkVersion isUpdate:(BOOL)update resultData:(NSDictionary*)dictionary;@end@interface AppCheckVersion : NSObject<NSURLConnectionDelegate>{     Class originalClass;    __unsafe_unretained id <AppCheckVersionDelegate> _delegate;}@property (nonatomic, retain) NSURLConnection *connection;@property (nonatomic, retain) NSMutableData  *reciveData;@property (nonatomic, retain) NSURLRequest  *currentRequest;@property (nonatomic, assign) id<AppCheckVersionDelegate> delegate;-(void)checkVersion;-(BOOL)isDelegateRelease;+(void)onCheckVersion:(id<AppCheckVersionDelegate>)target;@end

.m檔案

////  AppCheckVersion.m//  MobileBusiness////  Created by kevin on 14-7-4.////#import "AppCheckVersion.h"#define App_ID  @"886620861" //284417350 應用程式APP ID#define APP_URL @"http://itunes.apple.com/lookup?id=886620861"Class object_getClass(id object);@implementation AppCheckVersion@synthesize connection;@synthesize reciveData;@synthesize currentRequest;@synthesize delegate;+(void)onCheckVersion:(id)target{    AppCheckVersion *app = [[AppCheckVersion alloc] init];    app.delegate = target;    [app checkVersion];}-(BOOL)isDelegateRelease{    if(originalClass == object_getClass(_delegate)){        return YES;    }    return NO;}-(void)cancelConnection{    if(self.connection){        [self.connection cancel];    }    self.connection = nil;    self.currentRequest = nil;    self.reciveData = nil;}-(void)checkVersion{    originalClass = object_getClass(_delegate);    [self cancelConnection];    NetworkStatus networkStatus = [[Reachability reachabilityForInternetConnection] currentReachabilityStatus];    if(networkStatus == NotReachable)    {        //無網路        return;    }    NSURLRequest * requeset = [NSURLRequest requestWithURL:[NSURL URLWithString:APP_URL] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:30];    self.currentRequest = requeset;    NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:self.currentRequest delegate:self];    self.connection = conn;}-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{    self.reciveData = [NSMutableData data];}- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{    [self.reciveData appendData:data];}- (void)connectionDidFinishLoading:(NSURLConnection *)connection{    NSError *error = nil;    NSDictionary *appDictionary = [NSJSONSerialization JSONObjectWithData:self.reciveData options:NSJSONReadingMutableContainers error:&error];    if(error == nil){//        NSLog(@"dataObject ===  %@",appDictionary);        //當前應用版本        NSDictionary *infoDic = [[NSBundle mainBundle] infoDictionary];        NSString *currentVersion = [infoDic objectForKey:@"CFBundleVersion"];        int resultCount = [[appDictionary objectForKey:@"resultCount"] intValue];        if (resultCount)        {            //有版本資訊            NSDictionary *resultDictionary = [[appDictionary objectForKey:@"results"] objectAtIndex:0];            if(resultDictionary)            {                //擷取appstore最新的版本號碼                NSString *version = [resultDictionary objectForKey:@"version"];                //擷取應用程式的地址//                NSString *appURL = [resultDictionary objectForKey:@"trackViewUrl"];                //擷取更新的內容//                NSString *appUpdateContent = [resultDictionary objectForKey:@"description"];                if([self isDelegateRelease])                {                    if(self.delegate && [self.delegate respondsToSelector:@selector(checkVersionResult:isUpdate:resultData:)]){                        if(![currentVersion isEqualToString:version]){                            //版本不同有更新                            [self.delegate checkVersionResult:self isUpdate:YES resultData:resultDictionary];                        }else{                            //已經是最新版本                            [self.delegate checkVersionResult:self isUpdate:NO resultData:nil];                        }                    }                }                           }        }else{            if([self isDelegateRelease])            {                 if(self.delegate && [self.delegate respondsToSelector:@selector(checkVersionResult:isUpdate:resultData:)]){                     //已經是最新版本                     [self.delegate checkVersionResult:self isUpdate:NO resultData:nil];                 }            }        }    }    }-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{    //失敗    [self cancelConnection];}@end

代理對象,一般就是appdelegate

#pragma mark - 檢查版本更新-(void)checkVersionResult:(AppCheckVersion *)checkVersion isUpdate:(BOOL)update resultData:(NSDictionary *)dictionary{    UIAlertView *alterView = nil;    if(update){        //新頒布        //擷取應用程式的地址        appURL = [dictionary objectForKey:@"trackViewUrl"];        alterView = [[UIAlertView alloc] initWithTitle:@"溫馨提示" message:@"軟體已有新版本了,請下載最新版本" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"確定", nil];        [alterView show];    }}-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{    if(buttonIndex==1){        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:appURL]];    }}

 

ios 檢查版本更新

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.