loo*_*139 3 plugins ios flutter
我一直在关注Flutter Workmanager iOS 设置,在页面底部,他们展示了如何将插件与 Workmanager 一起使用来执行后台任务。
我的 AppDelegate.swift 中有这个确切的代码:
class AppDelegate: FlutterAppDelegate {
/// Registers all pubspec-referenced Flutter plugins in the given registry.
static func registerPlugins(with registry: FlutterPluginRegistry) {
GeneratedPluginRegistrant.register(with: registry)
}
override func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// ... Initialization code
AppDelegate.registerPlugins(with: self) // Register the app's plugins in the context of a normal run
WorkmanagerPlugin.setPluginRegistrantCallback { registry in
// The following code will be called upon WorkmanagerPlugin's registration.
// Note : all of the app's plugins may not be required in this context ;
// instead of using GeneratedPluginRegistrant.register(with: registry),
// you may want to register only specific plugins.
AppDelegate.registerPlugins(with: registry)
}
}
}
Run Code Online (Sandbox Code Playgroud)
他们最后的评论意味着我可以注册特定的插件,我想要这样做,而不是加载我已安装的每个插件。
我尝试在.setPluginRegistrantCallback括号内做一些事情,例如:
GeolocatorPlugin.register(with: registry.registrar(forPlugin: "GeolocatorPlugin"))
Run Code Online (Sandbox Code Playgroud)
但这似乎不起作用,因为在我在 xCode 中模拟后台获取后,Workmanager 不断返回失败结果:
[<workmanager.SwiftWorkmanagerPlugin: 0x600003b5b060>] application(_:performFetchWithCompletionHandler:) -> UIBackgroundFetchResult.failed (finished in 0.24 seconds)
Run Code Online (Sandbox Code Playgroud)
如果有人可以帮助我注册一个特定的插件,例如 Geolocator 或其他任何东西,那就太好了。而不是通过GeneratedPluginRegistrant.
几天后我找到了自己的答案,以防有人需要解决方案。
为了向 Workmanager 注册特定插件,您需要找到该插件的“完全限定名称”。您可以在这里查看插件注册参数的说明。
为了找到完全限定的名称,请访问您的 Android,GeneratedPluginRegistrant.java您将在其中看到一些 Flutter 引擎的注册。它将如下所示:
flutterEngine.getPlugins().add(new io.flutter.plugins.googlemaps.GoogleMapsPlugin());
flutterEngine.getPlugins().add(new vn.hunghd.flutter.plugins.imagecropper.ImageCropperPlugin());
flutterEngine.getPlugins().add(new io.flutter.plugins.imagepicker.ImagePickerPlugin());
flutterEngine.getPlugins().add(new com.baseflow.location_permissions.LocationPermissionsPlugin());
Run Code Online (Sandbox Code Playgroud)
可能有更多或更少,具体取决于您有多少插件。然而,这些部分:io.flutter.plugins.firebaseadmob.FirebaseAdMobPlugin或vn.hunghd.flutter.plugins.imagecropper.ImageCropperPlugin是您将用于在 iOS 上注册特定插件的完全限定名称。
然后,在 AppDelegate.swift 文件中的Workmanager.setPluginRegistrantCallback括号内,您可以添加插件,如下所示:
WorkmanagerPlugin.setPluginRegistrantCallback { registry in
GeolocatorPlugin.register(with: registry.registrar(forPlugin: "
com.baseflow.geolocator.GeolocatorPlugin"))
LocationPermissionsPlugin.register(with: registry.registrar(forPlugin: "
com.baseflow.location_permissions.LocationPermissionsPlugin"))
FLTCloudFirestorePlugin.register(with: registry.registrar(forPlugin: "
io.flutter.plguins.firebase.cloudfirestore.CloudFirestorePlugin"))
}
Run Code Online (Sandbox Code Playgroud)
这会将这些特定插件注册到在后台运行的 Dart Isolate,以便您可以在应用程序隐藏时使用地理定位器或查询数据库。基本上,无论您想在 Workmanager 隔离区或 iOS 上您自己的 Dart 隔离区中运行什么。
请务必在 AppDelegate.swift 文件的顶部包含插件和 Workmanager 本身的导入语句。这是我的这 3 个插件的样子:
import UIKit
import Flutter
import GoogleMaps
import workmanager
import location_permissions
import geolocator
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1905 次 |
| 最近记录: |