2017-02-13 16 views

回答

1

從Apple文檔:

沒有必要,也沒有API,爲您的應用程序,以瞭解是否有可用的觸摸條。無論您的應用程序是否在支持Touch Bar的計算機上運行,​​您的應用程序的屏幕用戶界面(UI)都會顯示並且行爲方式相同。

要檢查是否觸杆可用(改善UI/UX例如),你應該實現委託並設置一個布爾值,如:

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

+0

感謝您這樣回答,你能告訴我去訪問可用輸入,用於在Mac /輸出。以便我可以從硬件列表中識別TouchBar是否可用。 –

+0

我正在更新我的答案 – Ludovic

+0

請考慮這種情況:假設舊Mac用戶升級其操作系統但沒有TouchBar(硬件),我們如何識別。 (Bcz我需要在偏好設置中提供一些與觸摸欄有關的選項。) –

1

如果你想一些控制,你可以使用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. 
    */ 
+0

@I'L'I我們可以使用OSX版本來識別API支持,假設舊的Mac用戶升級了他們的操作系統,但沒有TouchBar(硬件),我們如何識別。 –

+0

@JebaMoses:沒有具體的方式,我知道,我想你可以返回一個touchBar是否能夠成功返回。否則,根據文檔,實際上沒有API需要處理,因爲無論系統是否具有支持它的硬件,touchBar都可用。 [這裏有一種方法可以檢查返回值...](https://gist.github.com/anonymous/1f8ad96ebcfb4ecfa489ee56ce6df1f6)(Swift)。如果沒有返回,那麼你可以假設沒有touchBar。 –

+0

基本上在'Obj-C'中,你可能會做一些等同於'if(NSTouchBar()== nil)'......老實說,我不知道它是否會起作用。我什麼都沒有返回,但我沒有touchBar來測試它:) –

2

NSTouchBar可以在軟件工作,以及硬如Xcode中的模擬器所示。它不需要硬件工作。但是,Xcode 8發行說明會告訴您如何測試操作系統是否可以支持任何類型的觸摸欄功能。

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

的Xcode 8.1支持觸摸條對於Mac系統,其中包括它,並支持添加觸摸條功能,您的應用程序。 (28859277) 在您的應用程序中使用觸摸欄功能之前,請確認應用程序使用運行時檢查在包含對觸摸欄的支持的macOS版本上運行。

例如,下面的Objective-C代碼執行運行時檢查,以確保NSTouchBar可用:

NSClassFromString(@ 「NSTouchBar」)=零

在斯威夫特代碼,請可用性檢查對於macOS 10.12.1,以及針對Touch Bar類可用性的運行時檢查。例如:

NSClassFromString(「NSTouchBar」)=零