To schedule a local notification in Swift, you need to use the UserNotifications framework.
First, import the framework in your ViewController or AppDelegate:
main.swift25 chars2 lines
Then, request authorization to display notifications:
main.swift150 chars4 lines
To schedule a notification, create a UNMutableNotificationContent object with the details of your notification (e.g. title, body, sound):
main.swift179 chars5 lines
Next, create a UNCalendarNotificationTrigger or UNTimeIntervalNotificationTrigger object to define when your notification should be triggered. For example, to trigger a notification 30 seconds after scheduling, you could use:
main.swift82 chars2 lines
To schedule the notification, create a UNNotificationRequest with your content and trigger, and add it to the notification center:
main.swift218 chars5 lines
That's it! Your notification should now be scheduled and will trigger at the specified time.
gistlibby LogSnag