LoginSignup
7
6

More than 3 years have passed since last update.

iOSアプリの起動方法を判別する

Posted at

概要

アプリがホーム画面から起動されたのか、スキーム起動されたのかなど起動時の状態をアプリで判別する方法

活用ケース

  • ホーム画面からの起動の場合のみ何か処理を行う
  • アプリの起動方法を厳密に判定して処理を行う

実装

AppDelegatedidFinishLaunchingWithOptionsの引数であるlaunchOptionsにて判別を行います。
ホーム画面からアプリを起動した際にはnilとなっています。

func application(_: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    // launchOptionsで起動方法を判別する
}

See: https://developer.apple.com/documentation/uikit/uiapplicationdelegate/1622921-application
See: https://developer.apple.com/documentation/uikit/uiapplication/launchoptionskey

Sample

ホーム画面から起動した場合のlaunchOptions

nil

Safariからスキーム起動した場合のlaunchOptions

[__C.UIApplicationLaunchOptionsKey(_rawValue: UIApplicationLaunchOptionsURLKey): hoge://fuga?a=b
 __C.UIApplicationLaunchOptionsKey(_rawValue: UIApplicationLaunchOptionsSourceApplicationKey): com.apple.mobilesafari]

補足

起動時の振る舞いとなるため、デバッグにはConsoleを用いる必要があります。
下記を参考ください。
https://qiita.com/m_orishi/items/c5593878e1ab55d5c3f2

さいごに

ニーズがあるように感じたのですが、意外とまとまった記事がなく。
もっと良い判定方法があるよ!この方法だとこんな問題があるよ!など知見がありましたら教えて頂ければ嬉しいです。

7
6
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
7
6