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 - swift 2.0 : Could not cast value of type '__NSArrayM' (0x10a9348d8) to 'NSDictionary' (0x10a934d60)

ios - valueForKey 返回 nil

swift - 解压无效文件时如何使 initWithCoder() 失败?

ios - 在项目选择上调用委托(delegate)方法

C#委托(delegate): Do i use this correctly?

iphone - 从 UINavigationController 弹出一个包含 UITabBarController 的 UIViewController

ios - 如何从iPhone专辑复制视频而不进行压缩?

ios - 为什么app在设置属性时会陷入死循环?

ios - swift 4如何控制多个按钮只高亮一个按钮?

objective-c - 需要引用 NSURLConnection 吗?