Home > Enterprise >  How to show alert in every ViewController when app is active (foreground)?
How to show alert in every ViewController when app is active (foreground)?

Time:10-29

I'm trying to show an alert when user opens the app (app become active). The reason of showing alert is to give information that user is logged in, in another device, so he will be logged out from the device.

The code below is implemented in AppDelegate. My expectation is - if it's going to cause failure - it means he will be logged out.

The API works well, but I don't know how to show the alert when apps in the foreground. How to implement this correctly?

I don't want to hit the API in every viewDidLoad in each viewController.

extension AppDelegate: MessagingDelegate {
    func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String?) {
        guard let fcmToken = fcmToken else { return }
        print("Firebase registration token: \(fcmToken)")
        
        let dataDict:[String: String] = ["token": fcmToken]
        NotificationCenter.default.post(name: Notification.Name("FCMToken"), object: nil, userInfo: dataDict)

         is generated.
        guard !AuthManager.shared.userLoginSession.isEmpty,  //true  user belom login
              let tokenlogin = SettingsManager.tokenlogin,
              !tokenlogin.isEmpty //true  token login harus kosong
        else { return }
        NetworkManager.instance.requestObject(ServiceAPI.start(regID: fcmToken, id: AuthManager.shared.userLoginSession, token: tokenlogin), c: SingleResponse<StartResponse>.self) { (result) in
            switch result {
            case .success(let response):
                AuthManager.shared.userLoginSession = response.data.apikey ?? ""
                //                MoEngage.sharedInstance().setAlias("1234")
            case .failure(let error):
                // If it's going failure. it means they will be logged out 
                print("FCM with error", error.description)
            }
        }
    }
}

CodePudding user response:

You can add code in AppDelegate below like this

func application(_ application: UIApplication,   didFinishLaunchingWithOptions launchOptions:   [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    // Override point for customization after application launch.
   
    DispatchQueue.main.asyncAfter(deadline: .now()   1.0) {
        let window = UIApplication.shared
            .connectedScenes
            .flatMap { ($0 as? UIWindowScene)?.windows ?? [] }
            .first { $0.isKeyWindow }
        
        let alertController = UIAlertController(title: "Test Data", message:"Message", preferredStyle: UIAlertController.Style.alert)
                
        // add an action (button)
        alertController.addAction(UIAlertAction(title: "OK", style: UIAlertAction.Style.default, handler: nil))
               
        
        window?.rootViewController?.present(alertController, animated: true)

    }
    
    return true
}
  • Related