2016-05-19 3 views
0

Я новичок в swift, я не знаю, как реализовать локальное уведомление. Я пробовал какой-то код, но он не совсем работает, поэтому любой может помочь реализовать локальное уведомление в iOS с помощью swift?Локальное уведомление с использованием ios с помощью swift

+2

Есть много учебников по Google для этого. Документация Apple Apple может объяснить вам, как это работает. –

+0

Взгляните на [этот пример Swift 3] (https://stackoverflow.com/a/45247943/4754881) для локальных уведомлений с помощью [UNUserNotificationCenter] (https://developer.apple.com/documentation/usernotifications/unusernotificationcenter) , –

ответ

3

Для этого есть много обучающих онлайн, вы могли бы просто Google.

Вот учебник: http://jamesonquave.com/blog/local-notifications-in-ios-8-with-swift-part-1/

Кроме того, здесь приведен пример локального уведомления:

let notification = UILocalNotification() 
notification.fireDate = date 
notification.alertBody = "Alert!" 
notification.alertAction = "open" 
notification.hasAction = true 
notification.userInfo = ["UUID": "reminderID" ] 
UIApplication.sharedApplication().scheduleLocalNotification(notification) 
26

Здесь я делюсь пример,

Регистрация для локального оповещения,

@IBAction func registerLocal(sender: AnyObject) { 
    let notificationSettings = UIUserNotificationSettings(forTypes: [.Alert, .Badge, .Sound], categories: nil) 
    UIApplication.sharedApplication().registerUserNotificationSettings(notificationSettings) 
} 

Расписание местного оповещения,

@IBAction func scheduleLocal(sender: AnyObject) { 
    let notification = UILocalNotification() 
    notification.fireDate = NSDate(timeIntervalSinceNow: 5) 
    notification.alertBody = "Hey you! Yeah you! Swipe to unlock!" 
    notification.alertAction = "be awesome!" 
    notification.soundName = UILocalNotificationDefaultSoundName 
    notification.userInfo = ["CustomField1": "w00t"] 
    UIApplication.sharedApplication().scheduleLocalNotification(notification) 


    guard let settings = UIApplication.sharedApplication().currentUserNotificationSettings() else { return } 

    if settings.types == .None { 
     let ac = UIAlertController(title: "Can't schedule", message: "Either we don't have permission to schedule notifications, or we haven't asked yet.", preferredStyle: .Alert) 
     ac.addAction(UIAlertAction(title: "OK", style: .Default, handler: nil)) 
     presentViewController(ac, animated: true, completion: nil) 
     return 
    } 

} 

Он отправит местное уведомление после 5 seconds.

Есть много учебных пособий, также доступных как Appcoda !! Просто Google это вы получите много

Надеются, что это поможет :)

Смежные вопросы