Phil
Phil

Reputation: 351

How to integrate Siri into Flutter with MethodChannels?

I want to integrate Sirikit into my flutter application. Therefore I have to establish a MethodChannel for communication between the host and the client. I want the iOS-side to invoke a flutter function and that response be Siris answer. This is the flutter Side:

void nativeFunc() {
  const platform = const MethodChannel("flutter.siri");

  Future<dynamic> _handleMethod(MethodCall call) async {
    if (call.method == "message") {
      return api_request("Physics", call.arguments, 50);
    }
  }

  platform.setMethodCallHandler(_handleMethod);
}

Then I have to specify the methodChannel on the iOS-side:

//AppDelegate.swift
import UIKit
import Flutter

@UIApplicationMain
@objc class AppDelegate: FlutterAppDelegate {
  override func application(
    _ application: UIApplication,
    didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
  ) -> Bool {
    let controller : FlutterViewController = window?.rootViewController as! FlutterViewController
    let methodChannel = FlutterMethodChannel(name:"flutter.siri")

    
    GeneratedPluginRegistrant.register(with: self)
    return super.application(application, didFinishLaunchingWithOptions: launchOptions)
  }
}

//IntentHandler.swift
    func handle(intent: INSendMessageIntent, completion: @escaping (INSendMessageIntentResponse) -> Void) {
        // Implement your application logic to send a message here.
        
        let userActivity = NSUserActivity(activityType: NSStringFromClass(INSendMessageIntent.self))
        let response = methodChannel.invokeMethod()//this should invoke the MethodChannel-function from Flutter
        
        
        completion(response)
    }

I am no Ios-developer so I have no clue how to actually do this. Right now the problem is that the IntentHandler.swift-File doesn't have access to the method channel data because it was declared in another file. How can my Intenthandler-file call the flutter method and respond with Siri?

Upvotes: 2

Views: 1248

Answers (1)

Andres Sosa Martinez
Andres Sosa Martinez

Reputation: 149

I was facing a similar issue and I was able to make it work.

The problem is that Siri intents are in a different Target because they can run in the background if needed and because of this your communication using Method channels would be really complicated to implement (if possible).

My approach was to use the flutter extension HomeWidget https://pub.dev/packages/home_widget

Since Home widgets also use a different target and This extension opens a communication channel using App groups and UserDefaults we could use the same communication channel to send data to our Siri Intent.

Just follow the configuration guide of the HomeWidget extension and add AppGroups capabilities to your runner Target and also to your Siri intent. make sure you use the same group ID in all of them including on the flutter side:

HomeWidget.setAppGroupId('YOUR_GROUP_ID');

Then you can use the extension methods to send and receive data between flutter and swift.

Upvotes: 4

Related Questions