간단해서 예제 소스코드로 정리합니다.

 

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 처리
                   }
             }
        }
    }
}

+ Recent posts