IOS 获取app store相关信息 及提示更新app

//1. 检查更新
-(void)checkVersionUpdata{
    NSString *urlStr     = @"http://itunes.apple.com/lookup?id=983122949";//id替换即可
    NSURL *url           = [NSURL URLWithString:urlStr];
    NSURLRequest *requst = [NSURLRequest requestWithURL:url];
    [NSURLConnection connectionWithRequest:requst delegate:self];
}

//2. 网络连接
-(void)connection:(NSURLConnection *)connection didReceiveData:(nonnull NSData *)data
{
    NSError *error;
    // 在App Store获取的所有信息
    id jsonObject            = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:&error];
    // app相关信息
    NSDictionary *appInfo    = (NSDictionary*)jsonObject;
    NSArray *infoContent     = [appInfo objectForKey:@"results"];
    NSString * version       = [[infoContent objectAtIndex:0]objectForKey:@"version"];//线上最新版本
    // 获取当前版本
    NSString *currentVersion = [self version];//当前用户版本
    BOOL result              = [currentVersion compare:version] == NSOrderedAscending;
    if (result) {//需要更新
        NSLog(@"不是最新版本需要更新");
    NSString *updateStr      =  @"提示更新内容"
        [self creatAlterView:updateStr];
    } else {//已经是最新版;
        NSLog(@"最新版本不需要更新");
    }
}

//3. 弹框提示
-(void)creatAlterView:(NSString *)msg{
    UIAlertController *alertText = [UIAlertController alertControllerWithTitle:@"更新提醒" message:msg preferredStyle:UIAlertControllerStyleAlert];
    //增加按钮
    [alertText addAction:[UIAlertAction actionWithTitle:@"我再想想" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
    }]];
    [alertText addAction:[UIAlertAction actionWithTitle:@"立即更新" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
    NSString *str                = @"itms-apps://itunes.apple.com/cn/app/id1329918420?mt=8"; //更换id即可
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:str]];
    }]];
    [self.window.rootViewController presentViewController:alertText animated:YES completion:nil];
}
//版本
-(NSString *)version
{
    NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
    NSString *app_Version        = [infoDictionary objectForKey:@"CFBundleShortVersionString"];
    return app_Version;
}

App Store 内置评分

 if([SKStoreReviewController respondsToSelector:@selector(requestReview)]) {
        
        //防止键盘遮挡
        
        [[UIApplication sharedApplication].keyWindow endEditing:YES];
        
        [SKStoreReviewController requestReview];
        
    } else {
        
        //不论iOS 版本均可使用APP内部打开网页形式,跳转到App Store 直接编辑评论
        
        NSString *APPID = @"983122949";
        
        NSString *nsStringToOpen = [NSString stringWithFormat: @"itms-apps://itunes.apple.com/app/id%@?action=write-review",APPID];
        
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:nsStringToOpen]];
        
    }

App Store 内跳

    NSString *appId = @"983122949";
    
    // 创建对象
    SKStoreProductViewController *storeVC = [[SKStoreProductViewController alloc] init];
    // 设置代理
    storeVC.delegate = self;
    // 初始化参数
    NSDictionary *dict = [NSDictionary dictionaryWithObject:appId forKey:SKStoreProductParameterITunesItemIdentifier];

    // 跳转App Store页
    [storeVC loadProductWithParameters:dict completionBlock:^(BOOL result, NSError * _Nullable error) {
        if (error) {
            NSLog(@"错误信息:%@",error.userInfo);
        }
        else
        {
            // 弹出模态视图
            [self presentViewController:storeVC animated:YES completion:nil];
        }
    }];

你可能感兴趣的:(IOS 获取app store相关信息 及提示更新app)