ios - 如何在我的 AppDelegate.swift 中向 Workmanager 注册 "specific plug-in"?

标签 ios flutter plugins

我一直在关注 Flutter Workmanager iOS setup并在页面底部展示了如何将插件与 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)
        }
    }
}

他们最后的评论暗示我可以注册特定的插件,我想这样做而不是加载我安装的每个插件。

我尝试在 .setPluginRegistrantCallback 括号内做一些事情,例如:

GeolocatorPlugin.register(with: registry.registrar(forPlugin: "GeolocatorPlugin"))

但这似乎不起作用,因为在我在 xCode 中模拟后台获取后,Workmanager 不断返回失败结果:

[<workmanager.SwiftWorkmanagerPlugin: 0x600003b5b060>] application(_:performFetchWithCompletionHandler:) -> UIBackgroundFetchResult.failed (finished in 0.24 seconds)

如果有人可以帮我注册一个特定的插件,比如 Geolocator 或其他任何插件,那就太好了。而不是通过 GeneratedPluginRegistrant 注册所有插件。

最佳答案

几天后我想出了自己的答案,以防有人需要解决方案。

为了将特定插件注册到 Workmanager,您需要找到该插件的“完全限定名称”。可以看插件注册的参数说明here .

为了找到完全限定的名称,请访问您的 Android 的 GeneratedPluginRegistrant.java,您会在其中看到一些对 Flutter Engine 的注册。这是它的样子:

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());

根据您拥有的插件数量,可能会增加或减少。但是,这些部分:io.flutter.plugins.firebaseadmob.FirebaseAdMobPluginvn.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"))
    }

这会将那些特定的插件注册到在后台运行的 Dart Isolate 中,以便您可以在隐藏应用程序时使用地理定位器或查询数据库。基本上无论您想在 Workmanager isolate 或您自己的 iOS 上的 Dart isolate 中运行。

确保在 AppDelegate.swift 文件的顶部包含插件和 Workmanager 本身的导入语句。这是我使用这 3 个插件的样子:

import UIKit
import Flutter
import GoogleMaps
import workmanager
import location_permissions
import geolocator

关于ios - 如何在我的 AppDelegate.swift 中向 Workmanager 注册 "specific plug-in"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62767194/

相关文章:

ios - 在 iOS 中,我可以访问 iTunes 歌曲列表吗?

iphone - 架构 i386 的 undefined symbol - 在制作 UIButton 子类时

iOS应用程序基本路径

php - 自定义 Shopware6 路由给出 "405 Method Not Allowed"

javascript - 使用 jQuery/JavaScript 进行组合键

ios - 最佳实践 : exposing NSManagedObject in framework

fullscreen - Flutter如何进入全屏模式

flutter - 使用flutter http请求获取数据,并在屏幕上向下滚动以加载更多数据

flutter - 如何创建这个进度指示器?特别是我不知道如何制作Flow动画

eclipse - 如何使用键盘快捷键移动 Eclipse 选项卡?