UIKit 프로젝트의 경우 App Scheme 처리시 아래 함수로 호출이 되는데 SwiftUI 에서는 어떻게 처리하는지 몰라 찾아봤음.

 

[App to App]

func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool

 

[Universal-Link]

func application(_ application: UIApplication,

                     continue userActivity: NSUserActivity,

                     restorationHandler: @escaping ([UIUserActivityRestoring]?) -> Void) -> Bool

 

SwiftUI 에서 Scheme 처리하기 방식 (간단함 ㅎ)

@main
struct SwiftUIComponemtApp: App {
    var body: some Scene {
        WindowGroup {
            ContentView()
                // MARK: App Open Scheme URL (App to App / Universal-Link)
                // Universal Link 및 Scheme App to App 둘 다 들어옴
                .onOpenURL { url in
                    log(direction: .ETC, ofType: self, datas: "App to App 스키마", url)
                    
                    //if url.scheme == APP_SCHEME {
                        // NotificationCenter User 데이터 전송
                        NotificationCenter.default.post(name: NSNotification.Name(rawValue: "schemeNotification"), object: url, userInfo: nil)
                    //}
                    
                    
                }
        }
    }
}

 

하이브리드 앱의 경우 다이렉트로 웹뷰 랩퍼 클래스에서 받아서 처리하는게 편함.

class IWebView: UIView {
     deinit {
        log(direction: .WEBVIEW, ofType: self, datas: "deinit : \(String(describing: webView?.url?.absoluteString))")
        
        // NotificationCenter 삭제
        NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: "schemeNotification"), object: nil)
    }
    
    init(_ parent: IWebViewRepresentable) {
        super.init(frame: .zero)
        
        //////////////////////////////////////////////////
        // NotificationCenter User Noti 등록
        NotificationCenter.default.addObserver(self, selector: #selector(schemeNotification(noti:)), name: NSNotification.Name(rawValue: "schemeNotification"), object: nil)
        
    }
    
    //MARK: SSO 및 기타 외부 URL 스키마 연결
    @objc func schemeNotification(noti: Notification){
    
        if let url: URL = noti.object as? URL {
        
            // Universal-Link
            if url.scheme == "http" || url.scheme == "https" {
                let urlString = url.absoluteString
                webViewLoad(urlString: urlString, httpMethod: .GET)
                return
            }
            
            // APP to APP Link
            // ex : "app://open?target_url=https://xxxxxxxx.com"
            if url.scheme == APP_SCHEME {
                if url.host == "open" {
            	    // target_url 파싱 후 URL 연경
                }
            }
        }
    }

}

+ Recent posts