The following is my code, which runs successfully and is recommended to succeed, but it still does not show Siri's suggestion to users in the system sharing in Safari
import AppIntents
import Contacts
import CoreSpotlight
import Intents
import UIKit
class TestViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = UIColor.white
DispatchQueue.global().async {
self.donateMessageInteraction(recipientName: "张三", phoneNumber: "+8613812345678")
}
}
func donateMessageInteraction(recipientName: String, phoneNumber: String) {
INPreferences.requestSiriAuthorization { status in
guard status == .authorized else { return }
// 1. 创建接收者
let recipientHandle = INPersonHandle(value: phoneNumber, type: .phoneNumber)
let recipient = INPerson(
personHandle: recipientHandle,
nameComponents: nil,
displayName: recipientName,
image: nil,
contactIdentifier: nil,
customIdentifier: "com.yourapp.recipient.\(phoneNumber)"
)
// 2. 创建发送者(你的应用身份)
let senderHandle = INPersonHandle(value: "15210639372@163.com", type: .emailAddress)
let sender = INPerson(
personHandle: senderHandle,
nameComponents: nil,
displayName: "我的应用",
image: nil,
contactIdentifier: nil,
customIdentifier: "com.yourapp.sender"
)
// 3. 创建消息意图
let intent = INSendMessageIntent(
recipients: [recipient],
outgoingMessageType: .outgoingMessageText,
content: "最近怎么样?", // 常用消息内容
speakableGroupName: nil, // 群组名称(一对一设为nil)
conversationIdentifier: "com.yourapp.conversation.\(phoneNumber)", // 唯一会话ID
serviceName: "My Chat Service", // 你的消息服务名称
sender: sender, // 发送者身份
attachments: nil
)
// 4. 配置意图参数
intent.setImage(INImage(named: "user0"), forParameterNamed: \.sender)
// 5. 创建并捐赠交互
let interaction = INInteraction(intent: intent, response: nil)
interaction.direction = .outgoing
interaction.donate { error in
error.map { print("捐赠失败: \($0)") }
}
print("捐赠代码执行完成")
}
}
}