White status bar with "App Scene WindowGroup" - swift

With the new App Scene WindowGroup structure in Xcode 12, how can you specify the white status bar? I've searched around but so far have not been able to find an answer.
#main
struct MyApp: App {
var body: some Scene {
WindowGroup {
AppView()
}
}
}

Answered in the Apple Developer Forum: https://developer.apple.com/forums/thread/658539

Open your project in Xcode. form the left navigator(it is usually open, but if its not, you can open it by (command + 0) ) select Your_Project_Name.xcodeporj file (usually the first file, with a mainly blue foreground with appstore icon on it). select your ios-macos target from the targets (or any other targets which do have an status bar to configure). make sure youre on the general tab. one of the section should be Development Info which includes a status bar part. configure the status bar there.

Related

Why does the MacMenuBarExtra label only work for sf symbol images and text?

I'm using a MacMenuBarExtra for my macOS app, however, I can't seem to make any view that's not a sf symbol image or text render properly. Here is the code I am using in my app file and if you run it, you'll see that no icon shows in the menu bar. I want to create a dynamic icon which will show up there, does anyone have any idea why this is happening?
MenuBarExtra {
ContentView()
} label: {
Circle().foregroundColor(.green)
}
.menuBarExtraStyle(.window)

My CPU Usgage is constant 100% (Xcode, using SwiftUI)

My SwiftUI App is extremly lagging and I have no Idea why. Even if I only compile this code, my CPU Power goes up to 100%, even if I dont touch anything:
#main
struct Order_Corner_V8App: App {
var body: some Scene {
return WindowGroup {
TabView{
Text("").tabItem {
Text("click")
}
Text("").tabItem {
Text("click")
}
}
}
}
}
I need the TabView. I put my whole Files in an other new Project, the same thing happend again. Maybe its usefull to know that im working with Firebase.
I'm targeting iOS.
I've already watched to my App in 'Instruments', it only says that the problem is my main View.
Please help me.
Here is some image that shows my CPU Data
Thanks, Boothosh
Okay I got the solution: I tried the App on an other phone and everything works fine. I think this is an mistake of my phone, but I don't know what I can do against that.
Thanks, Boothosh
In my case, I found Xcode source control caused the problem
To fix this problem Xcode -> Preferences -> Source Control uncheck Enable Source control. Then restart Xcode, everything will be fine

NSWorkspace.shared.icon(forFile:) does not return icon image

I am working on a MacOS application where the app should show the application icons. For some app it works, for some apps it don't.
One the app for which I do not get the application icon image is the Notes.app.
let icon = NSWorkspace.shared.icon(forFile: "/Applications/Notes.app")
For Numbers.app, for example, it is loading the app icon as expected.
What is the reason behind?
I have figured out that icons are only appearing for those applications where the application has an associated filetype. Applications which do not have associations does not return the icon.
There must be a was to open an .icns file. The path to it is clearly accessible and the content of the icon file should be too.
Ok. I have figured out that some Applications are located under /System/Applications, but appears under /Applications too. This is causing the problem above.

Unable to see preview and unable to see the main window of my app (when running) while using swift ui for macOS developement

So I created a macOS cocoa project called "test" using swift ui in Xcode 11 beta and got problems at the very beginning.
Sorry in advance if my question is dumb.
The first problem is that the automatic preview says it is in pause. If I click resume, I got an error.
So I tried to run the code Xcode generated (I changed nothing in the code.) and it build successfully. When I tried to run the code freshly build, I come across the following error: EXC_BAD_INSTRUCTION
The line highlighted in red is the .frame line in the following piece of code:
import SwiftUI
struct ContentView : View {
var body: some View {
Text("Hello World")
.frame(maxWidth: .infinity, maxHeight: .infinity)
}
}
#if DEBUG
struct ContentView_Previews : PreviewProvider {
static var previews: some View {
ContentView()
}
}
#endif
I'm on macOS Catalina Public Beta 1 so this is not a problem with the version of the system....
So, did I miss something? The tutorials I found online (I mostly looked on Youtube and Apple Dev websites) either show examples for iOS or show examples that are not starting from scratch ( For example on Apple websites, when they are demo-ing codes for macOS apps ).
Thanks in advance
EDIT
I'm adding this picture, it might help:
Actual screen when the app is running with the info of the threads section
Final answer:
Just needed an update Xcode to beta 2 :) .

Accessibility Permissions for Development in macOS and XCode

