2016-10-04 102 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

相關問題