我开发了一个 Mac OS 应用程序。同一包中有一个主窗口应用程序和一个菜单栏帮助程序应用程序。
帮助应用程序有一个名为“显示主应用程序”的菜单项。如果我单击菜单项后未启动辅助应用程序,则辅助应用程序应启动主应用程序。如果辅助应用程序隐藏在扩展坞中或其主窗口已关闭,则辅助应用程序应将主应用程序置于最前面。
我知道如何启动主应用程序。但我不知道如何实现重新激活功能。我使用了如下代码。
let apps = NSRunningApplication.runningApplications(withBundleIdentifier: "com.xxx.yyy")
if let mainApp = apps.first {
mainApp.activate(options: [ .activateIgnoringOtherApps ])
}
Run Code Online (Sandbox Code Playgroud)
当主应用程序刚刚启动并隐藏在扩展坞中时,激活方法似乎不执行任何操作。我注意到主应用程序实际上已激活,因为主菜单栏已更改为主应用程序的主菜单。但其AppDelegate类的“applicationShouldHandleReopen”方法没有被调用。所以主窗口不能放在前面。
我怎样才能让它发挥作用?
我的应用程序依赖于applicationProtectedDataDidBecomeAvailable被调用,但我注意到,如果我锁定设备并等待大约 3 分钟然后解锁,则applicationProtectedDataDidBecomeAvailable不会被调用。另外,什么时候会applicationProtectedDataDidBecomeAvailable被调用,而不是applicationWillEnterForeground因为我注意到,当它被调用时,有时这些调用的顺序是随机的
多谢你们
在 iOS 和 Objective C 上工作时,我发现了UIApplicationDelegate,
applicationProtectedDataDidBecomeAvailable
Run Code Online (Sandbox Code Playgroud)
设备解锁后被多次调用。可能的原因是什么?
applicationProtectedDataDidBecomeAvailable - 在使用内容保护的设备上,受保护的文件以加密形式存储并且仅在特定时间可用,通常是在设备解锁时。此通知让您的应用知道设备现已解锁,您可以再次访问某些类型的受保护文件。
问题:
我发现有关 AppDelegate 方法的一些意外行为application(_:configurationForConnecting:options:)。
该文件指出:
UIKit 在创建新场景前不久调用此方法。
我希望每次启动应用程序时都是这种情况。
当我第一次启动我的应用程序时确实会调用该方法,但是对于所有后续启动,它不是.
复制:
我有一个非常简单的测试用例来重现:
AppDelegate如下所示:
// from Apple's sample project:
func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration {
// Called when a new scene session is being created.
// Use this method to select a configuration to create the new scene with.
print("I was called!"). // <--- debugging statement
return UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role) …Run Code Online (Sandbox Code Playgroud)就像标题一样,我在某些用户设备上遇到了一个非常奇怪的错误。我已经搜索过,但找不到任何与我完全相同的问题。
以下是有关此错误的更多详细信息:
我相信这是一个iOS系统错误,有人可以确认吗?如果没有,我该如何解决?谢谢!
我想移植我现有的 UIKit 应用程序以使用适用App于 iOS 14 的新 API。(但仍然使用AppDelegatevia 的方法UIApplicationDelegateAdaptor,但是我仍然想支持 iOS 13。有没有一种方法可以设置,以便 iOS 14 ,使用Appas@main但如果 iOS 13 使用AppDelegateas @UIApplicationMain?即使下面的代码也无法编译,因为我的最低构建目标是 iOS 13:
@available(iOS 14.0, *)
@main
struct HockeyTrackerApp: App {
// inject into SwiftUI life-cycle via adaptor !!!
@UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
var body: some Scene {
WindowGroup {
HomeView()
}
}
}
Run Code Online (Sandbox Code Playgroud) 所以我是 Swift 的新手。我认为这是一种快速发展的语言。
但是我看到的很多教程都将 AppDelegate 和 SceneDelegate 作为标准 init 文件,为什么我没有呢?为什么我似乎找不到他们的选择?
这些教程包括 2020 年 7 月的教程,所以我猜这是最近的某种更新。我应该如何处理具有不同文件类型的两种类型的应用程序?作为“老手”程序员,哪个更有优势?
谢谢大家。快乐编码。
我正在尝试在我的应用程序中设置深层链接。我已经设置了应用程序以及推送通知。但是,我无法完成 AppDelegate 接收用户单击我想要深层链接到的屏幕的通知的最终链接。我基本上想从 AppDelegate 调用 viewRouter.goToFred() 。我在这里设置了一个 git 存储库(https://github.com/cameronhenige/SwiftUITestDeepLink),其中包含一个应用程序,该应用程序已设置好除最后一部分之外的所有内容。有人可以帮我解决这个问题吗?这是相关的代码片段。谢谢!
import SwiftUI
struct MainView: View {
@EnvironmentObject var viewRouter: ViewRouter
var body: some View {
NavigationView {
List {
ForEach(viewRouter.pets) { pet in
NavigationLink(
destination: PetView(),
tag: pet,
selection: $viewRouter.selectedPet,
label: {
Text(pet.name)
}
)
}
Button("Send Notification to Fred") {
viewRouter.sendNotification()
}
Button("Manually go to Fred") {
viewRouter.goToFred()
}
}
}
}
}
struct MainView_Previews: PreviewProvider {
static var previews: some View {
MainView()
}
}
Run Code Online (Sandbox Code Playgroud)
import SwiftUI …Run Code Online (Sandbox Code Playgroud) 当 SwiftUI 应用程序最小化并单击停靠图标时。该应用程序不会像其他应用程序一样被减少并放在前面。
import SwiftUI
@main
struct MyApp: App {
@NSApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
var body: some Scene {
WindowGroup {
MainView()
}
}
}
class AppDelegate: NSObject, NSApplicationDelegate {
func applicationShouldHandleReopen(_ sender: NSApplication, hasVisibleWindows flag: Bool) -> Bool {
// THIS IS NEVER CALLED!!!
if !flag {
for window: AnyObject in sender.windows {
window.makeKeyAndOrderFront(self)
}
}
return true
}
}
Run Code Online (Sandbox Code Playgroud)
其他委托方法(例如 applicationDidLaunch)确实会被调用,因此这不是链接问题。有谁知道如何让它发挥作用?
我开始开发一款适用于 MacOS 的应用程序,但遇到了几个问题。其中之一如下。
\n我有一个 UIElement 应用程序。
\n它通过以下方式初始化:
\n@main\nstruct MyApp: App {\n @NSApplicationDelegateAdaptor(AppDelegate.self) var appDelegate\n\n var body: some Scene {\n Settings {\n EmptyView()\n }\n }\n}\nRun Code Online (Sandbox Code Playgroud)\n在我的 AppDelegate.swift 中我有:
\nimport SwiftUI\n\nclass AppDelegate: NSObject, NSApplicationDelegate, NSWindowDelegate {\n static private(set) var instance: AppDelegate!\n var settingsWindow: NSWindow!\n lazy var statusBarItem = NSStatusBar.system.statusItem(withLength: NSStatusItem.variableLength)\n let menu = AppMenu()\n \n func applicationDidFinishLaunching(_ notification: Notification) {\n statusBarItem.button?.image = NSImage(\n systemSymbolName: "circle",\n accessibilityDescription: "My App"\n )\n statusBarItem.menu = menu.createMenu()\n }\n\n @objc func openSettingsWindow() {\n …Run Code Online (Sandbox Code Playgroud)