iOS implementation
- Flutter course
- Expert
- Lesson 19/39
- 4 min
Write plugin methods on the iOS side.
iOS implementation
On the iOS side, a plugin is a Swift class implementing the FlutterPlugin protocol. The handle(_ call: FlutterMethodCall) method catches incoming calls from Dart.
class MyPluginiOS: NSObject, FlutterPlugin {
func handle(_ call: FlutterMethodCall, result: @escaping FlutterResult) {
if call.method == "getVersion" {
result("1.0")
} else {
result(FlutterMethodNotImplemented)
}
}
}Swift and FlutterPlugin
A plugin written in Swift extends NSObject and implements the FlutterPlugin protocol. FlutterMethodCall.method gives the call name; the reply returns to Dart via FlutterResult.
Practice IOSPlugin simulation
Define a Dart class named IOSPlugin. Its handle(String method) method returns a String: '1.0' if method == 'getVersion', otherwise 'unknown'. In main, create an IOSPlugin instance and print the result of handle('getVersion').
Starter code
// IOSPlugin sınıfını tanımla
// handle metodu String alıp String döndürsün
// version çağrısı için 1.0, diğerleri için unknown döndürsün
void main() {
// IOSPlugin örneği al ve version çağrısının sonucunu yazdır
}Sample solution
class IOSPlugin {
String handle(String method) {
if (method == 'getVersion') {
return '1.0';
}
return 'unknown';
}
}
void main() {
final plugin = IOSPlugin();
print(plugin.handle('getVersion'));
}Expected output
1.0
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 Which method catches calls from Dart on the iOS side?
- viewDidLoad
- handle(_ call:) Correct answer
- onClick
- build
Explanation: The handle(_ call:) method of the FlutterPlugin protocol receives FlutterMethodCalls from Dart; build is for widgets, viewDidLoad for view controllers, and onClick for view events.
2 Swift method comparison
if call.method == "____" {
if call.method == "getVersion" {
Explanation: On the iOS side, call.method is compared to "getVersion"; version is a different name, and init and dispose are lifecycle methods, not call names.
3 iOS implementation matches
| Term | Meaning |
|---|---|
iOS |
Apple platform |
FlutterPlugin |
iOS protocol |
Swift |
iOS programming language |
handle |
Call capture |