Currently I have a function in my AppDelegate.swift file which fires when a notification is received. I am using Firebase to send these notifications.
When the function runs, it checks for any extra attached data, with the key 'url', and if so, it runs a function in my ViewController.swift file.
The issue is that the function that is run in the ViewController tries to change the text of a label to "The function has ran", but when this line is run, it throws the error "Unexpectedly found nil while unwrapping an Optional value".
I can't figure out what this Optional is, so if anyone could suggest a possible issue with my code that would be great.
Things I have tried:
- Creating and using a new label
- Renaming the label
- Checked that the Outlet is properly connected
- Checking classes are properly set
Snippet of the AppDelegate.swift code:
import UIKit
import Firebase
import FirebaseInstanceID
import FirebaseMessaging
import UserNotifications
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var vc = ViewController()
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any],
fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
print(userInfo)
let url = userInfo["url"]
print("url is: \(String(describing: url))")
if url != nil {
hasRun = true
vc.changeLabel()
print("Func done")
}
completionHandler(UIBackgroundFetchResult.newData)
}
}
Snippet of the ViewController.swift code:
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var theLabel: UILabel!
func changeLabel() {
theLabel.text = "The change label code has run here"
}
}