【问题标题】:UNUserNotificationCenter/ UserNotification not found in Xcode 11 iOS13 -在 Xcode 11 iOS13 中找不到 UNUserNotificationCenter/UserNotification -
【发布时间】:2019-10-01 03:39:05
【问题描述】:

我正在尝试在 iOS13 中的 Xcode 11 上设置通知,但我尝试添加链接框架(在图片中)但仍然无法识别模块。有什么建议吗?

import UIKit
import Firebase
import UNUserNotificationCenter


@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.
        FirebaseApp.configure()
        let center = UNUserNotificationCenter.current()
        // Request permission to display alerts and play sounds.
        center.requestAuthorization(options: [.alert, .sound])
           { (granted, error) in
              // Enable or disable features based on authorization.
           }
        return true
    }

【问题讨论】:

  • 请勿将代码发布为图片。请用文字替换图片(与您的答案相同)。
  • 更新@rmaddy

标签: swift xcode11 unusernotificationcenter


【解决方案1】:

改变

import UNUserNotificationCenter

import UserNotifications

【讨论】:

  • 另外,您不需要像现在这样手动链接框架;删除所有这些。 import 带有模块名称会自动链接您。
  • 我试过 UserNotifications 但它也没有识别它,它提供的唯一一个自动完成是 UNUserNoticaitionCenter 但仍然说它不识别它
  • 我向你保证import UserNotifications 有效。
  • 好的,但是我怎样才能让它显示或识别为一个模块,因为它一直说找不到模块?
【解决方案2】:

为了解决这个问题,我必须删除 import UNUserNotifications 并编写下面的代码。不知道为什么,但 import UserNotfications 对我不起作用。如果添加了链接框架,请删除。

我在 Apples 网站上也找到了修复程序 https://developer.apple.com/documentation/usernotifications/asking_permission_to_use_notifications

import UIKit
import Firebase


@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.
        FirebaseApp.configure()
        let center = UNUserNotificationCenter.current()
        // Request permission to display alerts and play sounds.
        center.requestAuthorization(options: [.alert, .sound])
           { (granted, error) in
              // Enable or disable features based on authorization.
           }
        return true
    }

【讨论】:

  • 那是不可能的。如果你在代码中说“UNUserNotificationCenter”,除非你导入 UserNotifications,否则它不会编译。另外,这段代码与您之前的代码有何不同?
  • 我知道它为什么会起作用,但这是我可以构建并让它成功运行的唯一方法。我删除了 UNUserNotificationCenter 的导入,并按代码所示调用它。甚至在我发布链接的苹果网站上也提到过
  • 苹果关于UNUserNotificationCenter的网站:developer.apple.com/documentation/usernotifications/…
【解决方案3】:

刚刚重试,现在 Xcode 仅使用 UserNotfications 即可正常工作,但现在无法识别 UNUserNotificationCenter

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-03-15
    • 2019-11-23
    • 2020-08-14
    • 1970-01-01
    • 1970-01-01
    • 2013-01-28
    • 2020-12-27
    相关资源
    最近更新 更多