在Swift上以编程方式将应用程序置于前台 [英] Put app on foreground programmatically on Swift

查看:85
本文介绍了在Swift上以编程方式将应用程序置于前台的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我测试过:

UIControl().sendAction(#selector(URLSessionTask.suspend), to: UIApplication.shared, for: nil)

用于将应用程序置于后台,并且可以正常工作.

which is for putting app on background and it works.

如何将应用放回前台?

我尝试过:

UIControl().sendAction(#selector(URLSessionTask.resume), to: UIApplication.shared, for: nil)

但是最终它崩溃了...

But eventually it crashes...

谢谢

推荐答案

更新:

由于您已经表明您正在寻找任何技术解决方案,即使那些与App Store或Apple条款不兼容的解决方案,也可以使用 私有API LSApplicationWorkspace:openApplicationWithBundleID .尝试这样的事情:

Since you've indicated that you're looking for any technical solution, even those not compatible with the App Store or Apple's terms, this should be possible using the Private API LSApplicationWorkspace: openApplicationWithBundleID. Try something like this:

创建一个.h文件,并设置 LSApplicationWorkspace 类的接口,并列出所需的方法.您将需要在桥接标头中 #import"PrivateHeaders.h" .

Create a .h file and set up an interface to the LSApplicationWorkspace class and list the required method. You will need to #import "PrivateHeaders.h" in your bridging header.

//
// PrivateHeaders.h
//

#ifndef PrivateHeaders_h
#define PrivateHeaders_h

@interface LSApplicationWorkspace : NSObject

- (bool)openApplicationWithBundleID:(id)arg1;

@end

#endif /* PrivateHeaders_h */

然后,您应该能够调用此函数,并以字符串形式传入应用程序的捆绑包标识符.

You should then be able to call this function and pass in the Bundle Identifier of your app as an string.

//
// SomeClass.swift
//

import MobileCoreServices

let workspace = LSApplicationWorkspace()

/**
 Launch an App given its bundle identifier
 - parameter bundleIdentifier: The bundle identifier of the app to launch
 - returns: True if app is launched, otherwise false
 */
func openApp(withBundleIdentifier bundleIdentifier: String) -> Bool {
    // Call the Private API LSApplicationWorkspace method
    return workspace.openApplication(withBundleID: bundleIdentifier)
}

原始:

您正在做的事情可能违反了

What you are doing is likely a violation of the iOS Human Interface Guidelines (although the "Don’t Quit Programmatically" is no longer specifically defined), so as the comments have said, it is not suited to the App Store. Regardless, once your app is suspended in this way, I don't expect that there is a way to resume it programmatically, unless you can hook into a Background Operation to run URLSessionTask.resume, but I have not tested it and am unsure whether it can work.

可以使用自定义URL方案,或通过

Apps can be launched (and hence brought into the foreground) programmatically from another app or today extension by using a Custom URL Scheme, or via a Push Notification. It isn't possible to launch the app from the Background Operation via a URL Scheme, since it is part of the UIKit framework, which must be run in the main thread.

总而言之,我认为您最好的选择是尝试使用通知.这只是意味着用户将需要单击通知以使您的应用回到前台.

In summary, I think your best option is to try to use a Notification. This just means that the user will need to click on the notification to bring your app back into the foreground.

这篇关于在Swift上以编程方式将应用程序置于前台的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