LoginSignup
5
4

More than 3 years have passed since last update.

【iOS】iOS14でcanOpenURLがfalseになる

Posted at

検証環境

Xcode: 12.0.1
iOS: 14.0
Swift: 5

事象

下記のコードでブラウザを外部起動しようとしたら、canOpenURLメソッドがfalseになり、外部起動されなかった。

guard let url: URL = URL(string: "https://www.yahoo.co.jp/") else { return }
if UIApplication.shared.canOpenURL(url) {
    UIApplication.shared.open(url, options: [:], completionHandler: nil)
}

コンソールを見ると下記のようなエラーメッセージが表示された。
httpsのURLスキームは許可されていないとのこと。

-canOpenURL: failed for URL: "https://www.yahoo.co.jp/" - error: "This app is not allowed to query for scheme https"

対処法

エラーメッセージに書いてある通り、httpsは許可されていないので許可してあげれば良い。

canOpenUrlの公式ドキュメントを確認すると、info.plistにLSApplicationQueriesSchemesを追加して、そこに許可するスキームを定義してあげれば良さそう。
(下記引用)

If your app is linked on or after iOS 9.0, you must declare the URL schemes you pass to this method by adding the LSApplicationQueriesSchemes key to your app's Info.plist file. This method always returns false for undeclared schemes, whether or not an appropriate app is installed. To learn more about the key, see LSApplicationQueriesSchemes.

LSApplicationQueriesSchemesのドキュメントにもアプリがcanOpenURL:メソッドを使用してテストできるURLスキームを指定すると書いてある。

実装

下記のように、LSApplicationQueriesSchemesにhttpsのスキームを定義する。
スクリーンショット_2020-09-28_17_33_02.png

この状態で実行すると、無事canOpenURLにtrueが返される。
以上。

5
4
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
5
4