【问题标题】:Conditional cast from 'AppDelegate' to 'UNUserNotificationCenterDelegate' always succeeds (Flutter)从“AppDelegate”到“UNUserNotificationCenterDelegate”的条件转换总是成功(颤振)
【发布时间】:2020-11-24 18:28:24
【问题描述】:

我尝试连接 iPhone 来构建我的颤振项目,但是出现了这个错误:

Conditional cast from 'AppDelegate' to 'UNUserNotificationCenterDelegate' always succeeds

我猜它可能与本地通知有关,但不知道为什么会发生错误以及如何解决它。

AppDelegate.swift

import UIKit
import Flutter
import UserNotifications

@UIApplicationMain
@objc class AppDelegate: FlutterAppDelegate {
  override func application(
    _ application: UIApplication,
    didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
  ) -> Bool {
    if #available(iOS 10.0, *) {
      UNUserNotificationCenter.current().delegate = self as? UNUserNotificationCenterDelegate
    }
    GeneratedPluginRegistrant.register(with: self)
    return super.application(application, didFinishLaunchingWithOptions: launchOptions)
  }
}

【问题讨论】:

  • AppDelegate 继承自 FlutterAppDelegateFlutterAppLifeCycleProvider 兼容,UNUserNotificationCenterDelegate 兼容。所以你不应该需要演员:self as? UNUserNotificationCenterDelegate 应该是 self
  • @Larme 太棒了,非常感谢!

标签: ios swift flutter


【解决方案1】:

cmets 中提到的@Larme 是正确的。由于 AppDelegate 继承了 FlutterAppDelegate 类,因此无需转换 self (FlutterAppDelegate)。代码应该是

UNUserNotificationCenter.current().delegate = self

如果您使用的是 flutter_local_notifications 插件,您还可以尝试删除 ?,如他们的文档中提供的 sample 所示。

【讨论】:

    猜你喜欢
    • 2019-12-17
    • 2022-01-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多