首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >类型UNUserNotificationCenter没有成员当前,swift 2.3

类型UNUserNotificationCenter没有成员当前,swift 2.3
EN

Stack Overflow用户
提问于 2016-08-03 20:25:46
回答 4查看 3.3K关注 0票数 1

在iOS10中,苹果对用户通知进行了修改。现在我正在尝试调整我的应用程序以适应这些变化,遵循this

代码语言:javascript
复制
let center = UNUserNotificationCenter.current()

给我一个错误:

代码语言:javascript
复制
Type UNUserNotificationCenter has no member current, swift 2.3

我知道该教程可能是为swift3编写的。但我需要让它在swift 2.3中工作。这是可行的吗?如果可行,怎么做?

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2016-08-03 20:33:24

文档似乎与其本身相冲突。尽管它描述了current()方法并说明要使用它,但示例中显示的是let center = UNUserNotificationCenter.currentNotificationCenter()

正如您所说,这可能是Swift版本的依赖项。

票数 3
EN

Stack Overflow用户

发布于 2016-08-18 17:00:41

对于Xcode 8/ ios9/10

只需使用:

代码语言:javascript
复制
import  UserNotifications

..。..。

代码语言:javascript
复制
    // swift 3.0:
    if #available(iOS 10.0, *) {
        let center = UNUserNotificationCenter.current()
        center.requestAuthorization(options: [.alert, .sound]) { (granted, error) in
            // Enable or disable features based on authorization.
        }

    } else {
        // Fallback on earlier versions
    }

//注意:如果你在前台,你永远不会被调用!:)

票数 7
EN

Stack Overflow用户

发布于 2018-11-20 06:46:35

导入以下内容:

代码语言:javascript
复制
import  UserNotifications

appDelegate文件中使用实现此委托:

代码语言:javascript
复制
UNUserNotificationCenterDelegate

这将使您能够访问以下方法:

代码语言:javascript
复制
func userNotificationCenter(_ center: UNUserNotificationCenter, openSettingsFor notification: UNNotification?) {}

func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {}

func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {}

然后将其添加到您的应用程序委托的didFinishLaunchWithOptions方法中,以将其全部绑定在一起。

代码语言:javascript
复制
if #available(iOS 10.0, *) {
    // For iOS 10 display notification (sent via APNS)
    UNUserNotificationCenter.current().delegate = self

    let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound]
    UNUserNotificationCenter.current().requestAuthorization(
        options: authOptions,
        completionHandler: {_, _ in })
} else {
    let settings: UIUserNotificationSettings =
        UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
    application.registerUserNotificationSettings(settings)
}
application.registerForRemoteNotifications()
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38743628

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档