首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何用Swiftui应用生命周期制作深度链接CoreSpotlight?

如何用Swiftui应用生命周期制作深度链接CoreSpotlight?
EN

Stack Overflow用户
提问于 2021-04-20 11:35:50
回答 1查看 197关注 0票数 1

我用这个在聚光灯上添加项目,现在我可以在聚光灯下搜索这些项目

但是,当单击聚光灯下的项目时,我如何才能获得详细信息页呢?

我找不到一款迅捷应用生命周期药水。

增加项目:

代码语言:javascript
复制
    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

代码语言:javascript
复制
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

如何在用户单击项目外的同时获得正确的项和goto TodoDetail()?

EN

回答 1

Stack Overflow用户

发布于 2022-03-11 02:47:46

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

代码语言:javascript
复制
.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)
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67178125

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档