是否可以让 Siri 建议将事件添加到我的应用程序而不是默认的日历应用程序?

问题描述 投票:0回答:1

我看了这个视频https://developer.apple.com/videos/play/wwdc2019/243/ 我知道我的应用程序可以创建交互并将其捐赠给 Siri,这样 Siri 就可以在日历、地图中推荐那些等等。但我想要实现的是“反向捐赠”,即当 Siri 在电子邮件中发现事件时,它会建议添加到我的应用程序而不是默认的日历应用程序。

如果我做对了,Siri 发现的那些事件存储在某种称为“在自然语言中发现”的内部日历中,所以也许我可以做这样的事情?

let eventStore = EKEventStore()
var fetchedEvents = [EKEvent]()
        
let startDate = Date()
let endDate = Calendar.current.date(byAdding: .day, value: 30, to: Date())!
        
let predicate = eventStore.predicateForEvents(withStart: startDate, end: endDate, calendars: ["Found in Natural Language"] )
fetchedEvents = eventStore.events(matching: predicate)

但当然它不仅仅是一个字符串,我可能需要在那里提供实际的

EKCalendar
对象,但如何做到这一点? “在自然语言中发现”日历是否可以访问?

ios swift siri ekeventstore
1个回答
0
投票

是的,可以让 Siri 建议将事件添加到自定义应用程序而不是默认的日历应用程序。

为此,您需要在自定义应用程序中启用

SiriKit
支持并实现添加事件的相关意图。完成此操作后,您可以使用
INInteraction
类向 Siri 提供有关事件的信息以及用户将其添加到您的应用程序的意图。

以下是启用 SiriKit 支持以将事件添加到自定义应用程序所需遵循的基本步骤:

将 SiriKit 框架添加到您的 Xcode 项目中。

  1. 为您的应用创建一个新的 SiriKit 扩展目标。
  2. 实现添加事件的相关intent,如
    INAddTasksIntent
    .
  3. 通过创建一个
    NSUserActivity
    对象来配置您的应用程序的意图处理,该对象指定您的应用程序支持的意图类型。
  4. 使用
    INInteraction
    类向 Siri 提供有关事件的信息以及用户将其添加到您的应用程序的意图。
  5. 完成这些步骤后,Siri 将能够建议将事件添加到您的自定义应用程序,而不是默认的日历应用程序。
  6. 然后用户可以确认建议并直接从 Siri 界面将事件添加到您的应用程序中。

完成这些步骤后,Siri 将能够建议将事件添加到您的自定义应用程序,而不是默认的日历应用程序。然后,用户可以直接从 Siri 界面确认建议并将事件添加到您的应用程序。

如何实现添加事件的 INAddTasksIntent:

import Intents

class AddEventIntentHandler: NSObject, INAddTasksIntentHandling {
    func handle(intent: INAddTasksIntent, completion: @escaping (INAddTasksIntentResponse) -> Void) {
        guard let task = intent.targetTask else {
            completion(INAddTasksIntentResponse(code: .failure, userActivity: nil))
            return
        }
        
        // Get the details of the task
        let title = task.title
        let dueDateComponents = task.dueDateComponents
        
        // Create a new event in your custom app
        let event = Event(title: title, dueDateComponents: dueDateComponents)
        EventManager.shared.add(event: event)
        
        // Return a success response to Siri
        let response = INAddTasksIntentResponse(code: .success, userActivity: nil)
        completion(response)
    }
}

要为您的应用配置意图处理,您还需要创建 一个 NSUserActivity 对象,指定您的应用程序的意图类型 支持。这是一个如何做到这一点的例子:

let activity = NSUserActivity(activityType: "com.yourcompany.addEvent")
activity.title = "Add an event"
activity.userInfo = ["key": "value"]
activity.isEligibleForPrediction = true
activity.isEligibleForSearch = true
activity.isEligibleForPublicIndexing = true
activity.suggestedInvocationPhrase = "Add an event"

let attributes = CSSearchableItemAttributeSet(itemContentType: kUTTypeImage as String)
attributes.title = "Add an event"
attributes.contentDescription = "Add an event to my custom app"
attributes.thumbnailData = UIImage(named: "icon")?.pngData()

activity.contentAttributeSet = attributes

view.userActivity = activity
activity.becomeCurrent()
© www.soinside.com 2019 - 2024. All rights reserved.