How to integrate Siri into Flutter with MethodChannels? - ios

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?

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.

Related

Platform Method Channel From iOS Side to Flutter

so i have an issue when trying to implement the platform method channel for my flutter project, When i try to invoke a method from flutter to ios side, it gets triggered and everything is working perfectly but when i try to invoke a method from ios side (appDelegate file) to flutter in order to perform a sepecific task, it's not working.
AppDelegate.swift Code:
#UIApplicationMain
#objc class AppDelegate: FlutterAppDelegate {
override func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
) -> Bool {
FirebaseApp.configure()
application.registerForRemoteNotifications()
GeneratedPluginRegistrant.register(with: self)
let controller = (window?.rootViewController as! FlutterViewController)
let methodChannle = FlutterMethodChannel(name: "channelMethodTest", binaryMessenger: controller.binaryMessenger)
methodChannle.invokeMethod("taskName", arguments: [:])
return super.application(application, didFinishLaunchingWithOptions: launchOptions)
}
Flutter main.dart:
const methodChannel = MethodChannel('channelMethodTest');
Future<void> methodHandler(MethodCall call) async {
final String idea = call.arguments;
switch (call.method) {
case "taskName":
print(
"receiving task from ios to flutter");
break;
default:
print('no method handler for method ${call.method}');
}
}
Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();
methodChannel.setMethodCallHandler(methodHandler);
}
I tried searching for tutos on how to do it but i cannot find any
you need to make the channelmethod var global then you need to instantiate the method channel in the didFinishLaunchingWithOptions func if you're using the method channel to invoke any method in the app deletage or any extension related to it.
Ensure that the channel name is the same in Flutter and iOS side. Now they seem to be different:
let methodChannle = FlutterMethodChannel(name: "channelMethodTest", binaryMessenger: controller.binaryMessenger)
const methodChannel = MethodChannel('beyang.com-channelMethodTest');

flutter remote push notification(fcm) not working IOS

android works well
2.from firebase site to iOS real device works well (below this push to ios is ok)
3. remote push from iOS to andriod works well
but remote push message to iOS not working
my Xcode
my info
my Appdelegate.swift
import UIKit
import Flutter
import Firebase
#UIApplicationMain
#objc class AppDelegate: FlutterAppDelegate {
override func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
) -> Bool {
FirebaseApp.configure() //add this before the code below
GeneratedPluginRegistrant.register(with: self)
return super.application(application, didFinishLaunchingWithOptions: launchOptions)
}
}
Actually I had faced this issue , after long research realised that I haven't permit permission for notification, so used permission_handler plugin to ask permission for remote notification .
like this:
Future<Map<Permission, PermissionStatus>> requestPermission() async {
Map<Permission, PermissionStatus> statuses = await [
Permission.notification
].request();
return statuses;
}
Uninstall the application
add above code
Call this function at starting
give permission

Flutter : How to disable screenshot on iOS devices or just detect the action of screenshot

My problem is that I'am currently stuck and I can't block the screenshots on iOS devices in my flutter app even if I use this solution :
https://pub.dev/packages/screenshot_callback this package is depricated and I can't use it.
The plugin `screenshot_callback` uses a deprecated version of the Android embedding.
To avoid unexpected runtime failures, or future build failures, try to see if this plugin supports the Android V2 embedding. Otherwise, consider removing it since a future release of Flutter will remove these deprecated APIs.
If you are plugin author, take a look at the docs for migrating the plugin to the V2 embedding: https://flutter.dev/go/android-plugin-migration.
and even adding a swift code doesn't work :
import UIKit
import Flutter
#UIApplicationMain
#objc class AppDelegate: FlutterAppDelegate {
override func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
) -> Bool {
GeneratedPluginRegistrant.register(with: self)
return super.application(application, didFinishLaunchingWithOptions: launchOptions)
}
// <Add>
override func applicationWillResignActive(
_ application: UIApplication
) {
self.window.isHidden = true;
}
override func applicationDidBecomeActive(
_ application: UIApplication
) {
self.window.isHidden = false;
}
}
Please let me know if you have any solutions to fix it :)

How to use Firebase Remote Config in AppDelegate.swift?

Based on advice, I would like to manage API keys using Firebase Remote Config to avoid hard-coding API keys like google_maps_flutter suggests. It has an AppDelegate.swift like:
import UIKit
import Flutter
import GoogleMaps
#UIApplicationMain
#objc class AppDelegate: FlutterAppDelegate {
override func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
) -> Bool {
GMSServices.provideAPIKey("YOUR KEY HERE")
GeneratedPluginRegistrant.register(with: self)
return super.application(application, didFinishLaunchingWithOptions: launchOptions)
}
}
How can the above be modified to fetch the API key from Firebase Remote Config and then pass it to GMSServices?
Based on this article, I came up with:
import UIKit
import Firebase
import Flutter
import GoogleMaps
//import os.log
#UIApplicationMain
#objc class AppDelegate: FlutterAppDelegate {
override func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
) -> Bool {
FirebaseApp.configure()
RemoteConfig.remoteConfig().fetchAndActivate() { status, error in
let apiKey : String = RemoteConfig.remoteConfig()["Google_Maps_SDK_for_iOS_API_KEY"].stringValue ?? "MISSING";
// os_log("Google_Maps_SDK_for_iOS_API_KEY = '%#'", apiKey)
GMSServices.provideAPIKey(apiKey)
}
GeneratedPluginRegistrant.register(with: self)
return super.application(application, didFinishLaunchingWithOptions: launchOptions)
}
}
Is there a better way?
I was thinking of making this as a comment, but decided to make it as my answer as it was too long.
I think it's one way of getting keys from remoteConfig, but again failure can happen where fetch config was not retrieved. One way to solve it is by having a force refresh when it fails, but then again you are relying on firebase for those keys and if for some reason (experienced it before firebase was acquired by google) it went down your app then will be unuseable (so as for many apps).
For me, I still put my API keys bundled with the app just to make sure all the important functionality works.
Another option will be having your API Keys bundled and then having a WebService call to check for new keys once the current keys you have has expired/change. That way you have the capability to immediately expire your keys and change it to another one.

Handling tel: links in voip app

Is there any method in iOS (CallKit? perhaps) where a VoIP app can register to handle tel: links? That way when a user selects a phone number (in safari for instance). They would be presented with two options to complete the call.
This is supported in iOS these days if you press and hold on a tel: link. It doesn't use the standard URI scheme system, though. CallKit seems to automatically register your app as a handler of tel: links if you declare support for phone calls, and the link is passed through the following event:
import Intents
protocol SupportedStartCallIntent {
var contacts: [INPerson]? { get }
}
extension INStartAudioCallIntent: SupportedStartCallIntent {}
extension INStartVideoCallIntent: SupportedStartCallIntent {}
#UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, CXProviderDelegate {
func application(_ application: UIApplication, continue userActivity: NSUserActivity, restorationHandler: #escaping ([Any]?) -> Void) -> Bool {
let interaction = userActivity.interaction
let startCallIntent = interaction?.intent as? SupportedStartCallIntent
if let contact = startCallIntent?.contacts?.first?.displayName {
// do what you want with 'contact'
}
return true
}
That capability does not exist in iOS today. If you are interested in that, I recommend filing a bug report to request it on Apple's Bug Report website.

Resources