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];  


  • 相关阅读:
    Python操作MySQL之查看、增删改、自增ID
    MySQL之唯一索引、外键的变种、SQL语句数据行操作补充
    MySQL中出现Unknow column 'xx' in field list的解决办法
    字符集和编码
    Python基础之编译型语言和解释型语言的区别
    Python基础之小整数对象池和大整数对象池
    Python基础之数据类型
    Django实现全文检索
    安全问题-Cookie未设置HttpOnly&&Cookie未设置Secure标识
    基于支付场景下的微服务改造与性能优化
  • 原文地址:https://www.cnblogs.com/cnsec/p/11515897.html
Copyright © 2011-2022 走看看