wintertreey 님의 블로그
커스텀 URL 스킴 본문
Custom URL Scheme
앱마다 개발자가 지정한 고유 스킴(식별자)을 사용
myapp://something 처럼 :// 뒤에 경로/파라미터를 붙일 수 있음
Info.plist → CFBundleURLTypes 에 등록 필요
iOS 시스템이 이 스킴을 인식하면 앱을 실행하거나 호출
작업순서
1. info.plist 내부에 스킴을 추가한다.
myapp 자리에 앱을 호출 할때의 고유 스킴을 넣어주면 된다.
<key>CFBundleURLTypes</key>
<array>
<dict>
<key>CFBundleURLName</key>
<string>com.example.myapp</string>
<key>CFBundleURLSchemes</key>
<array>
<string>myapp</string>
</array>
</dict>
</array>
2. Scenedelegate.swift 작업
ios 13 부터 애플은 SceneDelegate 가 도입되었다.
AppDelegate는 앱 전체를 관리한다면, SceneDelegate는 각 Scene단위로 이벤트를 관리한다.
SceneDelegate의 주요 책임:
- UIWindow 생성/관리
- Scene lifecycle 관리 (willConnectTo, willEnterForeground, didDisconnect 등)
- Scene 단위의 외부 URL 호출 처리
그래서 앱에 URL Scheme 호출이 들어오면 시스템이 SceneDelegate에게 먼저 전달한다.
여기서 URL을 잡아내고, 내부로직으로 전달한다.
// ios가 딥링크를 앱으로 전달
func scene(_ scene: UIScene, openURLContexts URLContexts: Set<UIOpenURLContext>) {
guard let urlContext = URLContexts.first else { return }
let url = urlContext.url
println("custom scheme url:", url.absoluteString)
handleCustomUrl(url)
}
private func handleCustomUrl(_ url: URL) {
print("custom scheme 처리 함수 호출 확인로그", url)
if let nav = window?.rootViewController as? UINavigationController,
let webVC = nav.viewControllers.first as? WebviewController {
webVC.handleDeepLink(url)
}
}
3. WebViewController.swift 작업
SceneDelegate에서 받아서 실제 화면처리를 해준다.
// 딥링크
func handleDeepLink(_ url: URL) {
print("WebView 딥링크 받음:: ", url.absoluteString)
let js = "onDeeplink('\(url.absoluteString)')"
self.callJavaScript(script: js) { _, _ in }
}