
Phenaki API 价格:探索最新技术与市场趋势
苹果自 iOS10 开放了 Siri API,推出了 SiriKit,赋予开发者更大的自由度和应用空间。SiriKit 的功能非常强大,支持音频、视频、消息发送接收、搜索照片、预订行程、管理锻炼等等。本文将详细介绍如何调用 Siri 的 API,并提供丰富的代码示例和图片链接来帮助理解这一过程。
SiriKit 是苹果为开发者提供的一个强大的工具集,允许应用与 Siri 进行深度集成。开发者可以使用 SiriKit,让用户通过语音命令来控制应用的各种功能。SiriKit 支持的域包括语音通话、信息传递、媒体播放、任务管理、支付、餐厅预订、健身和打车服务等。通过这些域,开发者可以让应用与 Siri 无缝合作,提供更自然、更人性化的用户体验。
SiriKit 中的每个功能都属于特定的域(Domain),而每个域又包含多个意图(Intent)。例如,信息域支持发送信息和搜索信息的意图,媒体域支持播放和控制音频的意图。了解每个域和意图的具体功能,是有效利用 SiriKit 的关键。
要在应用中集成 SiriKit,首先需要在 Xcode 中创建一个新的 Intents Extension。这个扩展将处理所有与 Siri 的交互请求。开发者需要在扩展中实现特定的协议,以响应用户通过 Siri 发出的请求。
在 Xcode 中,选择 File -> New -> Target
,然后选择 Intents Extension
。命名为 MySiriExtension
,并勾选 Include UI Extension
,以便后续添加交互界面。
在 SiriKit 中发送消息是一个常见的功能。以下是实现这一功能的步骤。
首先,在 Xcode 中创建一个新的项目,并添加一个 Intents Extension。然后,在 Info.plist
文件中配置支持的意图,例如 INSendMessageIntent
。确保应用有权访问用户的联系人信息,以便正确解析和发送消息。
在主项目中创建一个 MyAccount.swift
文件,用于管理用户信息和消息发送逻辑。以下是示例代码:
import Intents
class MyUser {
var name: String?
var handle: String?
init(name: String? = nil, handle: String? = nil) {
self.name = name
self.handle = handle
}
func toInPerson() -> INPerson {
return INPerson(handle: handle!, displayName: name, contactIdentifier: name)
}
}
class MyAccount {
private static let instance = MyAccount()
class func share() -> MyAccount {
return MyAccount.instance
}
func contact(matchingName: String) -> [MyUser] {
return [MyUser(name: matchingName, handle: NSStringFromClass(MySendMessageIntentHandler.classForCoder()))]
}
func send(message: String, to recipients: [INPerson]) -> INSendMessageIntentResponseCode {
print("模拟发送消息:(message) 给 (recipients)")
return .success
}
}
在 IntentHandler.swift
中,苹果已经为我们生成了消息的示例代码。开发者只需实现 INSendMessageIntentHandling
协议中的方法来处理和发送消息。
以下是 MySendMessageIntentHandler.swift
的示例代码:
import UIKit
import Intents
class MySendMessageIntentHandler: NSObject, INSendMessageIntentHandling {
// MARK: - INSendMessageIntentHandling
// Implement resolution methods to provide additional information about your intent (optional).
// 处理收件人
func resolveRecipients(for intent: INSendMessageIntent, with completion: @escaping ([INSendMessageRecipientResolutionResult]) -> Void) {
if let recipients = intent.recipients {
// If no recipients were provided we'll need to prompt for a value.
if recipients.count == 0 {
completion([INSendMessageRecipientResolutionResult.needsValue()])
return
}
var resolutionResults = [INSendMessageRecipientResolutionResult]()
for recipient in recipients {
let matchingContacts = MyAccount.share().contact(matchingName: recipient.displayName)
// Implement your contact matching logic here to create an array of matching contacts
switch matchingContacts.count {
case 2 ... Int.max:
// We need Siri's help to ask user to pick one from the matches.
let disambiguations = matchingContacts.map{ $0.toInPerson() }
resolutionResults += [INSendMessageRecipientResolutionResult.disambiguation(with: disambiguations)]
case 1:
// We have exactly one matching contact
let recipient = matchingContacts[0].toInPerson()
resolutionResults += [INSendMessageRecipientResolutionResult.success(with: recipient)]
case 0:
// We have no contacts matching the description provided
resolutionResults += [INSendMessageRecipientResolutionResult.unsupported()]
default:
break
}
}
completion(resolutionResults)
} else {
completion([INSendMessageRecipientResolutionResult.needsValue()])
}
}
// 处理发送的文字内容
func resolveContent(for intent: INSendMessageIntent, with completion: @escaping (INStringResolutionResult) -> Void) {
if let text = intent.content, !text.isEmpty {
completion(INStringResolutionResult.success(with: text))
} else {
completion(INStringResolutionResult.needsValue())
}
}
// Once resolution is completed, perform validation on the intent and provide confirmation (optional).
// 确认阶段,可以判断用户是否有权限(如是否已登录)、可以获取intent事件中的值,并可对其做最终修改
func confirm(intent: INSendMessageIntent, completion: @escaping (INSendMessageIntentResponse) -> Void) {
// Verify user is authenticated and your app is ready to send a message.
let userActivity = NSUserActivity(activityType: NSStringFromClass(INSendMessageIntent.self))
let response = INSendMessageIntentResponse(code: .ready, userActivity: userActivity)
completion(response)
}
// Handle the completed intent (required).
// 处理阶段,实现app消息发送的代码逻辑
func handle(intent: INSendMessageIntent, completion: @escaping (INSendMessageIntentResponse) -> Void) {
// Implement your application logic to send a message here.
if let content = intent.content, let recipients = intent.recipients {
let userActivity = NSUserActivity(activityType: NSStringFromClass(INSendMessageIntent.self))
let sendResult = MyAccount.share().send(message: content, to: recipients)
completion(INSendMessageIntentResponse(code: sendResult, userActivity: userActivity))
} else {
let response = INSendMessageIntentResponse(code: .failure, userActivity: nil)
completion(response)
}
}
}
Siri Shortcuts 是苹果在 iOS12 推出的新功能,允许用户通过自定义的短语执行特定的操作。开发者可以使用 Siri Shortcuts 来简化用户的操作流程。例如,用户可以设置短语“播放下雨声”来触发某个应用的白噪音播放功能。