如何使用SiriKit通过Web Service发送数据?求可行实现方案
Great question! You’re right that early versions of SiriKit were limited to built-in intents, but since iOS 12, Apple introduced Custom Intents which let you define your own voice commands and actions—exactly what you need to trigger sending data to your web server with that specific phrase.
Here’s a step-by-step guide to implement this:
1. Set Up the Custom Intent
- In Xcode, add an Intents Extension to your project (File > New > Target > Intents Extension).
- Create a new Intent Definition file (File > New > File > Intent Definition).
- In the intent editor:
- Add a new intent, name it something like
SendDataIntent. - Under "Siri & Shortcuts", check "Eligible for Siri" and set the category to "Custom".
- In the Phrase Templates section, add your desired command:
"send data using SomeApplication"(replace "SomeApplication" with your app’s actual name). You can add multiple variations if needed. - If you don’t need any parameters (like specific data to send), leave the parameters section empty.
- Add a new intent, name it something like
2. Implement the Intent Handler
In your Intents Extension, create a handler class that conforms to the generated intent protocol (e.g., SendDataIntentHandling):
import Intents class SendDataIntentHandler: NSObject, SendDataIntentHandling { func handle(intent: SendDataIntent, completion: @escaping (SendDataIntentResponse) -> Void) { // Your code to send data to the web server goes here guard let url = URL(string: "https://your-web-server.com/api/endpoint") else { completion(SendDataIntentResponse.failure(error: "Invalid server URL")) return } var request = URLRequest(url: url) request.httpMethod = "POST" // Add your request body, headers, etc., as needed let task = URLSession.shared.dataTask(with: request) { [weak self] data, response, error in DispatchQueue.main.async { if let error = error { completion(SendDataIntentResponse.failure(error: error.localizedDescription)) } else if let httpResponse = response as? HTTPURLResponse, httpResponse.statusCode >= 400 { completion(SendDataIntentResponse.failure(error: "Server returned an error")) } else { completion(SendDataIntentResponse.success()) } } } task.resume() } }
Don’t forget to register this handler in the extension’s IntentHandler class:
class IntentHandler: INExtension { override func handler(for intent: INIntent) -> Any { guard intent is SendDataIntent else { fatalError("Unhandled intent type: \(intent)") } return SendDataIntentHandler() } }
3. Configure App Capabilities & Permissions
- In your main app’s target, enable the Siri capability (Project Settings > Signing & Capabilities > + Capability > Siri). Do the same for your Intents Extension target.
- Add the
NSSiriUsageDescriptionkey to your main app’sInfo.plistwith a message explaining why your app uses Siri (e.g., "To allow sending data via voice commands").
4. Test It Out
- Build your app to a physical iOS device (Siri custom intents don’t work reliably on simulators).
- Go to Settings > Siri & Search > All Apps > Your App Name and enable "Use with Siri".
- Now try saying: "Hey Siri, send data using SomeApplication"—your intent should trigger, and the code to send data to your server will run.
If you later need to pass specific data (like "send my location data using SomeApplication"), you can add parameters to your custom intent and update the phrase templates to include placeholders for those parameters.
内容的提问来源于stack exchange,提问作者Tania Jasam

