简体   繁体   中英

How to call appdelegate.swift after logout in ios swift?

I have have been working on the FCM token. Initially, I can able to register FCM token and receive the token from FCM. My issue is while doing log out, I have been calling delete FCM token API call from the backend and it removes token from the device. After removing the FCM token, it moves to the login page controller but I could not able register FCM token again. While login I need to pass FCM token along with login API call.

here is the code I used to get FCM token in login viewController:

       var fcmval :String?

override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.

   NotificationCenter.default.addObserver(self, selector: #selector(self.fcmTokenUpdated(notification:)), name: Notification.Name("FCMToken"), object: nil)


}

deinit {
    //Don't forget to removeObserver
    NotificationCenter.default.removeObserver(self, name: Notification.Name("FCMToken"), object: nil)
}

@objc func fcmTokenUpdated(notification:Notification){


    if let userInfo = notification.userInfo as? [String: String]{

        if let c = userInfo["token"] {

            print(c)

            fcmval = c

            print("fcm token from app view controller:::", fcmval)

             let baseURL = "https://url"
             A8FlowBuilder(self).showLoginView(url: baseURL, fcm: fcmval ?? "")
        }

    }
}

override func viewDidAppear(_ animated: Bool) {

     NotificationCenter.default.addObserver(self, selector: #selector(self.fcmTokenUpdated(notification:)), name: Notification.Name("FCMToken"), object: nil)
}

How to register FCM token after log out?

Save it in defaults / global vaiable

@objc func fcmTokenUpdated(notification:Notification){  
  if let userInfo = notification.userInfo as? [String: String] , let c = userInfo["token"] {
       UserDefaults.standard.set(c,"Token")
   }
}

then access it anywhere

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM