简体   繁体   中英

Sirikit intent handling

I do apologize if I'm not posting correctly since I'm a little new to posting here. I'm currently attempting to add a siri shortcut into my application. I've created the intent and I'm able to handle it properly and create a response with dummy data.

I am however, unable to access my service classes and other objects from the application despite adding my app to the intent handler class's target.

class IntentHandler: INExtension, TestIntentHandling {
        @available(iOS 12.0, *)

    func confirm(intent: TestIntent, completion: @escaping (TestIntentResponse) -> Void) {
        print("HERE")
        completion(TestIntentResponse.init(code: .ready, userActivity: nil))
    }


    @available(iOS 12.0, *)
    func handle(intent: TestIntent, completion: @escaping (TestIntentResponse) -> Void) {
        let response = TestIntentResponse.init(code: .success, userActivity: nil)

        //Trying to reach into service here to get real values

        response.workout = "Bench Press"
        response.weight = 150
        completion(response)

    }

}

I would like to reach into my application services to populate my workout and weight fields in my handle function but I keep getting an error saying that my service classes do not exist and was hoping someone would be able to point me in the right direction. Thanks!

According to the documentation :

When a user makes a request of your app using Siri or Maps, SiriKit loads your Intents app extension and creates an instance of its INExtension subclass. The job of your extension object is to provide SiriKit with the handler objects that you use to handle specific intents. You provide these objects from the handler(for:) method of your extension object.

You need to call the handler(for:) method and return the appropriate handler class (this will be a class you create). Your intent handler class, eg TestIntentHandler , will subclass NSObject and conform to your TestIntentHandling protocol. TestIntentHandler is where you would handle your intent.

You need to create an app group and move any classes and methods you need to use in both the app and intent into a Framework shared between both. For things like small bits of data you can use a shared UserDefaults using UserDefaults(suiteName: "your.app.group").

From the docs :

If your app and app extension share services, consider structuring your code in the following way:

• Implement your core services in a private shared framework. A private shared framework lets you place the code for accessing your services in one code module and use that code from multiple targets. Shared frameworks minimize the size of both executables and make testing easier by ensuring that each executable uses the same code path.

• Use a shared container to store common resources. Put relevant images and data files into a shared container so your app and app extension can use them. You enable shared container support in the Capabilities tab of each target.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM