使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技术人员的鼎力支持!

Leave a Comment.