0

I'm using XMPPframework for my application to get messages the problem is when user lock device when app is running and then unlock again the XMPPStream is disconnected from server and become useless but not when user just press home button (when user came to application again, XMPP starts reconnecting which is fine!) I searched the net and I found that this method are called before device get locked:

func applicationWillResignActive(_ application: UIApplication) {

    print("i am in will resign")
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
}

func applicationDidEnterBackground(_ application: UIApplication) {

    print("i am in background")
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

I want to implement this method and also didbecomeactive method in my viewcontroller which user locked device in I simply put this on my vc but didn't work out:

class ViewController: x ,UIApplicationDelegate {

func applicationWillResignActive(_ application: UIApplication) {

    print("frommmm vc i am in will resign")

}
}

I know it because I didn't give delegate to vc but how can I do this or what else can I do for my problem? Thanks in Advance

Eric Aya
  • 69,473
  • 35
  • 181
  • 253

1 Answers1

0

you can not just add the UIApplicationDelegate to a any view to get it called.

use the methods defined in the protocol UIApplicationDelegate in your AppDelegate.swift

PS: if you like to get informed deep in your views then you have to follow the suggestions in Detect iOS app entering background

Community
  • 1
  • 1
muescha
  • 1,544
  • 2
  • 12
  • 22