どうも、フリーランスのITエンジニア兼ブロガー兼投資家のKerubitoです。
アプリ開発者なら必ず通る更新通知。
こういうやつですね。
アプリを運用・保守していくには必須の機能になるかと。
Swiftで自作してみました。
ライブラリもいくつかあるんですが、そんなに大したことをしない割にはサイズが大きかったり、不具合の報告が上がっていたり。
昔は私もライブラリを使っていたんですが、自作のほうが融通もききます。
では、ぱぱっとやってみましょう。
環境は以下の通りです。
・Swiftバージョン:5
・Xcodeバージョン:12.4
AppStoreに最新バージョンがあれば通知する
通知するメイン処理です。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | Class UpgradeNotice { internal static let shared = UpgradeNotice() private init() {} private let apple_id = "***" internal func fire() { guard let url = URL(string: "https://itunes.apple.com/jp/lookup?id=\(apple_id)") else { return } let request = URLRequest(url: url, cachePolicy: .useProtocolCachePolicy, timeoutInterval: 60) let task = URLSession.shared.dataTask(with: request, completionHandler: { (data, response, error) in guard let data = data else { return } do { let jsonData = try JSONSerialization.jsonObject(with: data) as? [String: Any] guard let storeVersion = ((jsonData?["results"] as? [Any])?.first as? [String : Any])?["version"] as? String, let appVersion = Bundle.main.object(forInfoDictionaryKey: "CFBundleShortVersionString") as? String else { return } switch storeVersion.compare(appVersion, options: .numeric) { case .orderedDescending: DispatchQueue.main.async { self.showAlert() } return case .orderedSame, .orderedAscending: return } }catch { } }) task.resume() } } |
AppStoreの最新バージョンを取得
↓
アプリのバージョンを取得
↓
両者を比較し、AppStoreに最新バージョンがあれば、メッセージを表示する
といった流れです。
AppStoreの最新バージョンを取得するにはHTTP通信を行います。
URLは「https://itunes.apple.com/jp/lookup?id=」で、アプリのIDを指定します。
アプリのIDはiTunesConnectのアプリの情報から調べられます。
アプリのバージョンはBundle.main.object(forInfoDictionaryKey: "CFBundleShortVersionString")で取得できます。
アラートを表示する
アラートを表示します。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | private func showAlert() { guard let parent = topViewController() else { return } let actionA = UIAlertAction(title: "更新", style: .default, handler: { (action: UIAlertAction!) in if let url = URL(string: "itms-apps://itunes.apple.com/app/id\(self.apple_id)"), UIApplication.shared.canOpenURL(url) { UIApplication.shared.open(url, options: [:], completionHandler: nil) } }) let actionB = UIAlertAction(title: "あとで", style: .default, handler: { (action: UIAlertAction!) in }) let alert: UIAlertController = UIAlertController(title: "最新バージョンのお知らせ", message: "最新バージョンがあります。", preferredStyle: .alert) alert.addAction(actionA) alert.addAction(actionB) parent.present(alert, animated: true, completion: nil) } private func topViewController() -> UIViewController? { var vc = UIApplication.shared.keyWindow?.rootViewController while vc?.presentedViewController != nil { vc = vc?.presentedViewController } return vc } |
「更新」、「あとで」という選択肢を用意。
「更新」をタップするとAppStoreへと遷移します。
AppStoreのURLは「itms-apps://itunes.apple.com/app/id」で最後に上と同じく、アプリのIDを付与します。
呼び出してみる
呼び出し部分です。
SwiftUIで実装しており、ContentView.swiftの画面描画時に表示してみました。
1 2 3 4 5 6 7 8 9 | struct ContentView: View { var body: some View { Text("Hello, world!") .padding() .onAppear { UpgradeNotice.shared.fire() } } } |
UpgradeNotice.shared.fire()で呼び出しているだけです。
実行すると、こんな画面が出てきます。
カスタマイズし放題
最低限の機能はこれでいいかと。
ただ、実際には「次のバージョンまでスキップさせたい」や「メッセージの表示は1日1回」などカスタマイズが必要になってくるでしょう。
自作ならいくらでもできます(笑
あと、エラーハンドリングなんもしてなかったり、コードも汚いのでいい感じにして使ってください。
ちょっと雑に作ってしまいましたが、GitHubからソースをダウンロードできます。
https://github.com/Kerubito0/UpgradeNotice
以上、楽しい開発ライフを!