private void HandleMethodCall(FlutterMethodCall call, FlutterResult callback) { // Return an error if Flutter is invoking method calls through method channel // when bridge is configured for WebSocket communication if (Mode == FlutnetBridgeMode.WebSocket) { callback(ConstantsEx.FlutterMethodNotImplemented); return; } // Extract target method information from MethodCall.Method FlutnetMethodInfo methodInfo; NSObject dartReturnValue; try { methodInfo = JsonConvert.DeserializeObject <FlutnetMethodInfo>(call.Method, FlutterInterop.JsonSerializerSettings); dartReturnValue = FlutterInterop.ToMethodChannelResult(0); } catch (Exception ex) { callback(FlutterError.Create(FlutnetErrorCode.OperationNotImplemented.ToString(), ex.Message, null)); return; } // Send an empty - successful - response to immediately free Flutter thread callback(dartReturnValue); nint[] taskId = new nint[1]; taskId[0] = UIApplication.SharedApplication.BeginBackgroundTask(() => { FlutnetException error = new FlutnetException(FlutnetErrorCode.OperationCanceled); MainThread.BeginInvokeOnMainThread(() => SendError(methodInfo, error)); UIApplication.SharedApplication.EndBackgroundTask(taskId[0]); }); // Run the call in Background Task.Run(() => { BackgroundHandleMethodCall(methodInfo, call); UIApplication.SharedApplication.EndBackgroundTask(taskId[0]); }); }
private void HandleMethodCallTest(FlutterMethodCall call, FlutterResult callback) { if (call.Method == "FlutnetBridgeMode") { switch (Mode) { case FlutnetBridgeMode.PlatformChannel: callback(NSObject.FromObject("PlatformChannel")); break; case FlutnetBridgeMode.WebSocket: callback(NSObject.FromObject("WebSocket")); break; } } else { // Right now this handler is called just once at application startup // when Flutter module tries to detect if it's running // embedded into a native Xamarin app or as a standalone app callback(NSObject.FromObject("ok")); } }