推荐一个iOS拉取App Store里面的app自动更新的第三方
推荐一个iOS拉取App Store里面的app自动更新的第三方库,挺好用的
Git地址:https://github.com/wolfhous/HSUpdateApp
====================================================================
//
// HSUpdateApp.h
// HSUpdateAppDemo
//
// Created by 侯帅 on 2017/5/8.
// Copyright © 2017年 com.houshuai. All rights reserved.
//
// Git地址:https://github.com/wolfhous/HSUpdateApp
// 没有技术难点,只是希望为您节约更多的时间,去陪恋人、家人和朋友。祝您大吉大利、今晚吃鸡!
#import
@interface HSUpdateApp : NSObject
/**
block回调
@param currentVersion 当前版本号
@param storeVersion 商店版本号
@param openUrl 跳转到商店的地址
@param isUpdate 是否为最新版本
*/
typedefvoid(^UpdateBlock)(NSString*currentVersion,NSString*storeVersion,NSString*openUrl,BOOLisUpdate);
/**
一行代码实现检测app是否为最新版本。appId,bundelId,随便传一个 或者都传nil 即可实现检测。
@param appId 项目APPID,10位数字,有值默认为APPID检测,可传nil
@param bundelId项目bundelId,有值默认为bundelId检测,可传nil
@param block 检测结果block回调
*/
+(void)hs_updateWithAPPID:(NSString*)appIdwithBundleId:(NSString*)bundelIdblock:(UpdateBlock)block;
@end
====================================================================
//
// HSUpdateApp.m
// HSUpdateAppDemo
//
// Created by 侯帅 on 2017/5/8.
// Copyright © 2017年 com.houshuai. All rights reserved.
//
#import "HSUpdateApp.h"
@implementation HSUpdateApp
+(void)hs_updateWithAPPID:(NSString*)appIdwithBundleId:(NSString*)bundelIdblock:(UpdateBlock)block{
NSDictionary *infoDic=[[NSBundle mainBundle] infoDictionary];
__blockNSString*currentVersion=infoDic[@"CFBundleShortVersionString"];
NSURLRequest*request;
if(appId !=nil) {
request = [NSURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://itunes.apple.com/lookup?id=%@",appId]]];
NSLog(@"【1】当前为APPID检测,您设置的APPID为:%@ 当前版本号为:%@",appId,currentVersion);
}elseif(bundelId !=nil){
request = [NSURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://itunes.apple.com/lookup?bundleId=%@",bundelId]]];
NSLog(@"【1】当前为BundelId检测,您设置的bundelId为:%@ 当前版本号为:%@",bundelId,currentVersion);
}else{
NSString*currentBundelId=infoDic[@"CFBundleIdentifier"];
request = [NSURLRequestrequestWithURL:[NSURLURLWithString:[NSStringstringWithFormat:@"http://itunes.apple.com/lookup?bundleId=%@",currentBundelId]]];
NSLog(@"【1】当前为自动检测检测, 当前版本号为:%@",currentVersion);
}
NSURLSession *session = [NSURLSession sharedSession];
NSLog(@"【2】开始检测...");
NSURLSessionDataTask*task = [sessiondataTaskWithRequest:requestcompletionHandler:^(NSData*_Nullabledata,NSURLResponse*_Nullableresponse,NSError*_Nullableerror) {
if(error) {
NSLog(@"【3】检测失败,原因:\n%@",error);
dispatch_async(dispatch_get_main_queue(), ^{
block(currentVersion,@"",@"",NO);
});
return;
}
dispatch_async(dispatch_get_main_queue(), ^{
NSDictionary*appInfoDic = [NSJSONSerializationJSONObjectWithData:dataoptions:NSJSONReadingMutableLeaveserror:nil];
if([appInfoDic[@"resultCount"]integerValue] ==0) {
NSLog(@"检测出未上架的APP或者查询不到");
block(currentVersion,@"",@"",NO);
return;
}
NSLog(@"【3】苹果服务器返回的检测结果:\n appId = %@ \n bundleId = %@ \n 开发账号名字 = %@ \n 商店版本号 = %@ \n 应用名称 = %@ \n 打开连接 = %@",appInfoDic[@"results"][0][@"artistId"],appInfoDic[@"results"][0][@"bundleId"],appInfoDic[@"results"][0][@"artistName"],appInfoDic[@"results"][0][@"version"],appInfoDic[@"results"][0][@"trackName"],appInfoDic[@"results"][0][@"trackViewUrl"]);
NSString*appStoreVersion = appInfoDic[@"results"][0][@"version"];
NSString*currentVersion2 = currentVersion;
if(currentVersion.length
{
NSIntegercount =appStoreVersion.length-currentVersion.length;
for(inti=0; i
{
currentVersion = [currentVersionstringByAppendingString:@"0"];
}
}
else
{
NSIntegercount =currentVersion.length-appStoreVersion.length;
for(inti=0; i
{
appStoreVersion = [appStoreVersionstringByAppendingString:@"0"];
}
}
currentVersion = [currentVersionstringByReplacingOccurrencesOfString:@"."withString:@""];
if(currentVersion.length==2) {
currentVersion = [currentVersionstringByAppendingString:@"0"];
}elseif(currentVersion.length==1){
currentVersion = [currentVersionstringByAppendingString:@"00"];
}
appStoreVersion = [appStoreVersionstringByReplacingOccurrencesOfString:@"."withString:@""];
if(appStoreVersion.length==2) {
appStoreVersion = [appStoreVersionstringByAppendingString:@"0"];
}elseif(appStoreVersion.length==1){
appStoreVersion = [appStoreVersionstringByAppendingString:@"00"];
}
if([currentVersionfloatValue] < [appStoreVersionfloatValue])
{
NSLog(@"【4】判断结果:当前版本号%@ < 商店版本号%@ 需要更新\n=========我是分割线========",[[NSBundle mainBundle] infoDictionary][@"CFBundleShortVersionString"],appInfoDic[@"results"][0][@"version"]);
block(currentVersion2,appInfoDic[@"results"][0][@"version"],appInfoDic[@"results"][0][@"trackViewUrl"],YES);
}else{
NSLog(@"【4】判断结果:当前版本号%@ > 商店版本号%@ 不需要更新\n========我是分割线========",[[NSBundle mainBundle] infoDictionary][@"CFBundleShortVersionString"],appInfoDic[@"results"][0][@"version"]);
block(currentVersion2,appInfoDic[@"results"][0][@"version"],appInfoDic[@"results"][0][@"trackViewUrl"],NO);
}
});
}];
[taskresume];
}
@end