일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |
- async/await
- Static Dispatch
- Universal Link
- @NameSpace
- ios
- SFSafariView
- await
- matchedGeometryEffect
- wwdc23
- Dynamic Dispatch
- SwiftData
- The Composable Architecture
- Custom URL Scheme
- AnyCancellable
- @ObservedObject
- async
- MVVM
- combine
- Combine vs Async/Await
- fileprivate
- SwiftUI
- App Thinning
- ScrollViewProxy
- swift
- @main
- ScrollViewReader
- 이것이나의다정입니다
- architecture
- Concurrency Programming
- @StateObject
- Today
- Total
홍로그
DeepLink 본문
📖DeepLink란?
딥링크(Deep Link)는 모바일 앱에서 특정 화면이나 기능으로 사용자를 바로 연결하게 해주는 링크 기술입니다. iOS에서 딥링크는 Universal Links와 Custom URL Scheme이라는 방식으로 구현됩니다.
Universal Links
Universal Links는 iOS 9 이상에서 사용하는, 앱과 웹 사이트 간의 연결을 간소화한 딥링크 방식입니다. Universal Links는 웹사이트와 앱의 특정 화면을 연결하고, 앱이 설치되어 있다면 사이트의 URL을 클릭하면 바로 해당 앱의 특정 화면으로 이동합니다. 앱이 없다면 기본 웹 페이지로 연결합니다.
이 기능을 활성화하려면, 앱 개발자는 웹서버에 apple-app-site-association 파일을 업로드하고, 앱의 프로젝트 내에서 일치하는 Associated Domains을 구성해야 합니다.
Custom URL Scheme
Custom URL Scheme은 앱 간의 연결에 사용되는 딥링크입니다. 이를 사용하면 특정 기능이나 앱으로 바로 연결할 수 있습니다.
"myphotoapp://"와 같은 URL Scheme이 등록되어 있다고 가정해 보면 이 URL Scheme을 사용하여 Deeplink를 생성할 수 있습니다. 예를 들어, "myphotoapp://profile"은 "myphotoapp"이라는 앱으로 이동하고, "profile"이라는 특정 화면을 열라는 의미를 가지게 됩니다.
예시코드
import UIKit
@main
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// 다른 설정들...
// Deeplink 처리
if let url = launchOptions?[.url] as? URL {
handleDeeplink(url)
}
return true
}
// Deeplink 처리 메서드
func handleDeeplink(_ url: URL) {
if url.scheme == "myapp" {
if url.host == "profile" {
// "profile" 화면으로 이동하는 코드를 여기에 작성
// 예: navigationController?.pushViewController(ProfileViewController(), animated: true)
}
}
}
// 다른 AppDelegate 메서드들...
}
import UIKit
class SceneDelegate: UIResponder, UIWindowSceneDelegate {
var window: UIWindow?
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
// 다른 설정들...
// Deeplink 처리
if let urlContext = connectionOptions.urlContexts.first {
handleDeeplink(urlContext.url)
}
}
// Deeplink 처리 메서드
func handleDeeplink(_ url: URL) {
if url.scheme == "myphotoapp" {
if url.host == "profile" {
// "profile" 화면으로 이동하는 코드를 여기에 작성
// 예: navigationController?.pushViewController(ProfileViewController(), animated: true)
}
}
}
// 다른 SceneDelegate 메서드들...
}
위의 코드에서 Deeplink를 처리하는 메서드handleDeeplink(_:) 안에는 원하는 화면으로 이동하는 코드를 추가해야 합니다. 이 예시에서는 ProfileViewController를 예시로 들었지만, 실제 앱에 맞게 필요한 화면으로 이동하는 코드를 작성해야 합니다. 또한, 앱이 백그라운드에서 실행 중일 때 Deeplink를 처리해야 하는 경우에도 AppDelegate.swift 파일의 application(_:continue:restorationHandler:) 메서드와 SceneDelegate.swift 파일의 scene(_:continue:)] 메서드를 수정하여 Deeplink를 처리할 수 있습니다. 이 경우에도 Deeplink를 처리하는 코드를 해당 메서드 안에 추가해주어야 합니다.
SwiftUI 예시코드
import SwiftUI
@main
struct MyApp: App {
@Environment(\.scenePhase) var scenePhase
var body: some Scene {
WindowGroup {
ContentView()
.onOpenURL(perform: handleDeeplink)
}
.onChange(of: scenePhase) { phase in
if phase == .active {
if let url = UIApplication.shared.windows.first?.windowScene?.session.stateRestorationActivity?.userInfo?[UIApplication.stateRestorationLaunchOptionsURLKey] as? URL {
handleDeeplink(url)
}
}
}
}
func handleDeeplink(_ url: URL) {
if url.scheme == "myphotoapp" {
if url.host == "profile" {
// "profile" 화면으로 이동하는 코드를 여기에 작성
}
}
}
}
struct ContentView: View {
var body: some View {
NavigationView {
VStack {
Text("Home Screen")
NavigationLink(destination: ProfileView()) {
Text("Go to Profile")
}
}
}
}
}
struct ProfileView: View {
var body: some View {
Text("Profile Screen")
}
}
위의 코드에서 MyApp 구조체는 App 프로토콜을 구현하고 있습니다. ContentView에는 "Go to Profile" 버튼이 있으며, 버튼을 클릭하면 ProfileView로 이동합니다. MyApp 구조체에서는 onOpenURL(perform:) modifier를 사용하여 Deeplink URL을 처리하고, 해당 화면으로 이동하는 로직을 추가합니다.
또한, scenePhase 환경변수를 사용하여 앱의 생명주기 이벤트를 관찰하고, 앱이 활성 상태로 전환될 때 저장된 Deeplink URL을 처리합니다. 이는 앱이 백그라운드에서 실행 중일 때 Deeplink를 처리해야 하는 경우에 유용합니다.
참조
https://developer.apple.com/documentation/xcode/supporting-associated-domains
Supporting associated domains | Apple Developer Documentation
Connect your app and a website to provide both a native app and a browser experience.
developer.apple.com
https://developer.apple.com/documentation/xcode/defining-a-custom-url-scheme-for-your-app
Defining a custom URL scheme for your app | Apple Developer Documentation
Use specially formatted URLs to link to content within your app.
developer.apple.com
'iOS' 카테고리의 다른 글
TCA (0) | 2023.08.03 |
---|---|
Concurrency Programming (0) | 2023.06.22 |
Combine AnyCancellable (0) | 2023.06.19 |
WWDC23 SwiftData (0) | 2023.06.14 |
SwiftUI @NameSpace (0) | 2023.06.13 |