47
46

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Swiftで最新バージョンを確認してAppStoreに移動する方法

Last updated at Posted at 2015-08-02

追記

だいぶコードが古いので、今だったらこちらの記事等を参考にされるといいと思います。
https://qiita.com/tommy19970714/items/4826a38917080743fde5

本文

Harpyがうまく使えなかったので今回はHarpyを使わずに実装します。
あとで載せてあるUserUtil.swiftをどっかに入れてください。
そしたら以下の簡単なコードだけで実装ができます。

if util.checkVersion() == Version.needUpdate {
        	util.openAppStore()
}

たとえばSampleViewControllerとかに実装するなら以下のような形になるかと。

SampleViewController.swift
import UIKit

class SampleViewController: UIViewController{
    let     
    override func viewDidLoad() {
        super.viewDidLoad()
        if util.checkVersion() == Version.new {
        	util.openAppStore()
        }
    }
}

ぜひ使ってみてください。

UserUtil.swift
import UIKit

var util = UserUtilLogic()
let APPLE_ID = "920856839" //自分のアプリのIDに変更して下さい。これは英単語アプリmikanのIDです。

enum Version {
    case needUpdate
    case same
    case oldVersion
    case noAppStoreResult
}

class UserUtilLogic: NSObject {
    override init() {
        super.init()
    }
    
    func checkVersion() -> Version {
        let url = "https://itunes.apple.com/jp/lookup?id=\(APPLE_ID)"
        let request = NSURLRequest(URL: NSURL(string: url)!, cachePolicy: NSURLRequestCachePolicy.UseProtocolCachePolicy, timeoutInterval: 60.0)
        var response:NSURLResponse?
        var error:NSErrorPointer
        if let data = NSURLConnection.sendSynchronousRequest(request, returningResponse: nil, error: nil) {
            let dataDic: AnyObject? = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions(), error: nil)
            let resultsArray = dataDic?.objectForKey("results") as! NSArray
            if resultsArray.count > 0 {
                let results = resultsArray[0] as! NSDictionary
                let storeVersion = results.objectForKey("version") as! NSString
                let currentVersion = NSBundle.mainBundle().objectForInfoDictionaryKey("CFBundleShortVersionString") as! String
                println("latest version : \(storeVersion)")                
                if storeVersion.compare(currentVersion, options: NSStringCompareOptions.NumericSearch) == NSComparisonResult.OrderedDescending {
                    println("store version is newer!")
                    return Version.needUpdate
                } else if storeVersion.compare(currentVersion, options: NSStringCompareOptions.NumericSearch) == NSComparisonResult.OrderedSame{
                    println("store version is equal")
                    return Version.same
                } else {
                    println("store version is older")
                    return Version.oldVersion
                }
            }
        }
        return Version.noAppStoreResult
    }

    func openAppStore() {
        let urlString = "itms-apps://itunes.apple.com/app/id\(APPLE_ID)"
        let url = NSURL(string: urlString)
        UIApplication.sharedApplication().openURL(url!)
    }
}

あと、勝手にAppStoreに飛ぶとむかつくので、ぼくは自分で作ったUpdateViewというのをaddSubViewすることにしています。

if util.checkVersion() == Version.needUpdate {
	let updateView = UpdateView(frame: self.view.frame)
    self.view.addSubview(updateView)
}

IMG_7528.jpg

47
46
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
47
46

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?