๋ฐ์ํ
1. URL scheme ์ง์ ํ๊ธฐ
2. ๋ฅ๋งํฌ ์ง์ ํ๊ธฐ
1. URL scheme ์ง์ ํ๊ธฐ
- URL Scheme์ url์ด ์์๋๊ธฐ ์ url ์์ ๋ถ๋ ํ๋ฆฌํฝ์ค ์คํธ๋ง (์๋ฅผ ๋ค์ด https:// ๊ฐ ์์)
- ์ฑ์ด ์ค์น๋์ง ์์ ๊ฒฝ์ฐ ์ฌ์ฉํ ์ ์์
- url์ด์ง๋ง ์ฌํ๋ฆฌ์์ ๋์ํ๋ ๊ฒ์ด ์๋๋ผ ๋ด ์ฑ์ ์ง์ ๋ ๋์์ ์ํํ๋๋ฐ ์ฌ์ฉํจ
(1) ์ฑ์์ URL Scheme ๋ฑ๋กํ๊ธฐ
- Info.plist ํ์ผ์ URL scheme ๋ฑ๋ก
- Info.plist > URL Types ์น์
- Identifier์ ์ฌ์ฉ์ ์ ์ ์คํด์ ์ ๋ ฅํด ์ค
- Identifier๋ ๋ฒ๋ค id์ ๋์ผ
(2) Schene delegate ๋ฉ์๋ ์ ์ํ๊ธฐ
- ์ฑ์ ๋ฅ๋งํฌ ์ฒ๋ฆฌ ์์ฒญํ๊ธฐ
- ์ฝ๋๋ ๋ฒจ์์ ํด๋น ์ฝ๋ ๋์ ์ํค๋ฉด ๋จ
- ํน์ ์ฌํ๋ฆฌ์ ์ฃผ์์ฐฝ์์ ์ง์ ์คํด์ ํฌํจํ URL์ ๊ฒ์
let url = URL(string: "์ฌ์ฉ์์ง์ ์คํด://path/")!
UIApplication.shared.open(url, options: [:], completionHandler: nil)
- ์ฑ์ด ๋ฅ๋งํฌ๋ฅผ ์ฒ๋ฆฌํ๋ผ๋ ์์ฒญ์ ๋ฐ์ผ๋ฉด ์๋ ํจ์๋ฅผ ํธ์ถํจ
- SceneDelegate๋ฅผ ์ฌ์ฉํ๋ ๊ฒฝ์ฐ scene(_:openURLContexts:)
- AppDelegate๋ฅผ ์ฌ์ฉํ๋ ๊ฒฝ์ฐ application(_:open:options:)
- ์ผ๋ฐ URL์ธ ๊ฒฝ์ฐ ์ฌํ๋ฆฌ๋ก ํด๋น ๋งํฌ๋ฅผ ์ด๊ณ , ์ฌ์ฉ์ ์ง์ ์คํด์ธ ๊ฒฝ์ฐ ์ฑ์๊ฒ ๋ฅ ๋งํฌ ์ฒ๋ฆฌ ์์ฒญ
func scene(_ scene: UIScene, openURLContexts URLContexts: Set<UIOpenURLContext>) {
guard let url = URLContexts.first?.url else { return }
let scheme = url.scheme
let host: String?
if #available(iOS 16.0, *) {
host = url.host()
} else {
host = url.host
}
let path: String
if #available(iOS 16.0, *) {
path = url.path()
} else {
path = url.path
}
let components = url.pathComponents
}
์ Set<>์ผ๋ก URLContexts๊ฐ ์ค๋์ง ์ด์ ๋ชป ์ฐพ์- path์ host ํ๋กํผํฐ๋ deprecate ์์ ์ด๋ผ (์ฐ๋๋ฐ ๋ฌธ์ ๋ ์์ ์์ง๊น์ง๋.) Percent encoding ์ต์ ์ด ํฌํจ๋ ๋ฉ์๋๋ฅผ ์ฌ์ฉํด path์ host๋ฅผ ์ป์ ์ ์์. ์ด๋ iOS 16 ๋ถํฐ ์ง์
- pathComponents๋ path๋ฅผ /๋ก ๊ตฌ๋ถํ์ฌ String ๋ฐฐ์ด๋ก ๋ฐํํจ. ์ธ๋ฑ์ค 0์ / ์ด๋ค (["/", "test", "10"] ๋ ๋ฒ์งธ, ์ธ๋ฑ์ค๋ก๋ 1๋ฒ ๋ถํฐ ์ฌ์ฉ)
2. ๋ฅ๋งํฌ ์ง์ ํ๊ธฐ
func scene(_ scene: UIScene, willContinueUserActivityWithType userActivityType: String) {
guard userActivity?.activityType == NSUserActivityTypeBrowsingWeb,
let urlToOpen = userActivity?.webpageURL else {
return
}
handleURL(urlToOpen)
}
func scene(_ scene: UIScene, openURLContexts URLContexts: Set<UIOpenURLContext>) {
guard let url = URLContexts.first?.url else { return }
handleURL(url)
}
func handleURL(_ url: URL) {
guard url.scheme == "mycustomscheme" else { return }
let host: String?
if #available(iOS 16.0, *) {
host = url.host()
} else {
host = url.host
}
let components = url.pathComponents
switch host {
....
}
}
- switch๋ฅผ ์ฌ์ฉํด์ host์ ๊ฐ์ ๋ฐ๋ผ ๋ก์ง์ ์ํํ๋๋ก ํ ์ ์์. host๊ฐ ๊ฐ๊ณ path๋ก ๊ตฌ๋ถํด์ผ ํ๋ ๊ฒฝ์ฐ url.pathComponents๋ก ์ป์ ๊ฐ์ ์ฌ์ฉํ ์ ์์
- ์ฑ์ด ์ด๋ฏธ ์คํ๋ ๊ฒฝ์ฐ window์ rootViewController์ ํ๋ฉด์ ํธ์
var router = AppCoordinator().strongRouter
guard let navigationController = router.viewController as? UINavigationController else { return }
- xCoordinator์ ์ฌ์ฉํ ๊ฒฝ์ฐ router์ viewController๋ก ํ์ธํ ์ ์์
guard let navigationController = window?.rootViewController as? UINavigationController else { return }
์ฐธ๊ณ ์ฌ์ดํธ
728x90
๋ฐ์ํ
'๐ iOS' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[iOS/Swift] UIViewControllerAnimatedTransitioning ์ ๋ฆฌ (0) | 2023.01.29 |
---|---|
[Pod/iOS] Pod ๋ผ์ด๋ธ๋ฌ๋ฆฌ ์์ฑํ๊ณ ๊ด๋ฆฌํ๊ธฐ (0) | 2023.01.29 |
[SwiftUI] Sticky Header ์คํฌ๋กค์ ๊ณ ์ ์ํค๊ธฐ (0) | 2022.12.17 |
[SwiftUI] Model, ViewModel ์์ฑ (0) | 2022.12.17 |
[SwiftUI] Service Layer to ViewModel with Combine (1) | 2022.11.21 |
๋๊ธ