使用 fir.im 接口实现应用自动更新

本文介绍如何使用 fir.im 提供的接口实现 iOS/Android 应用内新版本检测、自动安装等功能

Mou icon

iOS 检测更新

iOS 的应用通过以下代码可实现在 fir.im 中自动检测更新的功能,有新的应用版本将自动提醒测试人员更新。

[NSURLConnection sendAsynchronousRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://fir.im/api/v2/app/version/53e9c0581019215c1e000009"]] queue:[NSOperationQueue currentQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {

    if (data) {
        @try {
            NSDictionary *result= [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];

            //对比版本
            NSString * version=result[@"version"]; //对应 CFBundleVersion, 对应Xcode项目配置"General"中的 Build
            NSString * versionShort=result[@"versionShort"]; //对应 CFBundleShortVersionString, 对应Xcode项目配置"General"中的 Version
             NSString * localVersion=[[NSBundle mainBundle] infoDictionary][@"CFBundleVersion"];
            NSString * localVersionShort=[[NSBundle mainBundle] infoDictionary][@"CFBundleShortVersionString"];

            NSString *url=result[@"update_url"]; //如果有更新 需要用Safari打开的地址
            NSString *changelog=result[@"changelog"]; //如果有更新 需要用Safari打开的地址




        }
        @catch (NSException *exception) {
            //返回格式错误 忽略掉
        }
    }

}];

Android 检测更新

Android 的应用通过以下代码可实现在 fir.im 中自动检测更新的功能,有新的应用版本将自动提醒测试人员更新。

    String baseUrl = "http://fir.im/api/v2/app/version/%s?token=%s";
   String checkUpdateUrl = String.format(baseUrl, appId, token);
   HttpClient httpClient = new DefaultHttpClient();
   //请求超时
   httpClient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 10000);
   //读取超时
   httpClient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, 20000);
   try {
       HttpGet httpGet = new HttpGet(checkUpdateUrl);
       HttpResponse httpResponse = httpClient.execute(httpGet);
       HttpEntity httpEntity = httpResponse.getEntity();
       int statusCode = httpResponse.getStatusLine().getStatusCode();
       if (statusCode == HttpStatus.SC_OK) {
           String firResponse = EntityUtils.toString(httpEntity);
           JSONObject versionJsonObj = new JSONObject(firResponse);
           //FIR上当前的versionCode
           int firVersionCode = Integer.parseInt(versionJsonObj.getString("version"));
           //FIR上当前的versionName
           String firVersionName = versionJsonObj.getString("versionShort");
           PackageManager pm = context.getPackageManager();
           PackageInfo pi = pm.getPackageInfo(context.getPackageName(),
                   PackageManager.GET_ACTIVITIES);
           if (pi != null) {
               int currentVersionCode = pi.versionCode;
               String currentVersionName = pi.versionName;
               if (firVersionCode > currentVersionCode) {
                   //需要更新
                   Log.i("info","need update");
               } else if (firVersionCode == currentVersionCode) {
                   //如果本地app的versionCode与FIR上的app的versionCode一致,则需要判断versionName.
                   if (!currentVersionName.equals(firVersionName)) {
                      Log.i("info","need update");
                   }
               } else {
                   //不需要更新,当前版本高于FIR上的app版本.
                   Log.i("info"," no need update");
               }
           }
       }
   } catch (UnsupportedEncodingException e) {
       e.printStackTrace();
   } catch (ClientProtocolException e) {
       e.printStackTrace();
   } catch (IOException e) {
       e.printStackTrace();
   } catch (PackageManager.NameNotFoundException e) {
       e.printStackTrace();
   } catch (JSONException e) {
       e.printStackTrace();
   }

ZJ

继续阅读此作者的更多文章

北京