2014-10-04 34 views
4

我正在檢查以確保用戶已授權警報和徽章。一旦得到它,我就會遇到一個問題,想知道如何處理當前的設置。檢查通知設置iOS8

let settings = UIApplication.sharedApplication().currentUserNotificationSettings() 
println(settings) 
// Prints - <UIUserNotificationSettings: 0x7fd0a268bca0; types: (UIUserNotificationTypeAlert UIUserNotificationTypeBadge UIUserNotificationTypeSound);> 

if settings.types == UIUserNotificationType.Alert // NOPE - this is the line that needs an update 
{ 
    println("Yes, they have authorized Alert") 
} 
else 
{ 
    println("No, they have not authorized Alert. Explain to them how to set it.") 
} 

回答

7

您與==檢查是否所有設置選項都包含在您要比較其價值只會返回true。請記住,這是一個位圖枚舉,您可以通過按位或|將其他選項添加到相同的值。您可以通過按位&來檢查特定選項是否爲該值的一部分。

if settings.types & UIUserNotificationType.Alert != nil { 
    // .Alert is one of the valid options 
} 

在Swift 2.0+中,您需要使用新的符號。您的設置收集[UIUserNotificationType]類型的數組,所以你檢查這樣的:

if settings.types.contains(.Alert) { 
    // .Alert is one of the valid options 
} 
+0

謝謝。奇蹟般有效。 – ShadowDES 2014-10-04 23:52:25

+0

不幸的是,從Swift 2開始,這不起作用:/ – cph2117 2015-11-05 20:33:41