딥링크 (URL Scheme, Universal Link)
betterhee
2022. 11. 21. 10:51
딥링크는 앱이 실행되거나 앱 내 특정 화면으로 이동시키는 기능을 수행하는 링크입니다.
딥링크는 2가지 방식으로 구분됩니다.
- URL Scheme 방식 : 앱에 URL Sscheme 값을 등록하는 방식
- Universal Link 방식 : 도메인 주소를 이용하는 방식
While custom URL schemes are an acceptable form of deep linking, universal links are strongly recommended. For more information on universal links, see Allowing apps and websites to link to your content.
Apple 공식문서에서는 custom URL Scheme 방식보다는 Universal Link 방식을 강력히 권장합니다.
URI 스킴 방식
클라이언트 앱
이 서버 앱
을 호출한다고 가정합니다.
1. 서버 앱
의 URL 형식을 정의합니다.
// ex) 사진 라이브러리 앱의 경우 표시할 앨범의 name이나 index를 포함하는 URL 형식을 정의합니다.
myphotoapp:albumname?name="albumname"
myphotoapp:albumname?index=1
2. 서버 앱
에서 URL 스키마를 등록합니다.
- [Target] - [Info] - [URL Types]
identifier
은 고유성을 보장하기 위해 (도메인+앱 이름)을 역방향으로 작성한 DNS 문자열을 사용하는 것을 권장합니다.
3. 클라이언트 앱
에서 서버 앱
의 URL을 호출합니다.
let url = URL(string: "myphotoapp:Vacation?index=1")
UIApplication.shared.open(url!) { (result) in
if result {
// The URL was delivered successfully!
}
}
4. 서버 앱
에서 수신한 URL을 처리합니다.
// Scene-based App
func scene(_ scene: UIScene,
willConnectTo session: UISceneSession,
options connectionOptions: UIScene.ConnectionOptions) {
// Determine who sent the URL.
if let urlContext = connectionOptions.urlContexts.first {
let sendingAppID = urlContext.options.sourceApplication
let url = urlContext.url
print("source application = \(sendingAppID ?? "Unknown")")
print("url = \(url)")
// Process the URL.
guard let components = NSURLComponents(url: url, resolvingAgainstBaseURL: true),
let albumPath = components.path,
let params = components.queryItems else {
print("Invalid URL or album path missing")
return false
}
if let photoIndex = params.first(where: { $0.name == "index" })?.value {
print("albumPath = \(albumPath)")
print("photoIndex = \(photoIndex)")
return true
} else {
print("Photo index missing")
return false
}
}
}
- 앱이 메모리에 실행되고 있지 않은 경우, 시스템은 앱을 실행한 후 scene(_:willConnectTo:options:) 메서드를 호출하여 URL을 전달합니다.
- 메모리에서 실행 중이거나 정지된 상태일 경우, scene(_:openURLContexts:) 메서드에 URL를 호출하여 전달합니다.
유니버셜 링크 방식
유니버셜 링크를 이용하면 앱이 설치되어 있는 경우 앱으로 이동하고, 없으면 앱을 설치할 수 있는 앱스토어로 이동합니다.
1. 서버 앱
에 유니버셜 링크를 지원하기 위해 도메인을 추가합니다.
2. 클라이언트 앱
에서 서버 앱
의 유니버셜 링크를 호출합니다.
if let appURL = URL(string: "https://myphotoapp.example.com/albums?albumname=vacation&index=1") {
UIApplication.shared.open(appURL) { success in
if success {
print("The URL was delivered successfully.")
} else {
print("The URL failed to open.")
}
}
} else {
print("Invalid URL specified.")
}
3. 서버 앱
에서 수신한 유니버셜 링크를 처리합니다.
func scene(_ scene: UIScene, willConnectTo
session: UISceneSession,
options connectionOptions: UIScene.ConnectionOptions) {
// Get URL components from the incoming user activity.
guard let userActivity = connectionOptions.userActivities.first,
userActivity.activityType == NSUserActivityTypeBrowsingWeb,
let incomingURL = userActivity.webpageURL,
let components = NSURLComponents(url: incomingURL, resolvingAgainstBaseURL: true) else {
return
}
// Check for specific URL components that you need.
guard let path = components.path,
let params = components.queryItems else {
return
}
print("path = \(path)")
if let albumName = params.first(where: { $0.name == "albumname" })?.value,
let photoIndex = params.first(where: { $0.name == "index" })?.value {
print("album = \(albumName)")
print("photoIndex = \(photoIndex)")
} else {
print("Either album name or photo index missing")
}
}