1. 程式人生 > >版本更新迭代

版本更新迭代

鄭重說明這是自己的筆記,方便後期自己檢視

原來專案中提示版本更新是用友盟做的,後來友盟(蘋果)的後臺不支援了,所以需要自己來做。當然前期我們自己做的版本更新,後來想了想,因為iOS取到只有一個( 企業版的也不多)。所以我就百度了一下,然後用蘋果給咱們的rest介面做了檢測更新。

原理:拿到蘋果伺服器我們產品資訊(包含版本號version和buile 還有產品的所有資訊)跟當前安裝的產品的版本進行比較。

步驟:
1.取到蘋果伺服器我們產品的資訊的地址。
注意:下面的這個APP_URL地址中有個/cn 因為我的只在國內銷售。如果像是“微信”等應用的話就是@”http://itunes.apple.com/lookup?id=

微信的appid”(aped怎麼找啊,有好多辦法,可以在你填寫資訊的後臺自己看 apple ID隨機生成碼就是)

2、根據上面的地址請求需要的資訊

<code class="hljs objectivec has-numbering"><span class="hljs-comment">//#define APP_URL @"http://itunes.apple.com/cn/lookup?id=1093039842"</span>

 <span class="hljs-comment">//檢測更新</span>
    AFHTTPSessionManager *mgr = [AFHTTPSessionManager manager];
    [mgr POST:APP_URL parameters:<span class="hljs-literal">nil</span> progress:<span class="hljs-literal">nil</span> success:^(NSURLSessionDataTask * _Nonnull task, <span class="hljs-keyword">id</span>  _Nullable responseObject) {
        <span class="hljs-built_in">NSLog</span>(@<span class="hljs-string">"%@"</span>,responseObject);
        <span class="hljs-comment">/*responseObject是個字典{},有兩個key

        KEYresultCount = 1//表示搜到一個符合你要求的APP
        results =()//這是個只有一個元素的陣列,裡面都是app資訊,那一個元素就是一個字典。裡面有各種key。其中有 trackName (名稱)trackViewUrl = (下載地址)version (可顯示的版本號)等等
        */</span>

    <span class="hljs-comment">//具體實現為</span>
    <span class="hljs-built_in">NSArray</span> *arr = [responseObject objectForKey:@<span class="hljs-string">"results"</span>];
        <span class="hljs-built_in">NSDictionary</span> *dic = [arr firstObject];
        <span class="hljs-built_in">NSString</span> *versionStr = [dic objectForKey:@<span class="hljs-string">"version"</span>];
        <span class="hljs-built_in">NSString</span> *trackViewUrl = [dic objectForKey:@<span class="hljs-string">"trackViewUrl"</span>];
        <span class="hljs-built_in">NSString</span> *releaseNotes = [dic objectForKey:@<span class="hljs-string">"releaseNotes"</span>];<span class="hljs-comment">//更新日誌</span>


<span class="hljs-comment">//NSString* buile = [[NSBundle mainBundle] objectForInfoDictionaryKey: (NSString*) kCFBundleVersionKey];build號 </span>
        <span class="hljs-built_in">NSString</span>* thisVersion = [[<span class="hljs-built_in">NSBundle</span> mainBundle] objectForInfoDictionaryKey:@<span class="hljs-string">"CFBundleShortVersionString"</span>];

        <span class="hljs-keyword">if</span> ([<span class="hljs-keyword">self</span> compareVersionsFormAppStore:versionStr AppVersion:thisVersion]) {
            UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:[<span class="hljs-built_in">NSString</span> stringWithFormat:@<span class="hljs-string">"發現新版本:%@"</span>,versionStr] message:releaseNotes preferredStyle:UIAlertControllerStyleAlert];
            UIAlertAction *cancelAction  = [UIAlertAction actionWithTitle:@<span class="hljs-string">"取消"</span> style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
                <span class="hljs-built_in">NSLog</span>(@<span class="hljs-string">"點選了取消"</span>);
            }];

            UIAlertAction *OKAction  = [UIAlertAction actionWithTitle:@<span class="hljs-string">"更新"</span> style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                <span class="hljs-built_in">NSLog</span>(@<span class="hljs-string">"點選了知道了"</span>);
                <span class="hljs-built_in">NSURL</span> * url = [<span class="hljs-built_in">NSURL</span> URLWithString:trackViewUrl];<span class="hljs-comment">//itunesURL = trackViewUrl的內容</span>
                [[<span class="hljs-built_in">UIApplication</span> sharedApplication] openURL:url];
            }];
            [alertVC addAction:cancelAction];
            [alertVC addAction:OKAction];
            [<span class="hljs-keyword">self</span> presentViewController:alertVC animated:<span class="hljs-literal">YES</span> completion:<span class="hljs-literal">nil</span>];
        }

 } failure:^(NSURLSessionDataTask * _Nullable task, <span class="hljs-built_in">NSError</span> * _Nonnull error) {
        <span class="hljs-built_in">NSLog</span>(@<span class="hljs-string">""</span>);

    }];</code>
<code class="hljs objectivec has-numbering"><span class="hljs-comment">//比較版本的方法,在這裡我用的是Version來比較的</span>
- (<span class="hljs-built_in">BOOL</span>)compareVersionsFormAppStore:(<span class="hljs-built_in">NSString</span>*)AppStoreVersion WithAppVersion:(<span class="hljs-built_in">NSString</span>*)AppVersion{

    <span class="hljs-built_in">BOOL</span> littleSunResult = <span class="hljs-literal">false</span>;

    <span class="hljs-built_in">NSMutableArray</span>* a = (<span class="hljs-built_in">NSMutableArray</span>*) [AppStoreVersion componentsSeparatedByString: @<span class="hljs-string">"."</span>];
    <span class="hljs-built_in">NSMutableArray</span>* b = (<span class="hljs-built_in">NSMutableArray</span>*) [AppVersion componentsSeparatedByString: @<span class="hljs-string">"."</span>];

    <span class="hljs-keyword">while</span> (a<span class="hljs-variable">.count</span> < b<span class="hljs-variable">.count</span>) { [a addObject: @<span class="hljs-string">"0"</span>]; }
    <span class="hljs-keyword">while</span> (b<span class="hljs-variable">.count</span> < a<span class="hljs-variable">.count</span>) { [b addObject: @<span class="hljs-string">"0"</span>]; }

    <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> j = <span class="hljs-number">0</span>; j<a<span class="hljs-variable">.count</span>; j++) {
        <span class="hljs-keyword">if</span> ([[a objectAtIndex:j] integerValue] > [[b objectAtIndex:j] integerValue]) {
            littleSunResult = <span class="hljs-literal">true</span>;
            <span class="hljs-keyword">break</span>;
        }<span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span>([[a objectAtIndex:j] integerValue] < [[b objectAtIndex:j] integerValue]){
            littleSunResult = <span class="hljs-literal">false</span>;
            <span class="hljs-keyword">break</span>;
        }<span class="hljs-keyword">else</span>{
            littleSunResult = <span class="hljs-literal">false</span>;
        }
    }
    <span class="hljs-keyword">return</span> littleSunResult;<span class="hljs-comment">//true就是有新版本,false就是沒有新版本</span>

}</code>

3、總結(注意點):1>我用的是AFN 3.1.0版本(相容IP6)所以用的是AFHTTPSessionManager物件; 2>UIAlertView棄用了,說實話我工程裡用的都是它,因為當時沒棄用。現在用的就是這個。見怪不怪,就是更好用了。