我正在为我的应用程序使用 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.
}
我想实现这个方法,并且在我的 View Controller 中实现 didbecomeactive 方法,用户锁定了设备,我只是把它放在我的 vc 上,但没有成功:
class ViewController: x ,UIApplicationDelegate {
func applicationWillResignActive(_ application: UIApplication) {
print("frommmm vc i am in will resign")
}
}
我知道这一点是因为我没有将委托(delegate)交给 vc,但是我该怎么做或者我还能为我的问题做些什么? 提前致谢
最佳答案
您不能仅将 UIApplicationDelegate
添加到任何 View 来调用它。
在 AppDelegate.swift
中使用协议(protocol) UIApplicationDelegate
中定义的方法
关于ios - 当用户在应用程序中锁定设备时,如何处理我的应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43946619/