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

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

我想实现这个方法,并且在我的 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 中定义的方法

PS: 如果您想深入了解您的观点,那么您必须遵循Detect iOS app entering background中的建议。

关于ios - 当用户在应用程序中锁定设备时,如何处理我的应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43946619/

相关文章:

ios - 在滚动 tableview 时,我的 waves 动画在 iOS 中停止

iOS-charts 具有多种颜色的堆叠条形图

ios - 如何在 uiimagepickercontroller 中创建可裁剪的横幅图像?

ios - UIRefreshcontrol - UItableview 标题 View 卡住了,refreshview 没有显示

json - 使用 Swift 处理 GZipped(包含 JSON)HTTP 响应

c# - 如何使用反射创建 lambda 并将它们添加到操作中

ios - 使用 swift 在 ios 上集群谷歌地图标记

ios - 如何在 Xcode Interface Builder 中使用约束/自动布局来正确缩放图像和字体?

ios - 协议(protocol)/委托(delegate)不起作用

iphone - viewcontroller 中 appdelegate 和 delegate 的区别