使用 Swift 在 iOS 8 中为应用程序图标添加徽章

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28011118/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 04:24:59  来源:igfitidea点击:

Add badge to app icon in iOS 8 with Swift

iosswifticonsbadge

提问by hans

I'd like to set a badge on the icon of my app like in apple's mail app (number on top of the icon). How can I do this in Swift (iOS8)?

我想在我的应用程序图标上设置一个徽章,就像在苹果的邮件应用程序中一样(图标顶部的数字)。我怎样才能在 Swift (iOS8) 中做到这一点?

采纳答案by ericgu

The "number on top of the icon" is called a Badge. Badges can be set on a number of things besides Application Icons including Navigation Bar toolbar icons.

“图标顶部的数字”称为徽章。除了应用程序图标(包括导航栏工具栏图标)之外,还可以在许多东西上设置徽章。

There are many ways to change the Application Icon Badge. Most use cases involve setting this when the Application is in the background to alert the user that there is some change that they may be interested in. This would involve a push notification.

有多种方法可以更改应用程序图标徽章。大多数用例都涉及在应用程序处于后台时设置此项以提醒用户他们可能感兴趣的某些更改。这将涉及推送通知。

For more on that see: https://developer.apple.com/library/archive/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/APNSOverview.html#//apple_ref/doc/uid/TP40008194-CH8-SW1

有关更多信息,请参见:https: //developer.apple.com/library/archive/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/APNSOverview.html#//apple_ref/doc/uid/TP40008194-CH8-SW1

However, you can also change it while your app is active. You will need permission from the user by registering the UserNotificationType. Once you get permission, you can change it to whatever number you wish.

但是,您也可以在应用程序处于活动状态时更改它。您需要通过注册 UserNotificationType 来获得用户的许可。获得许可后,您可以将其更改为您想要的任何数字。

application.registerUserNotificationSettings(UIUserNotificationSettings(forTypes: UIUserNotificationType.Sound | UIUserNotificationType.Alert |
        UIUserNotificationType.Badge, categories: nil
        ))

application.applicationIconBadgeNumber = 5

enter image description here

在此处输入图片说明

回答by David Seek

ericgu's answer seems to be outdated. looks like this -> | got replaced.

ericgu的回答似乎已经过时了。看起来像这样 -> | 被替换了。

Here is a working Swift 2code:

这是一个有效的Swift 2代码:

    let badgeCount: Int = 0
    let application = UIApplication.sharedApplication()
    application.registerUserNotificationSettings(UIUserNotificationSettings(forTypes: [.Badge, .Alert, .Sound], categories: nil))        
    application.applicationIconBadgeNumber = badgeCount

Edit: Swift 3:

编辑:斯威夫特3:

import UIKit
import UserNotifications

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        let badgeCount: Int = 10
        let application = UIApplication.shared
        let center = UNUserNotificationCenter.current()
        center.requestAuthorization(options:[.badge, .alert, .sound]) { (granted, error) in
            // Enable or disable features based on authorization.
        }
        application.registerForRemoteNotifications()
        application.applicationIconBadgeNumber = badgeCount
    }  
}

enter image description here

在此处输入图片说明

回答by Fattie

2019

2019年

The answer is simply

答案很简单

UIApplication.shared.applicationIconBadgeNumber = 777

Unfortunately though it will not workunless you ask for permission first.

不幸的是,除非您先征得许可,否则它不会起作用

To do that you simply:

要做到这一点,您只需:

UNUserNotificationCenter.current().requestAuthorization(options: .badge)
     { (granted, error) in
          if error == nil {
              // success!
          }
     }

回答by datayeah

For iOS10, Swift 3with backward-compatibility, you can wrap the best answers into a nice (static) utility function:

对于iOS10,具有向后兼容性的Swift 3,您可以将最佳答案包装到一个不错的(静态)实用程序函数中:

class func setBadgeIndicator(badgeCount: Int) {
    let application = UIApplication.shared
    if #available(iOS 10.0, *) {
      let center = UNUserNotificationCenter.current()
      center.requestAuthorization(options: [.badge, .alert, .sound]) { _, _ in }
    } else {
      application.registerUserNotificationSettings(UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil))
    }
    application.registerForRemoteNotifications()
    application.applicationIconBadgeNumber = badgeCount
  }