간단해서 예제 소스코드로 정리합니다.
let APP_SCHEME = "SwiftUIComponemtApp"
extension Notification.Name {
static let schemeNotification = Notification.Name("schemeNotification")
}
@main
struct SwiftUIComponemtApp: App {
@UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
// @Environment(\.scenePhase) private var scenePhase
var body: some Scene {
WindowGroup {
ContentView()
// MARK: App Open Scheme URL (App to App)
.onOpenURL { url in
log(direction: .ETC, ofType: self, datas: "App to App 스키마", url)
if url.scheme == APP_SCHEME, url.host == "host명" {
if let param = url.queryDictionary {
if let token = param["token"] {
// NotificationCenter User 데이터 전송
NotificationCenter.default.post(
name: .schemeNotification,
object: ("host명", token),
userInfo: nil
)
}
}
return true
}
}
}
}
}
받는쪽
struct ContentView: View {
@ObservedObject var homeListViewModel = HomeListViewModel()
var body: some View {
ScrollView(.vertical) {
VStack {
MainBannerView(bannerInfos: homeListViewModel.bannerInfos)
.frame(height: 114.4)
Text("Hello!!!")
}
}
.onReceive(NotificationCenter.default.publisher(for: .schemeNotification), perform: { noti in
if let schemeData: (host: String, token: String) = noti.object as? (String, String){
if schemeData.host == "host명" {
let token = schemeData.token
//token 처리
}
}
}
}
}
'SwiftUI' 카테고리의 다른 글
[SwiftUI] View Full Modal Present (0) | 2024.05.02 |
---|---|
[SwiftUI] onAppear / onDisappear 호출시점 (0) | 2024.04.23 |
[SwiftUI] App to App / Universal-Link Scheme 처리 이벤트 (0) | 2024.04.17 |
[SwiftUI] View 백그라운드/포그라운드 이벤트 체크 (0) | 2024.04.17 |
[SwiftUI] AppDelegate 추가해서 Push 설정하기 (0) | 2024.04.17 |