app在程序中設置版本自動更新的步驟


1.通過post方式發送請求:http://itunes.apple.com/lookup?id=你的應用程序的ID

2.從獲得的 response 數據中解析需要的數據。因為從 appstore 查詢得到的信息是 JSON 格式的,所以
需要經過解析。解析之后得到的原始數據就是如下這個樣子的

{  
    resultCount = 1;  
    results =     (  
                {  
            artistId = 開發者 ID;  
            artistName = 開發者名稱;
            price = 0;
            isGameCenterEnabled = 0;  
            kind = software;  
            languageCodesISO2A =             (  
                EN  
            );
            trackCensoredName = 審(我們的)查名稱;  
            trackContentRating = 評級;  
            trackId = 應用程序 ID;
            trackName = 應用程序名稱";  
            trackViewUrl = 應用程序介紹網址;  
            userRatingCount = 用戶評級;  
            userRatingCountForCurrentVersion = 1;  
            version = 版本號;  
            wrapperType = software;
      }  
    );  
}

#pragma direct open itunes  此部分代碼為實現在app直接調用手機的app store應用並導向本app的鏈
接頁面
-(void)openReferralURL:(NSURL *)referralURL{
    NSURLConnection *con = [[NSURLConnection alloc] initWithRequest:[NSURLRequest  
requestWithURL:referralURL] delegate:self startImmediately:YES];
    [con release];
}
-(NSURLRequest *)connection:(NSURLConnection *)connection willSendRequest:(NSURLRequest *)
request redirectResponse:(NSURLResponse *)response{
    self.iTunesURL = [response URL];
    if ([self.iTunesURL.host hasSuffix:@"itunes.apple.com"]) {
        [connection cancel];
        [self connectionDidFinishLoading:connection];
        return nil;
    }else{
        return request;
    }
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection{
    [[UIApplication sharedApplication] openURL:self.iTunesURL];
}
#end pragram

-(void)afterItuensConnect:(ASIHTTPRequest *)request//請求完成之后調用的函數
{
    NSString *responseStr = [request responseString];//NSLog(@"%@",responseStr);
    NSDictionary *dic = [responseStr JSONValue];
    NSArray *infoArrays = [dic objectForKey:@"results"];
    NSDictionary *releaseInfo = [infoArrays objectAtIndex:0];
    self.itunes_url = [releaseInfo objectForKey:@"trackViewUrl"];
    NSString *version =[releaseInfo objectForKey:@"version"];//最新版本號,itunes上的為最新
的,而不是程序中的
    NSString *lastVersion =[[NSBundle mainBundle]  
objectForInfoDictionaryKey:@"CFBundleVersion"];//本次版本的版本號,在info.plist文件中設置的   
Bundle Version字段
   
    NSString *l_Path = [[lastVersion componentsSeparatedByString:@"."] objectAtIndex:0];
    NSString *p_Path = [[version componentsSeparatedByString:@"."] objectAtIndex:0];
    BOOL result = [l_Path compare:p_Path] == NSOrderedAscending;
   
    BOOL error_result = [p_Path compare:l_Path] == NSOrderedAscending;
    if (error_result) {
        return;
    }
   
    if ([lastVersion isEqualToString:version]) {
        //版本號相同,無任何操作
    }
    else if (result) {//第一個版本號大,強制更新
        update_tab = @"closeApp";
        UIAlertView *alerView = [[UIAlertView alloc]initWithTitle:@"" message:
[self.appIdAndAlertStr objectForKey:@"MandatoryUpdateStr"] delegate:self  
cancelButtonTitle:@"No" otherButtonTitles:@"Yes", nil];
       
        [alerView show];
        [alerView release];
    }else{//非強制性更新
        update_tab = @"";
        UIAlertView *alerView = [[UIAlertView alloc]initWithTitle:@"" message:
[self.appIdAndAlertStr objectForKey:@"SelectiveUpdateStr"] delegate:self  
cancelButtonTitle:@"No" otherButtonTitles:@"Yes", nil];
        [alerView show];
        [alerView release];
    }
 
}


-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    if ([alert_lab isEqualToString:@"no_network"]) {
        exit(0);
    }
    if(buttonIndex>0){//click "yes" in update app alert
        // turn to update app web page..
        NSURL *url = [NSURL URLWithString: self.itunes_url];
        [self openReferralURL:url];
      
    }else{//click "no"
        if ([update_tab isEqualToString:@"closeApp"]) {
            exit(0);
        }


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM