4
1

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.

【iOS13】Scheme 起動時にURLが取得できない!

Posted at

iOS13になってSceneDelegate追加されたこともあり、試行錯誤が続く日々を送っています。
前回に引き続き、仕事中にiOS13でスキーム起動で困ったことをつらつらと書きます。

今までスキーム起動、ブラウザで指定したSchemeを入力した場合、下記のメソッドが呼ばれていました。

// AppDelegate.swift
func application(_ app: UIApplication, open url: URL,  options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool

SceneDelegateの設定を追加した場合、代わりに下記が呼ばれるようになりました。

// SceneDelegate.swift
func scene(_ scene: UIScene, openURLContexts URLContexts: Set<UIOpenURLContext>)

よし、これで安心と思いきや、実はトラップがあって上記メソッドはアプリがバックグラウンドにある場合のみ実行されます。
タスクキルした状態でスキーム起動しても、Xcode上でログやブレークポイントが確認しずらいので解決策を見つけるのに苦労しました。

では、タスクキルした状態でどのようにして判定するかというと、以下のメソッドにヒントがありました。

// SceneDelegate.swift
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions)

このメソッドはアプリ起動時に呼ばれるものですが、第3引数の connectionOptions に着目してみるとプロパティに

open var urlContexts: Set<UIOpenURLContext> { get }

func scene(_:URLContexts:) の第2引数と同じものが!
ということで、タスクキルした状態でスキーム起動したら入っていました!

まさか、バックグラウンドとタスクキル状態からで処理が異なるとは・・・

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?