zoukankan      html  css  js  c++  java
  • 在代码中调用本机AppStore 程序 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);
            }
        }
    }
     
    还可以这样,只要服务的传回app的ID即可

    通过Modal view方式弹出App Store商品详情页面。我按照文档说明,写了个例子。部分代码如下:

    - (void)openAppWithIdentifier:(NSString *)appId {  
        SKStoreProductViewController *storeProductVC = [[SKStoreProductViewController alloc] init];  
        storeProductVC.delegate = self;  
          
        NSDictionary *dict = [NSDictionary dictionaryWithObject:appId forKey:SKStoreProductParameterITunesItemIdentifier];  
        [storeProductVC loadProductWithParameters:dict completionBlock:^(BOOL result, NSError *error) {  
            if (result) {  
                [self presentViewController:storeProductVC animated:YES completion:nil];  
            }  
        }];  
    }  
     
    另外,需要实现SKStoreProductViewControllerDelegate如下代理方法:
    [java] 
    #pragma mark - SKStoreProductViewControllerDelegate  
    - (void)productViewControllerDidFinish:(SKStoreProductViewController *)viewController {  
        [viewController dismissViewControllerAnimated:YES completion:^{  
            [viewController release];  
        }];  
    }  
     
     
    可以这样调用:
    [java]  
    [self openAppWithIdentifier:@"383037733"];  
     
    这段代码即实现了上面图示的效果。
    注:项目需要添加StoreKit框架,仅在iOS 6.0以上的设备中支持上述实现。
     
    [java]  
    Framework     
    /System/Library/Frameworks/StoreKit.framework  
    Availability      
    Available in iOS 6.0 and later.  
     
    如果需要兼容6.0以下的设备,可以使用下面的代码(这种方式会跳出当前应用):
     
    [java] 
    - (void)outerOpenAppWithIdentifier:(NSString *)appId {  
        NSString *urlStr = [NSString stringWithFormat:@"itms-apps://itunes.apple.com/us/app/id%@?mt=8", appId];  //这个地址是固定的
        NSURL *url = [NSURL URLWithString:urlStr];  
        [[UIApplication sharedApplication] openURL:url];  


  • 相关阅读:
    5:《地牢守卫者》代码分析:EnemyController
    4:《地牢守卫者》代码分析:Enemy
    1:《地牢守卫者》代码解析PlayerController
    0:地牢守卫者开发框架
    7:《地牢守卫者》代码分析:DarkElf,DrakElfController
    6:《地牢守卫者》代码分析:GoblinPawn,GoblinController
    ABAP学习(1):基本语法介绍
    项目实例:车辆信息统计报表管理系统导出PDF怎么调样式?
    软件制作:模拟登陆之WebBrowser
    编程经验:邮件发送接收
  • 原文地址:https://www.cnblogs.com/cnsec/p/11515897.html
Copyright © 2011-2022 走看看