
Phenaki API 價格:探索最新技術與市場趨勢
要在應用中集成 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 來簡化用戶的操作流程。例如,用戶可以設置短語“播放下雨聲”來觸發某個應用的白噪音播放功能。