2016-10-04 75 views
6

问题:如何在iOS 10中设置应用图标徽章号码?

我想设置在iOS版10的应用程序图标徽章数量,但它是失败的。据我所知,UIUserNotificationSettings现已在iOS中被弃用,UNNotificationSettings取而代之。

问:

如何修改下面的代码使用UNNotificationSettings到 更新图标徽章数量iOS中10?还是有另一种简洁的方法?

代码:

下面的代码显示了如何设置徽章从iOS的7 - 9 iOS版

let badgeCount: Int = 123 
let application = UIApplication.sharedApplication() 

if #available(iOS 7.0, *) { 
    application.applicationIconBadgeNumber = badgeCount 
} 

if #available(iOS 8.0, *) { 
    application.registerUserNotificationSettings(UIUserNotificationSettings(forTypes: [.Badge], categories: nil)) 
    application.applicationIconBadgeNumber = badgeCount 
} 

if #available(iOS 9.0, *) { 
    application.registerUserNotificationSettings(UIUserNotificationSettings(forTypes: [.Badge], categories: nil)) 
    application.applicationIconBadgeNumber = badgeCount 
} 

if #available(iOS 10.0, *) { 
    // ????? 
} 
+2

我使用的是一样的你的iOS9代码,它的工作对我iOS10物理设备和模拟器。 –

+1

但我想你正在寻找答案在迅速3? –

+0

谢谢。是的,Swift 3的答案会有帮助。但有趣的是,它在iOS 10上工作,考虑到它已被弃用? https://developer.apple.com/reference/uikit/uiusernotificationsettings现在我有点困惑。 – user4806509

回答

4

您需要实现UserNotificationsAppDelegate

import UserNotifications 

@UIApplicationMain 
class AppDelegate: UIResponder, UIApplicationDelegate { 

然后用下面的代码中didFinishLaunchingWithOptions

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { 

    UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge]) { (granted, error) in 
     if error != nil { 
      // 
     } 
    } 
    return true 

} 

Here你可以找到吨的重要的东西在通知话题。

对于徽章:

let content = UNMutableNotificationContent() 
content.badge = 10 // your badge count 
+0

谢谢@David Seek。我会放弃这一点。 – user4806509

+0

谢谢@David Seek,还没有到处去测试这个。我确实有另一个应用程序图标徽章类型的问题,您可能可以帮助解决的赏金。 http://stackoverflow.com/questions/39922580/voiceover-accessibility-label-and-hint-for-an-app-icon-badge-number – user4806509

相关问题