夫特3.1溶液。
我們正在通過來自聯繫人顯示名稱的信息傳遞該用戶活動。我需要說明我要打電話給誰。
我有這個簡單的數組,這是一個模擬數據庫的表示。也許在你的應用程序中有某種具有他們所有聯繫信息的用戶列表,並且你可以根據傳入resolveContacts的信息來檢查這些聯繫人。用戶說他們想給戴夫打電話,我確定那是在數據庫中,如果是,那麼我打電話給戴夫。而爲了打電話,你需要一個INPerson,它需要一個personHandle,它基本上是一個人的唯一標識符。
您可以使用電子郵件或電話號碼。我選擇在這裏用電話號碼。如果它有合適的名稱,它會創建這個INPersonHandle,將它作爲一個人傳遞給這個電話號碼和任何名字,如果它與我現有的聯繫人匹配,然後我說那個人的完成是成功的。如果沒有匹配的聯繫人,那麼我們會回顧用戶說我們需要一個值。
import Intents
class IntentHandler: INExtension,INStartAudioCallIntentHandling {
override func handler(for intent: INIntent) -> Any? {
return self
}
func handle(startAudioCall intent: INStartAudioCallIntent, completion: @escaping (INStartAudioCallIntentResponse) -> Void) {
print("handle")
let ua = NSUserActivity(activityType: "Call")
let person:String = intent.contacts![0].displayName
ua.userInfo = ["person":person]
completion(INStartAudioCallIntentResponse(code: .continueInApp, userActivity: ua))
}
func confirm(startAudioCall intent: INStartAudioCallIntent, completion: @escaping (INStartAudioCallIntentResponse) -> Void) {
completion(INStartAudioCallIntentResponse(code: .ready, userActivity: nil))
}
func resolveContacts(forStartAudioCall intent: INStartAudioCallIntent, with completion: @escaping ([INPersonResolutionResult]) -> Void) {
print("resolveContacts")
let contacts:[String] = ["Dave","James","Herman"]
for contact in contacts {
if intent.contacts?[0].spokenPhrase?.uppercased() == contact.uppercased() {
let personHandle:INPersonHandle = INPersonHandle(value: "1-555-555-5555", type: .phoneNumber)
let person:INPerson = INPerson(personHandle: personHandle, nameComponents: nil, displayName: contact, image: nil, contactIdentifier: nil, customIdentifier: nil)
completion([INPersonResolutionResult.success(with: person)])
return
}
}
completion([INPersonResolutionResult.needsValue()])
}
}
他們都是可選的,沒有幫助。 – EternalLight
@EternalLight檢查了這個? https://forums.developer.apple.com/thread/62250 – JuicyFruit
是的,正在閱讀。他們說這個問題是在Swift 3中使用了@escaping屬性。在Swift 2.3中,默認情況下會關閉Closures,它應該可以正常工作,但它不會。 – EternalLight