Check out code with Chris on YouTube!
Post
Replies
Boosts
Views
Activity
Ok, thanks!
Here is a link to it on google drive. Can you please take a look?
bit.ly/riddle-project (I was having trouble hyperlinking, so just copy and paste into your browser.)
Yay, it worked! Thanks
I would like to update the text weekly. Preferably based on text on a website.
I already have one, riddlewednesday.net/riddle is the page I want and I want to take the first block of text.
Home ViewController:
import UIKit
import SafariServices
import UserNotifications
class Home: UIViewController {
@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]) { (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
}
}
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")
}
else {
let isRegisteredForRemoteNotifications = UIApplication.shared.isRegisteredForRemoteNotifications
if isRegisteredForRemoteNotifications {
// User is registered for notification
Home.riddleNotification = true
UDM.shared.defaults.setValue(Home.riddleNotification, forKey: "riddleNotification")
}
else {
// Show alert user is not registered for notification
Home.riddleNotification = false
UDM.shared.defaults.setValue(Home.riddleNotification, forKey: "riddleNotification")
}
riddleNotification()
}
}
@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")!
}
Settings ViewController:
import UIKit
class Settings: UIViewController {
@IBOutlet weak var leading_Settings: NSLayoutConstraint!
@IBOutlet weak var trailing_Settings: NSLayoutConstraint!
@IBOutlet var riddleSwitch: UISwitch!
var menuOut = false
override func viewDidLoad() {
super.viewDidLoad()
if Home.riddleNotification == true {
riddleSwitch.setOn(true, animated: true)
}
if Home.riddleNotification == false {
riddleSwitch.setOn(false, animated: true)
}
}
@IBAction func menuTappedSettings(_ sender: Any) {
if menuOut == false {
leading_Settings.constant = 150
trailing_Settings.constant = -150
menuOut = true
}
else {
leading_Settings.constant = 0
trailing_Settings.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")
}
}
@IBAction func riddleSwitchDidChange(_ sender: Any) {
if riddleSwitch.isOn {
Home.riddleNotification = true
UDM.shared.defaults.setValue(Home.riddleNotification, forKey: "riddleNotification")
}
else {
Home.riddleNotification = false
UDM.shared.defaults.setValue(Home.riddleNotification, forKey: "riddleNotification")
}
}
}
AppDelegate:
import UIKit
import Firebase
@main
class AppDelegate: UIResponder, UIApplicationDelegate {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
FirebaseApp.configure()
return true
}
// MARK: UISceneSession Lifecycle
func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration {
// Called when a new scene session is being created.
// Use this method to select a configuration to create the new scene with.
return UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role)
}
func application(_ application: UIApplication, didDiscardSceneSessions sceneSessions: Set<UISceneSession>) {
// Called when the user discards a scene session.
// If any sessions were discarded while the application was not running, this will be called shortly after application:didFinishLaunchingWithOptions.
// Use this method to release any resources that were specific to the discarded scenes, as they will not return.
}
}
TheRiddle ViewController:
import SafariServices
import WebKit
import Firebase
class TheRiddle: UIViewController {
@IBOutlet weak var leading_TheRiddle: NSLayoutConstraint!
@IBOutlet weak var trailing_TheRiddle: NSLayoutConstraint!
@IBOutlet weak var CurrentRiddle: UILabel!
@IBOutlet weak var PreviousRiddle: UILabel!
var menuOut = false
override func viewDidLoad() {
super.viewDidLoad()
let db = Firestore.firestore()
db.collection("TheRiddle").document("Riddles").getDocument { (document, error) in
//check for error
if error == nil {
//check if document exists
if document != nil {
}else {
if let currentRiddle = document!.get("CurrentRiddle") as? String {
self.CurrentRiddle.text = "This Weeks Riddle: " + currentRiddle
}
if let previousRiddle = document!.get("CurrentRiddle") as? String {
self.CurrentRiddle.text = "Previous Riddle: " + previousRiddle
}
}
}
}
}
@IBAction func RiddleAnswerForm(_ sender: Any) {
let vc = SFSafariViewController(url: URL(string: "https://forms.office.com/Pages/ResponsePage.aspx?id=ytAqTDte6UK-KD5v_kOm4Y843IzqmYtFlDtLrfRYsi1UMFpUMk1GN01GS05BVFlJUElONk4yR1hKUCQlQCN0PWcu")!)
present(vc, animated: true)
}
@IBAction func menuTappedTheRiddle(_ sender: Any) {
if menuOut == false {
leading_TheRiddle.constant = 150
trailing_TheRiddle.constant = -150
menuOut = true
}
else {
leading_TheRiddle.constant = 0
trailing_TheRiddle.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")
}
}
}
I am just going to use Firebase. Thanks though!
I think I figured it out. When I initially determine if they allowed notifications something in there isn’t correct. I need code asking if they allowed local notifications, but I can’t find any. Ideas?
Just tried this, it didn’t work. Other ideas?
That didn't work to check, nothing happened with the switch.
Here is my current Home.swift code:
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")!
}
That didn't work to check, nothing happened with the switch. Same code as the other thread.
I don't think my app is connected to Firestore, how would I double check this?
Update, it is connected but I am not using the correct code. What code would i use to get the data of a specific data?
This is the most up to date code
import SafariServices
import WebKit
import FirebaseFirestore
import Firebase
class TheRiddle: UIViewController {
@IBOutlet weak var leading_TheRiddle: NSLayoutConstraint!
@IBOutlet weak var trailing_TheRiddle: NSLayoutConstraint!
@IBOutlet weak var CurrentRiddle: UILabel!
@IBOutlet weak var PreviousRiddle: UILabel!
var menuOut = false
override func viewDidLoad() {
super.viewDidLoad()
let db = Firestore.firestore()
print("TheRiddle collection: ", db.collection("TheRiddle").document("Riddles"))
db.collection("TheRiddle").document("Riddles").addSnapshotListener { snapshot, error in
//check for error
if error == nil {
//check if document exists
if snapshot != nil {
}else {
if let currentRiddle = snapshot?["CurrentRiddle"] as? String {
self.CurrentRiddle.text = "This Weeks Riddle: " + currentRiddle
print(currentRiddle)
}
if let previousRiddle = snapshot?["CurrentRiddle"] as? String {
self.PreviousRiddle.text = "Previous Riddle: " + previousRiddle
}
}
}
}
//db.collection("test").addDocument(data: ["hi":"bye"])
}
@IBAction func RiddleAnswerForm(_ sender: Any) {
let vc = SFSafariViewController(url: URL(string: "https://forms.office.com/Pages/ResponsePage.aspx?id=ytAqTDte6UK-KD5v_kOm4Y843IzqmYtFlDtLrfRYsi1UMFpUMk1GN01GS05BVFlJUElONk4yR1hKUCQlQCN0PWcu")!)
present(vc, animated: true)
}
@IBAction func menuTappedTheRiddle(_ sender: Any) {
if menuOut == false {
leading_TheRiddle.constant = 150
trailing_TheRiddle.constant = -150
menuOut = true
}
else {
leading_TheRiddle.constant = 0
trailing_TheRiddle.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")
}
}
}