【问题标题】:Flutter Workmanager plugin implementation missing error occurs in iOSiOS 中出现 Flutter Workmanager 插件实现缺失错误
【发布时间】:2020-12-24 14:20:10
【问题描述】:

我正在尝试将 workmanager: ^0.2.0 插件用于颤振项目中的后台进程。在 iOS 中运行应用程序时,它会引发如下错误,但在 Android 中插件可以无缝运行

MissingPluginException(No implementation found for method initialize on channel be.tramckrijte.workmanager/foreground_channel_work_manager)
#0      MethodChannel.invokeMethod (package:flutter/src/services/platform_channel.dart:319:7)
<asynchronous suspension>
#1      Workmanager.initialize (package:workmanager/src/workmanager.dart:100:30)
#2      main (package:worksample/main.dart:29:15)

我在 AppDelegate.swift 代码中手动注册了 Workmanager 插件,但它仍然没有在 GeneratedPluginRegistrant.m 类文件中自动生成插件注册代码。

下面是我的代码sn-ps,

import UIKit
import Flutter
import workmanager

@UIApplicationMain
@objc class AppDelegate: FlutterAppDelegate {
  override func application(
    _ application: UIApplication,
    didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
  ) -> Bool {
    GeneratedPluginRegistrant.register(with: self)
    UIApplication.shared.setMinimumBackgroundFetchInterval(TimeInterval(60*15))
    UNUserNotificationCenter.current().delegate = self
    
    WorkmanagerPlugin.setPluginRegistrantCallback { registry in
        AppDelegate.registerPlugins(with: registry)
    }
    return super.application(application, didFinishLaunchingWithOptions: launchOptions)
  }
    
    static func registerPlugins(with registry: FlutterPluginRegistry) {
               GeneratedPluginRegistrant.register(with: registry)
          }
       
    override func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
         completionHandler(.alert) 
     }
    
}


以下生成的文件中缺少 WorkManager 插件

//
//  Generated file. Do not edit.
//

#import "GeneratedPluginRegistrant.h"

#if __has_include(<flutter_native_timezone/FlutterNativeTimezonePlugin.h>)
#import <flutter_native_timezone/FlutterNativeTimezonePlugin.h>
#else
@import flutter_native_timezone;
#endif

#if __has_include(<shared_preferences/FLTSharedPreferencesPlugin.h>)
#import <shared_preferences/FLTSharedPreferencesPlugin.h>
#else
@import shared_preferences;
#endif

@implementation GeneratedPluginRegistrant

+ (void)registerWithRegistry:(NSObject<FlutterPluginRegistry>*)registry {
  [FlutterNativeTimezonePlugin registerWithRegistrar:[registry registrarForPlugin:@"FlutterNativeTimezonePlugin"]];
  [FLTSharedPreferencesPlugin registerWithRegistrar:[registry registrarForPlugin:@"FLTSharedPreferencesPlugin"]];
}

@end

我该如何解决这个问题?

【问题讨论】:

    标签: ios flutter flutter-plugin


    【解决方案1】:

    通过在 AppDelegate.swift 类中添加以下更改解决了该问题。

    import UIKit
    import Flutter
    import workmanager
    import shared_preferences
    
    @UIApplicationMain
    @objc class AppDelegate: FlutterAppDelegate {
      override func application(
        _ application: UIApplication,
        didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
      ) -> Bool {
     
        
        GeneratedPluginRegistrant.register(with: self)
        
        WorkmanagerPlugin.register(with: self.registrar(forPlugin: "be.tramckrijte.workmanager.WorkmanagerPlugin"))
        
        UNUserNotificationCenter.current().delegate = self
    
        UIApplication.shared.setMinimumBackgroundFetchInterval(TimeInterval(60*15))
    
        
        WorkmanagerPlugin.setPluginRegistrantCallback { registry in
            AppDelegate.registerPlugins(with: registry)
             FLTSharedPreferencesPlugin.register(with: registry.registrar(forPlugin: "io.flutter.plugins.sharedpreferences.SharedPreferencesPlugin"))
            
           
               
        }
        return super.application(application, didFinishLaunchingWithOptions: launchOptions)
      }
        
        static func registerPlugins(with registry: FlutterPluginRegistry) {
                   GeneratedPluginRegistrant.register(with: registry)
              }
           
        override func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
             completionHandler(.alert) 
         }
        
    }
    
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-06-24
      • 1970-01-01
      • 2018-11-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-02-18
      • 1970-01-01
      相关资源
      最近更新 更多