-
-
Save ctrleffive/3a46395a8b20215254d4ff5432bdb247 to your computer and use it in GitHub Desktop.
// ... | |
import io.flutter.app.FlutterActivity; | |
import io.flutter.plugin.common.MethodCall; | |
import io.flutter.plugin.common.MethodChannel; | |
import io.flutter.plugin.common.MethodChannel.MethodCallHandler; | |
import io.flutter.plugin.common.MethodChannel.Result; | |
// ... | |
public class MainActivity extends FlutterActivity { | |
private static final String PLATFORM_CHANNEL = "elaenor.inometrics.com/platform_channel"; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
new MethodChannel(getFlutterView(), PLATFORM_CHANNEL).setMethodCallHandler( | |
new MethodCallHandler() { | |
@Override | |
public void onMethodCall(MethodCall call, Result result) { | |
// if (call.method.equals("demoFunction")) { // INFO: method check | |
// String argument = call.argument("data"); // INFO: get arguments | |
// demoFunction(result, argument); // INFO: method call, every method call should pass result parameter | |
// } else | |
if (call.method.equals("getNetworks")) { | |
getNetworks(result); | |
} | |
else if (call.method.equals("connectNetwork")) { | |
String ssid = call.argument("ssid"); | |
String password = call.argument("password"); | |
connectNetwork(result, ssid, password); | |
} | |
else { | |
result.notImplemented(); // INFO: not implemented | |
} | |
} | |
} | |
); | |
GeneratedPluginRegistrant.registerWith(this); | |
} | |
public void demoFunction(Result result, String data) { | |
// INFO: function implementation | |
if (true) { // INFO: check for some condition | |
result.success("android call success with data " + data); // INFO: success response should return through this method | |
} else { | |
result.error("ERROR", "Error message description!", null); // INFO: error response should return through this method | |
} | |
} | |
} |
import 'dart:async'; | |
import 'package:flutter/services.dart'; | |
const PLATFORM_CHANNEL = const MethodChannel('<APP_BUNDLE_NAME>/platform_channel'); | |
// you can use whatever you like. but make sure you use the same channel string in native code also | |
// platform channel method calling | |
Future<Null> demoFunction(BuildContext context) async { | |
try { | |
final String result = await PLATFORM_CHANNEL.invokeMethod( | |
'demoFunction', // call the native function | |
<String, dynamic> { // data to be passed to the function | |
'data': 'sample data', | |
} | |
); | |
// result hold the response from plaform calls | |
} on PlatformException catch (error) { // handle error | |
print('Error: $error'); // here | |
} | |
} |
// ... | |
@UIApplicationMain | |
@objc class AppDelegate: FlutterAppDelegate { | |
override func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { | |
GeneratedPluginRegistrant.register(with: self) | |
let controller : FlutterViewController = window?.rootViewController as! FlutterViewController; | |
let PLATFORM_CHANNEL = FlutterMethodChannel.init(name: "elaenor.inometrics.com/platform_channel", binaryMessenger: controller); | |
PLATFORM_CHANNEL.setMethodCallHandler({ | |
(call: FlutterMethodCall, result: FlutterResult) -> Void in | |
// if ("demoFunction" == call.method) { // INFO: method check | |
// let arguments = call.arguments as! NSDictionary // INFO: get arguments | |
// demoFunction(result: result, data: arguments["data"] as! String) // INFO: method call, every method call should pass result parameter | |
// } else | |
if ("getNetworks" == call.method) { | |
getNetworks(result: result) // INFO: method call | |
} | |
else if ("connectNetwork" == call.method) { | |
// Connect to a network with provided SSID & Password | |
// This will not work, because we have no permission. So always return an error message. | |
result(FlutterError.init( | |
code: "NO_PERMISSION", | |
message: "Auto connect not possible in iOS devices! Please connect to network manually.", | |
details: nil | |
)) | |
} | |
else { | |
result(FlutterMethodNotImplemented) | |
} | |
}) | |
return super.application(application, didFinishLaunchingWithOptions: launchOptions) | |
} | |
} | |
func demoFunction(result: FlutterResult, data: String) { | |
// INFO: function implementation | |
if (true) { // INFO: check for some condition | |
result("ios call success with data \(data)") // INFO: success response should return through this method | |
} else { | |
result(FlutterError.init( | |
code: "ERROR", | |
message: "Error message description!", | |
details: nil | |
)) // INFO: error response should return through this method | |
} | |
} |
Can you please have an example for ios objective-c. I stuck in one issue as it shows error "Use of undeclared identifier 'controller'".
Here is my code:
-
(BOOL)application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary )launchOptions {
[GeneratedPluginRegistrant registerWithRegistry:self];
FlutterMethodChannel batteryChannel = [FlutterMethodChannel
methodChannelWithName:@"foo"
binaryMessenger: controller];[batteryChannel setMethodCallHandler:^(FlutterMethodCall* call, FlutterResult result) {
// TODO
}];
return [super application:application didFinishLaunchingWithOptions:launchOptions];
}
Can you please have an example for ios objective-c. I stuck in one issue as it shows error "Use of undeclared identifier 'controller'".
Here is my code:
The FlutterViewController instance must be created before the FlutterMethodChannel:
FlutterViewController* controller = (FlutterViewController*)self.window.rootViewController;
Check the original example
Now, we need to use binaryMesssenger of the controller:
let PLATFORM_CHANNEL = FlutterMethodChannel.init(name: "elaenor.inometrics.com/platform_channel", binaryMessenger: controller.binaryMessenger);
hey @deremakif, thanks for letting me know about this. this gist is an old one. ill update it.
Hey can you please suggest me how to get the location using background service in flutter dart