当应用程序中的用户锁定设备时,如何处理我的应用程序

时间:2017-05-12 21:18:18

标签: ios swift delegates appdelegate uiapplicationdelegate

我在我的应用程序中使用XMPPframework来获取消息问题是当用户在设备运行时锁定设备然后再次解锁时XMPPStream与服务器断开连接并变得无用但是当用户只按下主页按钮时(当用户再次来到应用程序,XMPP开始重新连接,这很好!)我搜索网络,我发现在设备锁定之前调用此方法:

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.
}

我想实现这个方法,并且在我的viewcontroller中也有一个主动方法,用户锁定设备我只是把它放在我的vc上但是没有解决:

class ViewController: x ,UIApplicationDelegate {

func applicationWillResignActive(_ application: UIApplication) {

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

}
}

我知道,因为我没有给vc授权,但我怎么能这样做或者我还能为我的问题做些什么呢? 在此先感谢

1 个答案:

答案 0 :(得分:0)

你不能只将UIApplicationDelegate添加到任何视图来调用它。

使用UIApplicationDelegate

中协议AppDelegate.swift中定义的方法

PS: 如果您希望深入了解您的观点,那么您必须遵循Detect iOS app entering background

中的建议