Is there a way to give an app I am developing in XCode accessibility permissions by default during development. The idea is that I could hit the run key and test the new code without having to jump through the hoops in the settings. For deployment obviously it wouldn't work, but for development is there a way to basically whitelist the app?
EDIT: This is a new method I've found/created that is working and has the added benefit of prompting the user first too, improving the onboarding experience of your app. The original method (which I've left at the bottom of this post) still prompted for accessibility on each new build of the app unfortunately.
I had to get this working in my macOS app and had another idea and way to approach it, given the user needs to action it anyway, I just have the app present the required dialogue on each run and during the app build, I run a script to clear the existing permissions.
For reference, see v1.3.0 of my Auto Clicker macOS app, specifically:
Services/PermissionsService.swift (whole class)
Init/AppDelegate.swift (line 21)
Init/AutoClickerApp.swift (line 32 & 40)
auto-clicker.xcodeproj/project.pbxproj (line 435)
The links are links to the tagged version, so should never change or break. There is quite a bit of code, I'll try to summarise here.
Firstly, I created a new class to manage permissions related functionality to keep things contextual:
//
// PermissionsService.swift
// auto-clicker
//
// Created by Ben on 10/04/2022.
//
import Cocoa
final class PermissionsService: ObservableObject {
// Store the active trust state of the app.
#Published var isTrusted: Bool = AXIsProcessTrusted()
// Poll the accessibility state every 1 second to check
// and update the trust status.
func pollAccessibilityPrivileges() {
DispatchQueue.main.asyncAfter(deadline: .now() + 1.0) {
self.isTrusted = AXIsProcessTrusted()
if !self.isTrusted {
self.pollAccessibilityPrivileges()
}
}
}
// Request accessibility permissions, this should prompt
// macOS to open and present the required dialogue open
// to the correct page for the user to just hit the add
// button.
static func acquireAccessibilityPrivileges() {
let options: NSDictionary = [kAXTrustedCheckOptionPrompt.takeRetainedValue() as NSString: true]
let enabled = AXIsProcessTrustedWithOptions(options)
}
}
Then, I added the following to my AppDelegate (baring in mind this was in Swift UI):
//
// AppDelegate.swift
// auto-clicker
//
// Created by Ben on 30/03/2022.
//
import Foundation
import Cocoa
final class AppDelegate: NSObject, NSApplicationDelegate {
// When the application finishes launching, request the
// accessibility permissions from the service class we
// made earlier.
func applicationDidFinishLaunching(_ notification: Notification) {
PermissionsService.acquireAccessibilityPrivileges()
}
}
Then finally, in our Swift UI app init, lets add some UI feedback to the user that we are waiting for permissions and listen to that isTrusted published property we setup earlier in the permissions service class that gets polled every second to unlock the UI when the user has granted the required permissions:
//
// AutoClickerApp.swift
// auto-clicker
//
// Created by Ben on 12/05/2021.
//
import Foundation
import SwiftUI
import Defaults
#main
struct AutoClickerApp: App {
// Create an instance of the permissions service class that we
// can observe for the trusted state change.
#StateObject private var permissionsService = PermissionsService()
var body: some Scene {
// Wait for trust permissions being granted from the user,
// displaying a blocking permissions view telling the user
// what to do and then presenting the main application view
// automatically when the required trust permissions are granted.
WindowGroup {
if self.permissionsService.isTrusted {
MainView()
} else {
PermissionsView()
}
.onAppear(perform: self.permissionsService.pollAccessibilityPrivileges)
}
}
}
You can see the blocking view I made in the app above, Views/Main/PermissionsView.swift.
Then in order to automatically clear the permissions during the app build, I added a new build script to the project that runs the following against /bin/sh:
tccutil reset Accessibility $PRODUCT_BUNDLE_IDENTIFIER
As seen in auto-clicker.xcodeproj/project.pbxproj (line 435).
This means I'll be prompted with the system dialogue on each app build to just press the + button and add the app.
Unfortunately this is the most frictionless way I've found to develop the app with these permissions being required.
Outdated answer:
Found a way to do this after some trial and error, navigating through Xcode's (>11, currently 13) new build system.
With Xcode open and having it as the foreground app (so it takes over the menu bar with its menu items), do the following:
From the menu bar, select 'Xcode'
If your project doesn't yet have a workspace, click 'Save as Workspace...' near the bottom of the list and save the Workspace alongside your *.xcodeproj so they should be in the same directory. From now on you'll open your project via the new *.xcworkspace Workspace instead of your *.xcodeproj Project.
From the menu bar, select 'Xcode' again
Click 'Workspace Settings...' near the bottom of the list
Under 'Derived Data' select 'Workspace-relative Location', if you want to customise the path do so now
Click 'Done' in the bottom right
This puts the build location of our *.app binary within the project so its easy to find, along with also allowing us to check the change into source control as we now have the Workspace settings stored in the *.xcworkspace file.
Next we need to now point the permissions at the above build binaries location, so:
Open System Preferences
Click 'Security & Privacy'
Click the padlock in the bottom right to make changes
Select 'Accessibility' from the left side list
Click the plus button at the bottom left of the list and find the *.app file to add it to the list that we put within the project directory, this should be something like $PROJECT_DIR/DerivedData/$PROJECT/Build/Products/Debug/*.app
Click the checkbox to the left of the app to check it if its not already checked
Restart the app
Any builds should now have the relevant permissions.
Just to note though, this will overwrite the permissions of any archived/prod builds as the app names and binaries are the same. They are easily added back though, just delete the permission for the build app and instead point it back at your prod app, usually in /Applications.