如何使用obj c识别Mac书中可用的实际触摸条硬件



如何在Mac书中使用obj c代码来识别实际的触摸条硬件,以便我可以提供触摸条菜单选项。

来自Apple文档:

没有必要,也没有API,让您的应用程序知道是否有触摸条可用。无论您的应用程序是否在支持触摸条的机器上运行,应用程序的屏幕用户界面(UI)都会以相同的方式显示和运行。

要检查触摸条是否可用(例如,为了改进UI/UX),您应该实现委托并设置类似Bool的:

// Declare a class variable
@property (nonatomic, assign) BOOL isTouchBarAvailable;
@available(OSX 10.12.1, *)
// A bellow version can not be installed on a new MacBook. 
// Like an iPhone 7 can't have iOS9 installed.
- (NSTouchBar *)makeTouchBar
{
// ... here the code to make the bar
self.isTouchBarAvailable = YES
return touchBar
}

来源:https://developer.apple.com/reference/appkit/nstouchbar?language=objc

NSTouchBar既可以在软件中工作,也可以在硬件中工作,如Xcode中的模拟器所示。它不需要硬件就可以工作。然而,Xcode8的发行说明告诉你如何测试操作系统是否可以支持任何类型的触摸条功能。

https://developer.apple.com/library/content/releasenotes/DeveloperTools/RN-Xcode/Introduction.html

Xcode 8.1支持包含它的Mac触摸条,并支持将触摸条功能添加到您的应用程序中。(28859277)在应用程序中使用触摸条功能之前,请使用运行时检查确认该应用程序运行在包含对触摸条支持的macOS版本上。

例如,以下Objective-C代码执行运行时检查以确保NSTouchBar可用:

NSClassFromString(@"NSTouchBar")!=无

在Swift代码中,对macOS 10.12.1进行可用性检查,并对Touch Bar类的可用性进行运行时检查。例如:

NSClassFromString("NSTouchBar")!=无

如果您想要一些控制,可以使用isAutomaticCustomizeTouchBarMenuItemEnabled:

import Cocoa
@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {
func applicationDidFinishLaunching(_ aNotification: Notification) {
// Here we just opt-in for allowing our instance of the
// NSTouchBar class to be customized throughout the app.
if #available(OSX 10.12.2, *) {
NSApplication.shared().isAutomaticCustomizeTouchBarMenuItemEnabled = true
}
}
/*
Whether or not a menu item to customize the touch bar can be automatically 
added to the main menu. It will only actually be added when hardware 
or simulator is present. Defaults to NO. Setting this property to YES 
is the recommended way to add the customization menu item.
*/

最新更新