SwiftUI Touchbar

Hi community,


I never build a macOS application before and just getting started with Xcode.
I'm completely lost on how to implement the touchbar while using SwiftUI.

I read and tested the tutorial provided here: https://developer.apple.com/documentation/appkit/touch_bar/creating_and_customizing_the_touch_bar
But the code doesn't make sense to me. I played around about 8 hours now and I wasn't even able to make the touchbar show up with a default dummy button. The longer I experimented, the more I had the impression that this all is not SwiftUI, but I really can't tell, because I don't know the difference yet. I just want to learn the new System and don't start with outdated stuff.

Indeed, everything I found on the internet, tutoring the touchbar implementation seems to be no SwiftUI. The only thing I found is this ( https://developer.apple.com/documentation/swiftui/touchbar ) and this ( https://stackoverflow.com/questions/59919050/how-can-i-display-touch-bar-buttons-using-swiftui ).
None of these help. I don't even know where to start. It's pretty easy to add content to the default window, but thats of course not what I want.
For context: I'm trying to build an app that functions like a bridge to implement touchbar functionality to an application I'm using professionally. The app will in the end only display touchbar content and will have no window whatsoever.
Hope someone can help, I'm maximum frustrated,

Nils

Answered by Noblauch in 418220022

Ah, I see, my last post isn't public since the code I posted is being moderated *sigh*.

BUT, I got it to work with a little hint from this guy (Vincent) in the sub-answers:
https://stackoverflow.com/questions/59919050/how-can-i-display-touch-bar-buttons-using-swiftui
"I needed to open

System Preferences
>
Keyboard
>
Shortcuts
and make sure 'Use keyboard navigation to move focus between controls' is checked."

This really seems like it's not the way it should be like, since other Touchbar apps functioned on my machine without having to set this setting, very strange!

Have a look kere for an example.

https://stackoverflow.com/questions/59682434/maccatalyst-swiftui-touch-bar


Notes:

- you should move this thread to SwiftUI.

- If you're a complete beginner, in Swift, in SwiftUI, in Mac app in general, may be you start with something a bit too complex which is not the best use of your hours.

Hey Claude,


thanks for the reply, I moved the thread, didn't find SwiftUI before.
I tried the solution you posted, raw project, as a new Project -> App (SwiftUI)
and then just changed the code from the ContentView according to the StackOverflow answer you posted.
I actually had something similar, and it doesn't show anything on the touchbar aswell. But the app starts as expected.

Do you want to post the complete code, so that we can have a try ?

Sure, here are the two classes (which are basically 95% vanilla new project):
https://pastebin.com/BZZ489yy

Accepted Answer

Ah, I see, my last post isn't public since the code I posted is being moderated *sigh*.

BUT, I got it to work with a little hint from this guy (Vincent) in the sub-answers:
https://stackoverflow.com/questions/59919050/how-can-i-display-touch-bar-buttons-using-swiftui
"I needed to open

System Preferences
>
Keyboard
>
Shortcuts
and make sure 'Use keyboard navigation to move focus between controls' is checked."

This really seems like it's not the way it should be like, since other Touchbar apps functioned on my machine without having to set this setting, very strange!

SwiftUI Touchbar
 
 
Q