【问题标题】:How to make deep link CoreSpotlight with Swiftui app Lifecycle?如何将 CoreSpotlight 与 Swiftui 应用生命周期进行深度链接?
【发布时间】: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

标签: ios swift xcode swiftui


【解决方案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)
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-04-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-08-16
    • 2021-02-18
    • 2015-08-01
    相关资源
    最近更新 更多