iOS Siri 快捷方式,NSUserActivity,不适用于 SiwiftUI

时间:2021-01-17 21:39:56

标签: ios swift swiftui nsuseractivity

尝试在 Swiftui 中为 Siri Shortcut 实现 NSUserActivity,但总是出现此错误

<块引用>

2021-01-18 02:23:32.625096+0500 Siri 随机数[56083:3284534] [main] sendUserActivityToServer,在活动上调用 B31E5E4B-A26C-4B0A-845A-BE24EE3729D2失效后,所以 什么都不做。

我认为“userActivity”在我的 SwiftUI 视图类中不可用,这就是它没有注册的原因。

我也尝试从 AppDelegate 调用一个函数,在那里我可以访问“userActivity”,但也不起作用。

环境: macOS:Catalina 10.15.2 代码:11.6 ios:13.6

import SwiftUI
import Intents
import UIKit
import Foundation



struct ContentView:  View {
    
    @State private var value = 0
    
    var body: some View {
        
        VStack{
            Text(String(value))
            Button(action:{
                self.increment()
            }){
                Text("Increment")
            }
        }
        
        
    }
    
    
    func increment() -> Void {
        value += 1
        print("working")
        
        
        createUserActivity()
        //        let ad = AppDelegate()
        //        ad.createUserActivity()
        //
        //        self.createUserActivity()
        //        NSActivity.init()
    }
    
    
    func createUserActivity() -> Void {
        let activity = NSUserActivity(activityType: "com.random.increment")
        
        activity.title = "Increment Counter"
        activity.isEligibleForSearch = true
        activity.isEligibleForPrediction = true
        activity.isEligibleForPublicIndexing = true
        
        activity.becomeCurrent()
        
        //        let sd = SceneDelegate()
        //        sd.userActivity = activity
        //        sd.userActivity?.becomeCurrent()
        //        let ad = AppDelegate()
        //        ad.userActivity = activity
        //        ad.userActivity?.becomeCurrent()
        
        
        
    }
    
    
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}



class NSActivity {
    
    
    init() {
        
        let activity = NSUserActivity(activityType: "com.random.increment")
        
        activity.title = "Increment Counter"
        activity.isEligibleForSearch = true
        activity.isEligibleForPrediction = true
        activity.isEligibleForPublicIndexing = true
        //        let ad = AppDelegate()
        //        ad.userActivity = activity
        //        ad.userActivity?.becomeCurrent()
        activity.becomeCurrent()
        
    }
    
}

0 个答案:

没有答案