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?
我正在尝试使用Swift以编程方式返回iOS8 App中的主屏幕。我想继续在后台运行应用程序。关于如何做到这一点的任何想法?
Thanks in advance for the help.
先谢谢您的帮助。
5 个解决方案
#1
25
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.
启动应用程序时,系统调用UIApplicationMain函数;在其他任务中,此函数创建一个单独的UIApplication对象。此后,您可以通过调用sharedApplication类方法来访问该对象。
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:
要正常退出(iOS开发人员库明确警告您不要使用exit(0),因为这会记录为崩溃),您可以使用:
UIControl().sendAction(#selector(URLSessionTask.suspend), to: UIApplication.shared, for: nil)
For example, my app exits when the user shakes the device. So, in ViewController.swift:
例如,当用户摇动设备时,我的应用会退出。所以,在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)
}}
#2
12
Swift 4:
斯威夫特4:
UIControl().sendAction(#selector(NSXPCConnection.suspend),
to: UIApplication.shared, for: nil)
#3
9
For that you should use following code
为此,您应该使用以下代码
import Darwin exit(0)
#4
1
To force your app into the background, you can legally launch another app, such as Safari, via a URL, into the foreground.
要强制将应用程序置于后台,您可以通过URL合法地将其他应用程序(例如Safari)启动到前台。
See: how to open an URL in Swift3
请参阅:如何在Swift3中打开URL
UIApplication.shared.open() (and the older openURL) are a documented public APIs.
UIApplication.shared.open()(以及较旧的openURL)是一个记录在案的公共API。
If you set the exits-on-suspend plist key, opening another app via URL will also kill your app. The use of this key is a documented legal use of app plist keys, available for any app to "legally" use.
如果您设置了退出暂停plist键,则通过URL打开另一个应用也会终止您的应用。此密钥的使用是对app plist密钥的合法使用,可供任何“合法”使用的应用程序使用。
Also, if your app, for some impolite reason, continues to allocate and dirty large amounts of memory in the background, without responding to memory warnings, the OS will very likely kill it.
此外,如果您的应用由于某些不礼貌的原因,继续在后台分配和弄脏大量内存,而不响应内存警告,操作系统很可能会将其杀死。
#5
0
There is no way to "programmatically return to the home screen" except for crashing, exiting your program or calling unofficial API. Neither is welcome by Apple. Plus the Human Interface Guidelines are also covering this.
除了崩溃,退出程序或调用非官方API之外,没有办法“以编程方式返回主屏幕”。 Apple也不欢迎。此外,人机界面指南也涵盖了这一点。
#1
25
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.
启动应用程序时,系统调用UIApplicationMain函数;在其他任务中,此函数创建一个单独的UIApplication对象。此后,您可以通过调用sharedApplication类方法来访问该对象。
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:
要正常退出(iOS开发人员库明确警告您不要使用exit(0),因为这会记录为崩溃),您可以使用:
UIControl().sendAction(#selector(URLSessionTask.suspend), to: UIApplication.shared, for: nil)
For example, my app exits when the user shakes the device. So, in ViewController.swift:
例如,当用户摇动设备时,我的应用会退出。所以,在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)
}}
#2
12
Swift 4:
斯威夫特4:
UIControl().sendAction(#selector(NSXPCConnection.suspend),
to: UIApplication.shared, for: nil)
#3
9
For that you should use following code
为此,您应该使用以下代码
import Darwin exit(0)
#4
1
To force your app into the background, you can legally launch another app, such as Safari, via a URL, into the foreground.
要强制将应用程序置于后台,您可以通过URL合法地将其他应用程序(例如Safari)启动到前台。
See: how to open an URL in Swift3
请参阅:如何在Swift3中打开URL
UIApplication.shared.open() (and the older openURL) are a documented public APIs.
UIApplication.shared.open()(以及较旧的openURL)是一个记录在案的公共API。
If you set the exits-on-suspend plist key, opening another app via URL will also kill your app. The use of this key is a documented legal use of app plist keys, available for any app to "legally" use.
如果您设置了退出暂停plist键,则通过URL打开另一个应用也会终止您的应用。此密钥的使用是对app plist密钥的合法使用,可供任何“合法”使用的应用程序使用。
Also, if your app, for some impolite reason, continues to allocate and dirty large amounts of memory in the background, without responding to memory warnings, the OS will very likely kill it.
此外,如果您的应用由于某些不礼貌的原因,继续在后台分配和弄脏大量内存,而不响应内存警告,操作系统很可能会将其杀死。
#5
0
There is no way to "programmatically return to the home screen" except for crashing, exiting your program or calling unofficial API. Neither is welcome by Apple. Plus the Human Interface Guidelines are also covering this.
除了崩溃,退出程序或调用非官方API之外,没有办法“以编程方式返回主屏幕”。 Apple也不欢迎。此外,人机界面指南也涵盖了这一点。