如何将 CoreSpotlight 与 Swiftui 应用生命周期进行深度链接?

Posted

技术标签:

【中文标题】如何将 CoreSpotlight 与 Swiftui 应用生命周期进行深度链接?【英文标题】:How to make deep link CoreSpotlight with Swiftui app Lifecycle? 【发布时间】:2021-04-20 11:35:50 【问题描述】:

我正在使用它在聚光灯上添加项目,现在我可以在聚光灯上搜索这些项目

但是当我点击聚光灯上的项目时,我怎样才能进入详细信息页面?

我找不到 swiftui-app 生命周期解决方案

添加项目:

    let attributeSet = CSSearchableItemAttributeSet(itemContentType: kUTTypeContact as String)
    attributeSet.title = task
    attributeSet.contentDescription = task
    attributeSet.relatedUniqueIdentifier = uuid.uuidString
    attributeSet.identifier = uuid.uuidString
   
    attributeSet.addedDate = Date()

    let searchableItem = CSSearchableItem(uniqueIdentifier: uuid.uuidString, domainIdentifier: aType, attributeSet: attributeSet)

    CSSearchableIndex.default()
        .indexSearchableItems([searchableItem])  error in
    
                if let error = error 
                    print("Error indexing: \(error)")
                 else 
                    print("Indexed.")
                 // error
         // .indexSearchableItems

我的 app.swift

import SwiftUI

import Intents
import CoreSpotlight
import CoreServices


let aType = "com.example.icecream-selection"

 

@main
struct DevoteApp: App 
    
    
    let persistenceController = PersistenceController.shared
    let nsUserActivity: NSUserActivity = NSUserActivity(activityType: aType)
    
    @AppStorage("isDarkMode") var isDarkMode: Bool = false

    @Environment(\.scenePhase) var scenePhase
    


    var body: some Scene 
        WindowGroup 
            
             
             
            
             // CSSearchableItemActionType
            if nsUserActivity.activityType == CSSearchableItemActionType 


                if (nsUserActivity.userInfo?[CSSearchableItemActivityIdentifier] as? String) != nil 

                        let context = PersistenceController.shared.container.viewContext
                        let newItem = Item(context: context)
                        TodoDetail(item:newItem)


                     // nil
             else 
                
                ContentView()
                // IceContentView()
                    .environment(\.managedObjectContext, persistenceController.container.viewContext)
                    .preferredColorScheme(isDarkMode ? .dark : .light)
                    .onOpenURL  url in
                              print("Received URL: \(url)")
                            
                    
                    .onContinueUserActivity( CSSearchableItemActionType)  userActivity in
                        if let color = userActivity.persistentIdentifier 
                                  print(">>>>>>>SUCCESS ACTIVITY<<<<<<<")
                                  print(color)
                              
                            
                    
             
              // CSSearchableItemActionType
     
            
            
            
          // WindowGroup
        
        .onChange(of: scenePhase)  newScenePhase in
              switch newScenePhase 
              case .active:
                print("App is active")
                print("KEY TYPE")
                print(nsUserActivity.activityType)
                print(CSSearchableItemActionType)
                
                
                
              case .inactive:
                print("App is inactive")
              case .background:
                print("App is in background")
              @unknown default:
                print("Oh - interesting: I received an unexpected new value.")
               // newScenePhase
             // .onChange
        
      // Scene
  // App

当用户单击项目外侧时,我怎样才能获得正确的项目并转到 TodoDetail() ?

【问题讨论】:

你有没有想过这个问题? 放弃吧。 swift ui 太难学了。人生苦短,我用flutter 【参考方案1】:

您正在寻找的是将其添加到您的其中一个具有 navigationLink 的视图中,其 viewModel 包含 selectItem 变量。

.onContinueUserActivity(CSSearchableItemActionType, perform: loadItem)

func loadItem(_ userActivity: NSUserActivity) 
    if let uniqueIdentifier = userActivity.userInfo?[CSSearchableItemActivityIdentifier] as? String 
        // You need to have a NavigationLink that you has the selection set to selectItem with it's tag being the uniqueIdentifier to trigger to navigation.
        viewModel.selectItem(with: uniqueIdentifier)
    

【讨论】:

以上是关于如何将 CoreSpotlight 与 Swiftui 应用生命周期进行深度链接?的主要内容,如果未能解决你的问题,请参考以下文章

如何将 swift 文件与 SKscene 连接?

如何将 SCNFloor 的属性与 Swift 和 Scenekit 一起使用?

如何将 TestFlight 与 Swift 1.2 应用程序一起使用?

如何将 FMDB 与 cocoapods 和 swift 一起使用?

如何将 NSError.code 与 Swift 5 中的#defined 错误号进行比较

Swift:如何将 for-in 循环与可选参数一起使用?