2016-01-28 46 views
-1

我正在開發一個iOS應用程序,我希望所有具有iOS 7的手機都能夠使用該應用程序。當我將它設置爲iOS 7時,有三種方法無法使用,因爲它們僅適用於iOS 8和更新版本。有誰知道舊的方法。這裏是有錯誤的代碼...需要iOS 7的相同方法

let settings = UIUserNotificationSettings.init(forTypes: [.Sound, .Alert, .Badge], categories: nil) //Error: UIserNotificationSettings is only available on iOS 8 and newer 
application.registerUserNotificationSettings(settings) //Error: registerUserNotificationSettings is only available on iOS 8 and newer 

application.registerForRemoteNotifications() //Error: registerForRemoteNotifications is only available on iOS 8 and newer 
+0

這可能會指導您http://corinnekrych.blogspot.mx/2014/07/how-to-support-push-notification-for.html –

回答

1

如果您使用SWIFT 2.0,用於檢查操作系統版本的新正確的方法是這樣的:

if #available(iOS 9, *) { 
    // do iOS 9 stuff here 
} else { 
    // do pre iOS 9 stuff here 
} 

所以在您的特定情況下,你會希望這樣的事情。

if #available(iOS 8, *) { 
    let settings = UIUserNotificationSettings.init(forTypes: [.Sound, .Alert, .Badge], categories: nil) 
    UIApplication.sharedApplication().registerUserNotificationSettings(settings) 
    UIApplication.sharedApplication().registerForRemoteNotifications() 
} else { 
    UIApplication.sharedApplication().registerForRemoteNotificationTypes([.Sound, .Alert, .Badge]) 
} 

此外,如果您的最低部署目標是比你檢查什麼(部署9,但檢查,對8)編譯器會警告你檢查是多餘的了。

0

我發現如何得到它從http://corinnekrych.blogspot.com.au/2014/07/how-to-support-push-notification-for.html工作,並轉換成斯威夫特:

if application.respondsToSelector("registerUserNotificationSettings:") { 
    let settings = UIUserNotificationSettings.init(forTypes: [.Sound, .Alert, .Badge], categories: nil) 
    UIApplication.sharedApplication().registerUserNotificationSettings(settings) 
    UIApplication.sharedApplication().registerForRemoteNotifications() 
} else { 
    UIApplication.sharedApplication().registerForRemoteNotificationTypes([.Sound, .Alert, .Badge]) 
} 
+0

感謝您的回答,但方法仍然不適用於iOS 7 –

+0

如果你沒有弄明白,代碼檢查是否存在iOS 8+方法。如果是,請運行所需的代碼,否則運行舊代碼。 ' - (void)registerForRemoteNotificationTypes:'被標記爲不適用於iOS 8+。 – George

+0

請參閱[這裏](https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIApplication_Class/#//apple_ref/occ/instm/UIApplication/registerForRemoteNotificationTypes :) – George

0
// Check to see if this is an iOS 8 device. 
let iOS8 = floor(NSFoundationVersionNumber) > floor(NSFoundationVersionNumber_iOS_7_1) 
if iOS8 { 
    // Register for push in iOS 8 
    let settings = UIUserNotificationSettings(forTypes: UIUserNotificationType.Alert | UIUserNotificationType.Badge | UIUserNotificationType.Sound, categories: nil) 
    UIApplication.sharedApplication().registerUserNotificationSettings(settings) 
    UIApplication.sharedApplication().registerForRemoteNotifications() 
} else {   
    // Register for push in iOS 7 
    UIApplication.sharedApplication().registerForRemoteNotificationTypes(UIRemoteNotificationType.Badge | UIRemoteNotificationType.Sound | UIRemoteNotificationType.Alert) 
} 

這是否對你的工作?

相關問題