Created
December 23, 2020 14:24
-
-
Save fumiyasac/29bc119860d131ebc82ea79a4ce76fec to your computer and use it in GitHub Desktop.
UI実装であると嬉しいレシピブック掲載サンプル変更点(iOS14対応箇所)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// UI実装であると嬉しいレシピブック掲載サンプル変更点(iOS14対応箇所) | |
// 1. UINavigationController左上にある戻るボタンをロングタップした際に一気に最初の画面に戻る機能への対応 | |
// 本書の中では、UINavigationControllerExtension.swiftに該当箇所があります。 | |
// 戻るボタンの「戻る」テキストを削除した状態にするメソッド | |
func removeBackButtonText() { | |
self.navigationController!.navigationBar.tintColor = UIColor(code: "#ffffff") | |
if #available(iOS 14.0, *) { | |
self.navigationItem.backButtonDisplayMode = .minimal | |
self.navigationItem.backButtonTitle = self.navigationItem.title | |
} else { | |
let backButtonItem = UIBarButtonItem(title: "", style: .plain, target: nil, action: nil) | |
self.navigationItem.backBarButtonItem = backButtonItem | |
} | |
} | |
// 2. URLを外部ブラウザで開く処理においてSafari以外をデフォルトブラウザに設定している場合にcanOpenURLがfalseとなる点への対応 | |
// 本書の中では、当該処理に関する部分は下記の様な形で実装しています。 | |
private func showWebPage() { | |
if let url = URL(string: "http://www.example.com/") { | |
if UIApplication.shared.canOpenURL(url) { | |
UIApplication.shared.open(url, options: [:]) | |
} else { | |
showAlertWith(completionHandler: nil) | |
} | |
} | |
} | |
// MEMO: iOS14からSafari以外のブラウザをデフォルトに変更することが可能です。 | |
// その場合には「LSApplicationQueriesSchemes」の設定をしないとcanOpenURLでfalseになってしまいます。 | |
// ※ 詳細はInfo.plistを参照 | |
// 確認したSafari以外のブラウザは下記の通りになります。 | |
// 検証ブラウザ: Google Chrome / Opera / Microsoft Edge / Firefox | |
private func showAlertWith(completionHandler: (() -> ())? = nil) { | |
let alert = UIAlertController( | |
title: "リンクを開くことができませんでした。", | |
message: "アプリ内部の設定が誤っている可能性があります。", | |
preferredStyle: .alert | |
) | |
let okAction = UIAlertAction(title: "OK", style: .default, handler: { _ in | |
completionHandler?() | |
}) | |
alert.addAction(okAction) | |
self.present(alert, animated: true, completion: nil) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment