2

How to identify actual touchBar hardware is available in an Mac book using obj c code so that i can provide touchbar menu options.

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Jeba Moses
  • 809
  • 8
  • 25

3 Answers3

3

From the Apple documentation :

There is no need, and no API, for your app to know whether or not there is a Touch Bar available. Whether your app is running on a machine that supports the Touch Bar or not, your app’s onscreen user interface (UI) appears and behaves the same way.

To check if touch bar is available (to improve UI/UX for example) you should implement the delegate and set a Bool like :

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

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

Ludovic
  • 1,992
  • 1
  • 21
  • 44
  • thank you for your kind answer, can you tell me the way to access available inputs/Outputs for a Mac. so that i can recognize if TouchBar is available or not from the hardware list. – Jeba Moses Feb 13 '17 at 08:21
  • I'm updating my answer – Ludovic Feb 13 '17 at 08:24
  • Please consider this case: suppose if the old Mac user upgraded their OS but don't have TouchBar(Hardware), how can we identify. (Bcz i need to give some options related to touch bar in preferences.) – Jeba Moses Feb 13 '17 at 08:45
  • @JebaMoses it's impossible! Apple ships new MacBook Pro with 10.12.1. A bellow version can not be installed on a new MacBook. Like an iPhone 7 can't have iOS9 installed. – Ludovic Feb 13 '17 at 08:49
  • @Ludovic: Just an FYI: I think they might've changed the specs for availability to `10.12.2`, although I can't confirm. I always get `@available(OSX 10.12.2, *)` when trying to implement any touchBar code though... – l'L'l Feb 13 '17 at 08:59
  • The original question is asking for Objective-C... I have no idea why the answers so far have all been Swift. – Michael Dautermann Feb 13 '17 at 08:59
  • 1
    @MichaelDautermann: In regard to the code; I don't see anything available from Apple that shows any difference between Swift and Objective-C; look for yourself: https://developer.apple.com/reference/appkit/nstouchbar?language=objc... (Objective-C is now Swift apparently) :D, and yes that was sarcasm. – l'L'l Feb 13 '17 at 09:04
  • the API's look the same (and that's by the purposeful design of Swift), but the two languages are different. Look at Ludovic's answer before and after his edit from Swift to Objective-C. You wouldn't be able to compile that Swift function in a `.m` file, nor would you be able to compile Objective-C in a `.swift` file (you'd need to use a bridging header to get access from Swift to Obj-C). – Michael Dautermann Feb 13 '17 at 09:08
  • @MichaelDautermann: Obviously, but why the hell doesn't Apple show Objective-C code when you click between Swift and Objective-C on that page — that's just lazy! – l'L'l Feb 13 '17 at 09:09
  • Developers have been [complaining about Apple's half-hearted and incomplete documentation for years](http://chaosinmotion.com/blog/?p=24), and to be fair, I *believe* a lot of Apple documentation is automatically generated from the interface (`.h`) files using tools/processes like what can be found [described in this blog post](http://nshipster.com/documentation/). Maybe an Apple engineer just forgot to put brackets or marks in the right places in the .h file? – Michael Dautermann Feb 13 '17 at 09:16
2

NSTouchBar can work in software as well as hardware, as show by the simulator in Xcode. It doesn't require the hardware to work. However, the Xcode 8 release notes tells you how to test if the OS can support any kind of Touch Bar functions.

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

Xcode 8.1 supports Touch Bar for Macs that include it, and supports adding Touch Bar functionality to your app. (28859277) Before using Touch Bar functionality in your app, confirm that the app is running on a macOS version that includes support for Touch Bar using a runtime check.

For example, the following Objective-C code performs a runtime check to make sure NSTouchBar is available:

NSClassFromString(@"NSTouchBar") != nil

In Swift code, do an availability check for macOS 10.12.1, and a runtime check for Touch Bar class availability. For example:

NSClassFromString("NSTouchBar") != nil

AndyTang
  • 547
  • 7
  • 24
1

If you want some control you can use 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.
    */
l'L'l
  • 44,951
  • 10
  • 95
  • 146
  • @I'L'I we can identify API support using OSX version, suppose the old Mac user upgraded their OS but don't have TouchBar(Hardware), how can we identify. – Jeba Moses Feb 13 '17 at 08:10
  • @JebaMoses: There's no specific way that I'm aware, I suppose you could return whether or not a touchBar was able to be successfully returned. Otherwise according to the docs, there really isn't an API to deal with, as the touchBar is more or less available whether the system has the hardware to support it. [Here's one way you might be able to check for a return value...](https://gist.github.com/anonymous/1f8ad96ebcfb4ecfa489ee56ce6df1f6) (Swift). If nothing is returned then you can assume there's no touchBar. – l'L'l Feb 13 '17 at 08:49
  • Basically in `Obj-C` you might do something equivalent to `if (NSTouchBar() == nil)`... honestly I have no idea if it will work or not. I get nothing returned, but I have no touchBar to test it on either :) – l'L'l Feb 13 '17 at 08:51