gpt4 book ai didi

ios - 在 Swift 中以编程方式将应用程序置于前台

转载 作者:行者123 更新时间:2023-11-28 07:39:30 38 4
gpt4 key购买 nike

我测试过:

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

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

如何将应用程序重新置于前台?

我试过:

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

但最终它崩溃了......

谢谢

最佳答案

更新:

既然您已经表示您正在寻找任何技术解决方案,即使是那些与 App Store 或 Apple 条款不兼容的解决方案,应该可以使用私有(private) API LSApplicationWorkspace:openApplicationWithBundleID。尝试这样的事情:

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

//
// PrivateHeaders.h
//

#ifndef PrivateHeaders_h
#define PrivateHeaders_h

@interface LSApplicationWorkspace : NSObject

- (bool)openApplicationWithBundleID:(id)arg1;

@end

#endif /* PrivateHeaders_h */

然后您应该能够调用此函数并将应用程序的捆绑标识符作为字符串传递。

//
// 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)
}

原文:

您的行为可能违反了 iOS Human Interface Guidelines (虽然“Don't Quit Programmatically”不再具体定义),所以正如评论所说,它不适合App Store。无论如何,一旦您的应用程序以这种方式暂停,我不希望有办法以编程方式恢复它,除非您可以连接到 Background Operation。运行 URLSessionTask.resume,但我还没有测试它,不确定它是否可以工作。

应用程序可以通过使用 Custom URL Scheme 从另一个应用程序或今天的扩展以编程方式启动(并因此进入前台) ,或通过 Push Notification .无法通过 URL Scheme 从后台操作启动应用程序,因为它是 UIKit 框架的一部分,必须在主线程中运行。

总而言之,我认为您最好的选择是尝试使用通知。这仅意味着用户需要单击通知才能将您的应用程序带回前台。

关于ios - 在 Swift 中以编程方式将应用程序置于前台,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52633574/

38 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com