Skip to content

Commit 97aecc9

Browse files
committed
Add Day #73 Challenge ##
1 parent 759fd1f commit 97aecc9

1 file changed

Lines changed: 52 additions & 0 deletions

File tree

Project2/Project2/ViewController.swift

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
//
88

99
import UIKit
10+
import UserNotifications
1011

1112
class ViewController: UIViewController {
1213
@IBOutlet var button1: UIButton!
@@ -25,6 +26,7 @@ class ViewController: UIViewController {
2526

2627
override func viewDidLoad() {
2728
super.viewDidLoad()
29+
2830
navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Score", style: .plain, target: self, action: #selector(checkScore))
2931
// Do any additional setup after loading the view.
3032
countries += ["estonia", "france", "germany", "ireland", "italy", "monaco", "nigeria", "poland", "russia", "spain", "uk", "us"]
@@ -40,6 +42,56 @@ class ViewController: UIViewController {
4042
askQuestion()
4143
}
4244

45+
func manageNotifications() {
46+
let center = UNUserNotificationCenter.current()
47+
48+
center.getNotificationSettings { [weak self] settings in
49+
50+
if settings.authorizationStatus == .notDetermined {
51+
let ac = UIAlertController(title: "Daily reminder", message: "Allow notifications to remind u to play games", preferredStyle: .alert)
52+
ac.addAction(UIAlertAction(title: "Next", style: .default) { _ in
53+
self?.notificationsAuthorization()
54+
})
55+
self?.present(ac, animated: true)
56+
return
57+
} else if settings.authorizationStatus == .authorized {
58+
self?.scheduleNotifications()
59+
}
60+
61+
}
62+
}
63+
64+
func notificationsAuthorization() {
65+
let center = UNUserNotificationCenter.current()
66+
67+
center.requestAuthorization(options: [.alert, .badge, .sound]) { [weak self] granted, error in
68+
if granted {
69+
self?.scheduleNotifications()
70+
} else {
71+
print("She wasn't ready!")
72+
}
73+
}
74+
75+
}
76+
func scheduleNotifications() {
77+
let center = UNUserNotificationCenter.current()
78+
79+
center.removeAllPendingNotificationRequests()
80+
81+
let content = UNMutableNotificationContent()
82+
content.title = "Daily Reminder"
83+
content.body = "PLAY THE FLAG GAME."
84+
content.categoryIdentifier = "reminder"
85+
content.sound = .default
86+
87+
let secondsPerDay: TimeInterval = 3600 * 24
88+
for day in 1...7 {
89+
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: secondsPerDay * Double(day), repeats: false)
90+
let request = UNNotificationRequest(identifier: UUID().uuidString, content: content, trigger: trigger)
91+
center.add(request)
92+
}
93+
}
94+
4395
func askQuestion(action: UIAlertAction! = nil) {
4496
countries.shuffle()
4597

0 commit comments

Comments
 (0)