public class FlutterNativeTimezonePlugin: NSObject, FlutterPlugin {
public static func addManuallyToRegistry(registry: FlutterPluginRegistry) {
// https://api.flutter.dev/objcdoc/Protocols/FlutterPluginRegistry.html#/c:objc(pl)FlutterPluginRegistry(im)registrarForPlugin:
let registrar = registry.registrar(forPlugin: "flutter_native_timezone")
if let safeRegistrar = registrar {
register(with: safeRegistrar)
}
}
// this is an override of this fucnion:
// https://api.flutter.dev/objcdoc/Protocols/FlutterPlugin.html#/c:objc(pl)FlutterPlugin(cm)registerWithRegistrar:
public static func register(with registrar: FlutterPluginRegistrar) {
let channel = FlutterMethodChannel(name: "flutter_native_timezone", binaryMessenger: registrar.messenger())
let instance = FlutterNativeTimezonePlugin()
registrar.addMethodCallDelegate(instance, channel: channel)
}
public func handle(_ call: FlutterMethodCall, result: @escaping FlutterResult) {
switch call.method {
case "getLocalTimezone":
result(NSTimeZone.local.identifier)
case "getAvailableTimezones":
result(NSTimeZone.knownTimeZoneNames)
default:
result(FlutterMethodNotImplemented)
}
}
}
1条答案
按热度按时间u3r8eeie1#
是的,有一种方法(浏览和阅读Flutter iOS引擎文档后)。
AppDelegate.swift
(也是Swift语言)AppDelegate.swift
中创建插件(在AppDelegate
类下面)。这是为了简单起见。在我的例子中是:GeneratedPluginRegistrant.register(with: self)
方法下面添加插件初始化方法:FlutterNativeTimezonePlugin.addManuallyToRegistry(registry: self)
所以这是为iOS构建添加插件的手动方式。遗憾的是没有文档。