iOS開發應用程式更新

來源:互聯網
上載者:User

標籤:

#import "ViewController.h"//1一定要先配置自己項目在商店的APPID,配置完最好在真機上運行才能看到完全效果哦#define STOREAPPID @"1080182980"@interface ViewController ()@end@implementation ViewController- (void)viewDidLoad {    [super viewDidLoad];    //一句代碼實現檢測更新    [self hsUpdateApp];}/** *  天朝專用檢測app更新 */-(void)hsUpdateApp{    //2先擷取當前工程項目版本號碼    NSDictionary *infoDic=[[NSBundle mainBundle] infoDictionary];    NSString *currentVersion=infoDic[@"CFBundleShortVersionString"];        //3從網路擷取appStore版本號碼    NSError *error;    NSData *response = [NSURLConnection sendSynchronousRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://itunes.apple.com/cn/lookup?id=%@",STOREAPPID]]] returningResponse:nil error:nil];    if (response == nil) {        NSLog(@"你沒有串連網路哦");        return;    }    NSDictionary *appInfoDic = [NSJSONSerialization JSONObjectWithData:response options:NSJSONReadingMutableLeaves error:&error];    if (error) {        NSLog(@"hsUpdateAppError:%@",error);        return;    }    NSArray *array = appInfoDic[@"results"];    NSDictionary *dic = array[0];    NSString *appStoreVersion = dic[@"version"];    //列印版本號碼    NSLog(@"目前的版本號:%@\n商店版本號碼:%@",currentVersion,appStoreVersion);    //4目前的版本號小於商店版本號碼,就更新    if([currentVersion floatValue] < [appStoreVersion floatValue])    {        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"版本有更新" message:[NSString stringWithFormat:@"檢測到新版本(%@),是否更新?",appStoreVersion] delegate:self cancelButtonTitle:@"取消"otherButtonTitles:@"更新",nil];        [alert show];    }else{        NSLog(@"版本號碼好像比商店大噢!檢測到不需要更新");    }}- (void)alertView:(UIAlertView*)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{    //5實現跳轉到市集進行更新    if(buttonIndex==1)    {        //6此處加入應用在app store的地址,方便使用者去更新,一種實現方式如下:        NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"https://itunes.apple.com/us/app/id%@?ls=1&mt=8", STOREAPPID]];        [[UIApplication sharedApplication] openURL:url];    }}@end

 

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.