首页 macOS AppStore 审核坑之一: 打开主窗口
文章
取消

macOS AppStore 审核坑之一: 打开主窗口

描述

在我发布的第一款 macOS App 时候遇到过这样一个问题: 在用户关闭应用主窗口后, 没有为用户提供重新打开主窗口的方法.

这个是一个不可忽略的问题, 否则你将会收到苹果的拒绝信息:

Design Preamble

The user interface of your app is not consistent with the macOS Human Interface Guidelines.

Specifically, we found that when the user closes the main application window there is no menu item to re-open it.

Next Steps

It would be appropriate for the app to implement a Window menu that lists the main window so it can be reopened, or provide similar functionality in another menu item. macOS Human Interface Guidelines state that “The menu bar always contains [a] Window menu”.

如邮件中所讲, 你必须为用户提供 重新打开主窗口 的方法.

解决方法

方法一

按照苹果的要求, 为用户提供 打开主窗口按钮/菜单. 此处有多个注意点.

1. 在 Dock 中点击 App 图标时候重新打开主窗口.

AppDelegate.m 中添加如下代码:

1
2
3
4
5
6
- (BOOL)applicationShouldHandleReopen:(NSApplication *)sender hasVisibleWindows:(BOOL)flag {
    if ([NSApplication sharedApplication].windows.count > 0) {
        [[NSApplication sharedApplication].windows.lastObject makeKeyAndOrderFront:[NSApplication sharedApplication]];
    }
    return YES;
}

2. 在工具栏中添加打开主窗口的菜单.

Storyboard

通过 Storyboard 拉线, 在 AppDelegate.m 中新增方法, 与上图的菜单要关联起来:

1
2
3
4
5
- (IBAction)reopenApplication:(id)sender {
    if ([NSApplication sharedApplication].windows.count > 0) {
        [[NSApplication sharedApplication].windows.lastObject makeKeyAndOrderFront:[NSApplication sharedApplication]];
    }
}

至此即完成了 重新打开主窗口 的这个目标, 至此基本万无一失 (也可以参考一下其他应用在这个地方是如何处理的).

方法二

如果你的应用在用户关闭窗口后, 不需要继续保持 App 进程的存活, 可以直接 杀死 App 的进程, 也是一种解决方法. 在 AppDelegate.m 中实现如下方法:

1
2
3
- (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)sender {
    return YES;
}

这样就会在第一次点击 x 按钮时候, 结束掉进程.

结语

以上两种做法都可以解决这个被拒的问题. 但是选择哪一种做法还要视你的 App 功能来定:

  1. 如果你的 App 提供的功能需要在后台持续工作, 就必须采取第一种方法来处理.
  2. 如果你的 App 每一个工作都依赖于用户的操作, 关闭窗口后就停止工作, 就可以直接结束进程. 比如 AppStore 就可以作为参考.
本文由作者按照 CC BY 4.0 进行授权

iOS 数组常用操作: 筛选

iOS App 被判定为太简被拒绝上架