2016-10-04 13 views
6

問題:私は、iOSの10にアプリのアイコンバッジ番号を設定しようとしていますiOS 10でアプリアイコンのバッジ番号を設定するにはどうすればよいですか?

しかし、それは失敗しています。私はUIUserNotificationSettingsがiOSで廃止され、UNNotificationSettingsがそれに取って代わることを理解しています。

質問:

どのように私はUNNotificationSettings への更新プログラムをiOSの10のアイコンバッジ番号を使用するには、以下のコードを変更しますか?それとも別の簡潔な方法がありますか?

コード:

次のコードは、私はiOSの7からバッジを設定する方法を示しています - iOSの9

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

ありがとうございます。はい、スウィフト3の答えが役に立つでしょう。しかし、それはiOS 10で廃止されていると考えているので面白いですか? https://developer.apple.com/reference/uikit/uiusernotificationsettings私は少し困惑しています。 – user4806509

答えて

4

あなたはAppDelegateUserNotificationsを実装する必要があります。

その後、
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

関連する問題