3
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.

OpenURLSchemeでアプリ起動後に画面遷移

Posted at

はじめに

自作アプリにOpenURLSchemeを設定して、他のアプリから起動出来るようにしたのですが、起動後の画面遷移がちょっとややこしかったのと、意外と情報が少なかったのでまとめておきます。

全体像

iOSのアプリはアプリ起動時にいくつかのメソッドがCallされます。Call順は、どのように起動したかによって違いますが、OpenURLのタップにより起動した時は、以下のメソッドが呼ばれます。(全てのメソッドは調べていません)

  1. applicationWillResignActive
  2. applicationWillEnterForeground
  3. application(_ app: ,open url: ,options:)
  4. applicationDidBecomeActive

タップしたOpenURLの情報は3のurlで取得出来るので、アプリ起動後の画面遷移は3か4に実装することになります。
3はアプリ切り替え前、4はアプリ切り替え後になりますので、画面遷移アニメーションをさせるかさせないかで選択するといいでしょう。

実装方法

以下のようなロジックでOpenURLの取得と画面遷移が可能です。アプリへのOpenURLSchemeの設定方法自体はググってみてくださいな。
なお、自作アプリのコードをべたっと貼り付けただけです。

func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool {
	let scheme = url.scheme
	if let host = url.host? {
		if let query = url.query? {
       		self.window = UIWindow(frame: UIScreen.main.bounds)
			let storyBoard = UIStoryboard(name: "Main", bundle: nil)
			if let tabBarViewController = storyBoard.instantiateInitialViewController() as? TabBarViewController {
				if let taskableViewController = tabBarViewController.viewControllers?[0] {
					tabBarViewController.selectedViewController = taskableViewController
					self.window?.rootViewController = tabBarViewController;
					self.window?.makeKeyAndVisible()
				}
			}
        }
    }
    return true
}

「schedulist://task?10」のようなOpenURLをタップすると、schemeに「schedulist」、hostに「task」、queryに「10」が格納されます。
これらの値を使用して画面遷移をして行きましょう。

3
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
3
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?