利用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技術人員的大力支持!

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容