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 연경
}
}
}
}
}'SwiftUI' 카테고리의 다른 글
| [SwiftUI] onAppear / onDisappear 호출시점 (0) | 2024.04.23 |
|---|---|
| [SwiftUI] NotificationCenter 사용하기 (0) | 2024.04.17 |
| [SwiftUI] View 백그라운드/포그라운드 이벤트 체크 (0) | 2024.04.17 |
| [SwiftUI] AppDelegate 추가해서 Push 설정하기 (0) | 2024.04.17 |
| [SwiftUI] TextField 화면 터치로 키보드 내리기(전역설정) (0) | 2024.04.17 |