Code Block //import UIKit |
|
import SafariServices |
|
import UserNotifications |
|
class Home: UIViewController, UNUserNotificationCenterDelegate { |
|
@IBOutlet weak var leadingHome: NSLayoutConstraint! |
|
@IBOutlet weak var trailingHome: NSLayoutConstraint! |
|
|
|
var menuOut = false |
|
|
|
static var riddleNotification = Bool() |
|
override func viewDidLoad() { |
|
super.viewDidLoad() |
|
|
|
//Ask for Notification Permision |
|
let center = UNUserNotificationCenter.current() |
|
center.requestAuthorization(options: [.alert, .sound, .badge]) { (granted, error) in |
|
} |
|
|
|
|
|
func riddleNotification() { |
|
|
|
//Notification Content |
|
let content = UNMutableNotificationContent() |
|
content.title = "Check out this weeks riddle!" |
|
content.body = "This weeks riddle is..." |
|
content.categoryIdentifier = "riddle" |
|
|
|
//Notification Trigger |
|
let date = Date().addingTimeInterval(5) |
|
|
|
var dateComponents = DateComponents() |
|
//dateComponents.hour = 9 |
|
//dateComponents.minute = 30 |
|
//dateComponents.weekday = 4 //Wednesday |
|
let trigger = UNCalendarNotificationTrigger(dateMatching: dateComponents, repeats: true) |
|
|
|
//Create Request |
|
|
|
let uuidString = UUID().uuidString |
|
let request = UNNotificationRequest(identifier: uuidString, content: content, trigger: trigger) |
|
|
|
//Register Request |
|
center.add(request) { (error) in |
|
//Check the parameter and handle any errors |
|
print("riddle notification scheduled") |
|
} |
|
} |
|
|
|
if (UDM.shared.defaults.object(forKey: "riddleNotification") != nil) { |
|
center.delegate = self |
|
center.getNotificationSettings(completionHandler: { (settings) in |
|
if settings.authorizationStatus == .notDetermined { |
|
// Notification permission has not been asked yet, go for it! |
|
// Notification permission was previously denied, go to settings & privacy to re-enable |
|
center.requestAuthorization(options: [.alert, .sound, .badge]) { (granted, error) in |
|
|
|
DispatchQueue.main.async { |
|
// Enable or disable features based on authorization. |
|
if !granted { |
|
let alert = UIAlertController(title: NSLocalizedString("Notification ", comment: ""), message: "", preferredStyle: .alert) |
|
alert.message = NSLocalizedString("Notifications disabled: Activate in Settings.", comment: "") |
|
alert.addAction(UIAlertAction(title: NSLocalizedString("ok", comment: ""), style: .cancel) { _ in |
|
// continue your work |
|
}) |
|
|
|
self.present(alert, animated: true, completion: nil) |
|
} |
|
} |
|
} |
|
} else if settings.authorizationStatus == .denied { |
|
DispatchQueue.main.async { |
|
let alert = UIAlertController(title: NSLocalizedString("Notification ", comment: ""), message: "", preferredStyle: .alert) |
|
// Enable or disable features based on authorization. |
|
alert.message = NSLocalizedString("Notifications disabled: Activate in Settings.", comment: "") |
|
alert.addAction(UIAlertAction(title: NSLocalizedString("ok", comment: ""), style: .cancel) { _ in |
|
// continue your work |
|
Home.riddleNotification = false |
|
UDM.shared.defaults.setValue(Home.riddleNotification, forKey: "riddleNotification") |
|
}) |
|
|
|
self.present(alert, animated: true, completion: nil) |
|
} |
|
} else if settings.authorizationStatus == .authorized { |
|
// Notification permission was already granted |
|
Home.riddleNotification = true |
|
UDM.shared.defaults.setValue(Home.riddleNotification, forKey: "riddleNotification") |
|
riddleNotification() |
|
} |
|
}) |
|
} |
|
if let value = UDM.shared.defaults.value(forKey: "riddleNotification") as? Bool { |
|
Home.riddleNotification = value |
|
} |
|
if Home.riddleNotification == true { |
|
riddleNotification() |
|
} |
|
else if Home.riddleNotification == false { |
|
print("no riddle notification scheduled") |
|
} |
|
} |
|
|
|
@IBAction func Riddles(_ sender: Any) { |
|
let vc = SFSafariViewController(url: URL(string: "https://riddles.com")!) |
|
|
|
present(vc, animated: true) |
|
} |
|
|
|
|
|
@IBAction func RiddlesandAnswers(_ sender: Any) { |
|
let vc = SFSafariViewController(url: URL(string: "https://riddlesandanswers.com")!) |
|
|
|
present(vc, animated: true) |
|
} |
|
|
|
@IBAction func menuTappedHome(_ sender: Any) { |
|
|
|
if menuOut == false { |
|
leadingHome.constant = 150 |
|
trailingHome.constant = -150 |
|
menuOut = true |
|
} |
|
else { |
|
leadingHome.constant = 0 |
|
trailingHome.constant = 0 |
|
menuOut = false |
|
} |
|
UIView.animate(withDuration: 0.2, delay: 0.0, options: .curveEaseIn, animations: { |
|
self.view.layoutIfNeeded() |
|
}) { (animationComplete) in |
|
print("The animation is complete") |
|
} |
|
} |
|
} |
|
|
class UDM { |
|
static let shared = UDM() |
|
let defaults = UserDefaults(suiteName: "com.riddlewednesday.saved.data")! |
|
|
} |