利用fir.im接口实现应用的自动检测更新和安装

闲话少说直接切入正题,本文章简述通过fir.im接口直接在app内做到版本更新的提示以及迭代!本次为大家分别提供swift版和OC版本
一、我们先获得自身app的版本号代码如下:

//OC
    NSString *localVersion = [[[NSBundle mainBundle]infoDictionary] objectForKey:@"CFBundleVersion"];
    self.LocalVersion = localVersion;
//swift
   let localVersion = Bundle.main.object(forInfoDictionaryKey: "CFBundleVersion")

二、获得上传到fir.im上app的版本号API如下:

参考文档:https://fir.im/docs/version_detection

 curl http://api.fir.im/apps/latest/xxx?api_token=xxx #使用 `id` 请求

通过解析version获得fir上的版本号

self.NewVersion = model.version

注:latest/xxx是您app的bundleId api_token在fir.im账号上生成
三、那么我们来通过比较版本的大小作判断

但是版本号一般类似于1.1.1与1.1.2,这样转换解析后来比较是比较麻烦的,给大家提供一个好的建议,降序比较:

//OC
if ([self.NewVersion compare:self.LocalVersion] == NSOrderedDescending) {
}
//swift
 ifself.NewVersion?.compare(self.LocalVersion!) == ComparisonResult.orderedDescending{
        }

四、获得download_token

curl "http://api.fir.im/apps/:id/download_token?api_token=xxxxx"

我们可以直接用浏览器打开获得download_token

五、第二步 安装应用 ( iOS )

在应用中, 直接openURL以下地址即可弹出系统安装提示:

itms-services://?action=download-manifest&url=https://download.fir.im/apps/:id/install?download_token=xxxxxx

不过 url 后面的链接地址需要 URLEncode转义方法如下:而且这个需要手机自带的Safari才能打开,因为只有手机自带的Safari才会识别itms-services://?action=download-manifest&url=的拼接方式

//OC
NSString * urlString = @"https://download.fir.im/apps/:id/install?download_token=xxxxxx";
              NSString *URLencodeString = CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault, (CFStringRef)urlString, NULL, CFSTR(":/?#[]@!$ &'()*+,;=\"<>%{}|\\^~`"), CFStringConvertNSStringEncodingToEncoding(NSUTF8StringEncoding)));
              NSString *installURL = [NSString stringWithFormat:@"itms-services://?action=download-manifest&url=%@", URLencodeString];
              NSURL *openURL = [NSURL URLWithString:installURL];
              [[UIApplication sharedApplication] openURL:openURL];
//swift
 let originalString = "https://download.fir.im/apps/:id/install?download_token=xxxxxx"
 let customAllowedSet = CharacterSet(charactersIn: ":/?#[]@!$ &'()*+,;=\"<>%{}|\\^~`").inverted
 let escapedString = originalString.addingPercentEncoding(withAllowedCharacters: customAllowedSet)!
 let installURL: String = "itms-services://?action=download-manifest&url=\(String(describing: escapedString))"
 let openURL = URL(string:installURL)
                    UIApplication.shared.openURL(openURL!)

好了,基本介绍就这么多,我们可以根据自己的需要部署需要展现的alert,如果需要实时展示的话我们可以放在 AppDelegate 中调用

//oc
- (void)applicationWillEnterForeground:(UIApplication *)application {
ZyzAuxiliary * aulia = [[ZyzAuxiliary alloc]init];
[aulia checkVersion];
}
//swift  
func applicationWillEnterForeground(_ application: UIApplication) {
      let aulia = ZyzAuxiliary()
        aulia.checkVersion()
    }

在此也特别鸣谢fir.im技术人员的大力支持!

你可能感兴趣的:(利用fir.im接口实现应用的自动检测更新和安装)