如何使用Swift编程退出app并返回iOS 8中的主屏幕 [英] How to exit app and return to home screen in iOS 8 using Swift programming
问题描述
我正在尝试使用Swift以编程方式返回iOS8 App中的主屏幕。我想继续在后台运行应用程序。关于如何做到这一点的任何想法?
I'm trying to programmatically return to the home screen in an iOS8 App using Swift. I want to continue the application running in the background though. Any ideas on how to do this?
提前感谢您的帮助。
推荐答案
启动应用程序时,系统调用UIApplicationMain函数;在其他任务中,此函数创建单个 UIApplication 对象。然后通过调用sharedApplication类方法访问该对象。
When an app is launched, the system calls the UIApplicationMain function; among its other tasks, this function creates a singleton UIApplication object. Thereafter you access the object by calling the sharedApplication class method.
要正常退出(iOS开发人员库明确警告您不要使用exit(0),因为它记录为崩溃)你可以使用:
To exit gracefully (the iOS developer library explicitly warns you not to use exit(0) because this is logged as a crash ) you can use:
UIControl().sendAction(#selector(URLSessionTask.suspend), to: UIApplication.shared, for: nil)
例如,当用户摇动设备时,我的应用会退出。所以,在ViewController.swift中:
For example, my app exits when the user shakes the device. So, in ViewController.swift:
override func motionEnded(motion: UIEventSubtype,
withEvent event: UIEvent?) {
if motion == .MotionShake{
//Comment: to terminate app, do not use exit(0) bc that is logged as a crash.
UIControl().sendAction(Selector("suspend"), to: UIApplication.sharedApplication(), forEvent: nil)
}}
这篇关于如何使用Swift编程退出app并返回iOS 8中的主屏幕的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!