반응형
- 이제 기본적인 설정 및 테스트를 완료하였으므로 구현 코드를 보자
// 수신되는 링크 포맷
// com.company.appname://google/link/?deep_link_id=https://com.company.appname/testlink?param=XXXXXX ... &match_message=Link is uniquely matched for this device.
func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {
if let dynamicLink = DynamicLinks.dynamicLinks().dynamicLink(fromCustomSchemeURL: url)
{
self.handleIncomingDynamicLink(dynamicLink)
return true
}
return true
}
// 수신되는 링크 포맷
// https://appname.page.link/?link=https://com.company.appname/testlink?param=XXXXXX ...
func application(_ application: UIApplication,
continue userActivity: NSUserActivity,
restorationHandler: @escaping ([UIUserActivityRestoring]?) -> Void) -> Bool
{
if let incomingURL = userActivity.webpageURL {
print("Incoming : \(incomingURL)")
let linkHandled = DynamicLinks.dynamicLinks().handleUniversalLink(incomingURL) { (dynamicLink, error) in
guard error == nil else {
print("Found an error! \(error!.localizedDescription)")
return
}
if let dynamicLink = dynamicLink {
self.handleIncomingDynamicLink(dynamicLink)
}
}
if linkHandled
{
return true
}
else
{
if incomingURL.host == "appname.page.link" {
guard let components = URLComponents(url: incomingURL, resolvingAgainstBaseURL: true),
let queryItems = components.queryItems else { return false }
if let link = queryItems.first(where: { $0.name == "link" })?.value {
print("link = \(link)")
// https://com.company.appname/testlink?param%3D12 ...
return true
}
}
}
}
return false
}
// 수신되는 링크 포맷
// https://com.company.appname/testlink?param=@#$%@#$%^#$%^
func handleIncomingDynamicLink(_ dynamicLink: DynamicLink) {
guard let url = dynamicLink.url else {
print("The dynamic link object has not url.")
return
}
print("url : \(url.absoluteString)")
guard let components = URLComponents(url: url, resolvingAgainstBaseURL: false),
let queryItems = components.queryItems else { return }
if let param = queryItems.first(where: { $0.name == "param" })?.value {
print("param = \(param)")
}
}
- 상황에 따라 호출되는 포맷이 다르므로 주의하도록 한다.
- 앱이 설치되어 있고 웹링크를 통해 호출된 경우 :
https://appname.page.link/?link=https://com.company.appname/testlink?param=XXXXXX ...
- 앱이 설치되어 있지 않아 스토어에서 설치된 후 실행된 경우 :
com.company.appname://google/link/?deep_link_id=https://com.company.appname/testlink?param=XXXXXX ... &match_message=Link is uniquely matched for this device.
2021.06.17 - [iOS/Tips] - Firebase dynamic link (다이나믹 링크) (1/4)
2021.06.17 - [iOS/Tips] - Firebase dynamic link (다이나믹 링크) (2/4)
2021.06.17 - [iOS/Tips] - Firebase dynamic link (다이나믹 링크) (3/4)
2021.06.17 - [iOS/Tips] - Firebase dynamic link (다이나믹 링크) (4/4)
2022.07.21 - [iOS/Tips] - 유니버셜 링크(Universal Links) 문제점 해결
반응형
'개발 > iOS' 카테고리의 다른 글
Foreground 이벤트 받기 (0) | 2021.08.02 |
---|---|
Firebase dynamic link (다이나믹 링크) (4/4) (0) | 2021.06.17 |
Firebase dynamic link (다이나믹 링크) (2/4) (0) | 2021.06.17 |
Firebase dynamic link (다이나믹 링크) (1/4) (2) | 2021.06.17 |
두개의 이미지 합치기 (0) | 2021.03.18 |