0

I'm trying to implement a simple headless dart module that can be run from an iOS function :

Here is the AppDelegate.swift :

import UIKit
import Flutter

@main
class AppDelegate: UIResponder, UIApplicationDelegate {



    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        
        call_dart_module();
        // Override point for customization after application launch.
        return true
    }

   

    func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration {
        return UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role)
    }

    func application(_ application: UIApplication, didDiscardSceneSessions sceneSessions: Set<UISceneSession>) {
    }
    
    func call_dart_module(){
        
        print("iOS part start");
        lazy var flutterEngine = FlutterEngine(name: "my flutter engine", project: nil,allowHeadlessExecution: true)
       
        flutterEngine.run();
        
        let methodChannel = FlutterMethodChannel(name: "com.test.shouldwork/ios", binaryMessenger: flutterEngine.binaryMessenger);
        
        
        
        methodChannel.setMethodCallHandler({
                 (call: FlutterMethodCall, result: @escaping FlutterResult) -> Void in
           
            if (call.method == "isReady") {
                     
                     print("isReady");
       
                 }
             })
    }
}

And here is my dart code :

main(){
  
    var platform = const MethodChannel('com.test.shouldwork/ios');
    print("start");
    WidgetsFlutterBinding.ensureInitialized();

    //platform.setMethodCallHandler(_receiveFromHost);
    try{
       platform.invokeMethod("isReady");
       print("worked");
      
    } catch (e){
      print('error');
    }
   
    print("end");
  
}

All the print calls from the dart files are called (appart from the error one), yet isReady from swift module handler is not printed. Is there any specific configuration to follow when using MethodChannel for an headless module ?

Thanks

Update : adding an infinite loop at the end of the call_dart_module() function seems to make it more stable as it was not called every time. Could the issue be related to the fact that the process is terminated or unavailble to actually process the call handler ?

aloys-c
  • 51
  • 5

1 Answers1

0

Unfortunately, the prints on the Swift side don't appear in Flutter/Dart console.

You can run the app on Xcode to see the prints.

  • Thanks for your answer, as it is an iOS app and not a flutter one, I am indeed running on xcode. All the prints are actually displayed but the one that would signal the "isReady" function has been called... – aloys-c Oct 08 '22 at 14:44