Construct and manage a graphical, event-driven user interface for your macOS app using AppKit.

AppKit Documentation

Posts under AppKit tag

245 Posts
Sort by:
Post not yet marked as solved
4 Replies
2.4k Views
Mac Mini (M1) macOS Monterey Version 12.0.1 (21A559) Xcode: Version 13.1 (13A1030d) Swift Language Version: 5 Today, I updated to Monterey. I started to work on an existing Xcode application after the Monterey update. Xcode immediately stated Xcode required an update too, so I did the update ... My applications now see two new items in the debug window after the Xcode update, stating the following: copy_read_only: vm_copy failed: status 1. Warning: Column selection is not supported in view-based table and outline views (<NSTableView: 0x12e0e1800>). Creating a new test application with Swift generates the first message too. Unfortunately, I did not read the Xcode message properly. I should have created a snapshot of the comment Xcode stated after the update, and before I restarted my computer. I wonder why Xcode generates the second warning too, when my applications do not select a column in any of my tableViews, although my applications retrieve, read, and write data source PList files to and from the Document Directiory, associated with the tableViews' displays. Then again, Xcode obviously knows something I do not know. As a side note, the first error is not consistent. I can start an existing application and the first warning appears. I quit the application, then restart the application, where the warning appears a few more times in succession. I repeat the process a few times, and then the application does not show the warning, including the new test application in Swift. I do have an older application with Xcode 12 where the warnings do not appear at all. If you have a moment, guidance would be appreciated, as to what I should (could) do to correct these new issues with Xcode, since I do not know where to start looking for a solution. I shall bet $$$ the issue happens to be the statement made by Xcode after the update. As always, thank you in advance with your suggestions. Best regards, jim_k
Posted
by jim_k.
Last updated
.
Post not yet marked as solved
1 Replies
201 Views
I developed a screen watermarking program that worked fine before macOS 12.4. After upgrading to 12.4, "System Extension Blocked" pop-up cann't be click, but the other window is ok. Any body can tell me the macOS 12.4 has do what to "System Extension Blocked" pop-up !!! my sample code like this code-block NSScreen *screen = [NSScreen screens][0]; CGFloat windowWidth = screen.frame.size.width; CGFloat windowHeight = screen.frame.size.height; CGFloat x = screen.frame.origin.x; CGFloat y = screen.frame.origin.y; MyView* view = [[MyView alloc]initWithFrame:NSMakeRect(0, 0, windowWidth, windowHeight)]; NSWindow* window = [[NSPanel alloc] initWithContentRect:NSMakeRect(0, 0, windowWidth, windowHeight) styleMask: NSWindowStyleMaskBorderless | NSWindowStyleMaskNonactivatingPanel backing:NSBackingStoreBuffered defer:NO]; [window setOpaque:NO]; [window setBackgroundColor:[NSColor clearColor]]; [window setCollectionBehavior:NSWindowCollectionBehaviorCanJoinAllSpaces | NSWindowCollectionBehaviorFullScreenAuxiliary]; [window setIgnoresMouseEvents:YES]; [window setHasShadow:NO]; [window setLevel:NSScreenSaverWindowLevel]; [window setContentView:views]; [window makeKeyAndOrderFront:nil]; and draw in MyView code-block - (void)drawRect:(NSRect)dirtyRect { NSRect screen = [self bounds]; int SW = screen.size.width; int SH = screen.size.height; [[NSColor clearColor] set]; NSRectFill(screen); NSString * strH= @"watermark test."; NSMutableDictionary *md = [NSMutableDictionary dictionary]; [md setObject:[NSFont fontWithName:@"Times" size:80] forKey:NSFontAttributeName]; [strH drawAtPoint:NSMakePoint(SH*0.5, SH*0.5) withAttributes:md]; [self setNeedsDisplay:YES]; }
Posted Last updated
.
Post not yet marked as solved
1 Replies
253 Views
How do I get updates about the file name change from the NSDocument window title? I noticed that despite file being property registered as a file presenter (by default), none of presentedItemDidChange, presentedSubitemDidChange, or presentedItemDidMove is called despite filename updates on the filesystem. the presentedItemURL does update value at some point. My subclass has autosave enabled, which makes title editing possible. override class var autosavesInPlace: Bool { true } [object Object]
Posted Last updated
.
Post not yet marked as solved
1 Replies
156 Views
Hi All, I 'm developing a SwiftUI based Mac app that amongst other things has a text editor. The editor is subclassed from NSTextView and some additional functionality added. It is also wrapped in a NSViewRepresentable to make it a SwiftUI View. I'd like to add a standard Find/Replace functionality to it - menu items, find/replace bar or panel, and etc. I did read the documentation on NSTextFinder and NSTextFinderClient, but it's kind of hard to see the full picture how these classes should be used together. Also, since this is SwiftUI based app, I'm not using story boards. If you know any examples, tutorials, or just code snippets that could help me get start, please share. Amy links are greatly appreciated. Thanks!
Posted
by ilia_saz.
Last updated
.
Post not yet marked as solved
9 Replies
671 Views
I have an app that displays photos from a directory that the user chooses (including children directories).  On launch it checks UserDefaults for stored values, if none from previous run, then asks user to open a directory of photos. Then initializes itself and saves the UserDefaults and also a persistent url so that on subsequent run can re-open directory without user intervention.   When first run and asks for directory, runs fine in setting up app state and saving UserDefaults and persistent url, but when gets to contentView the ImageView fails to show anything, but app continues running for about 15 or 20 sec. changing photo via timer, but no image displayed and then the crash @main hread 1: EXC_BREAKPOINT (code=1, subcode=0x1b8072158) and occurs seemingly async from the running code. Crash report attached. Then if re-launch app, it gets the saved UserDefaults, does set up and the runs fine, displaying images! Can even change directories (in the full app, with window commands which are not in this short version of the app. Crash Report
Posted
by jpromano.
Last updated
.
Post not yet marked as solved
2 Replies
193 Views
I'm evaluating switching an application device from windows10 to macOS but one of the key features needed that I'm unsure of is kiosk mode in macOS. Does something like that exist? The application would need to be the only app running (in full screen) and nothing else should show up. Even keyboard shortcuts should be disabled. Is that configuration possible? Is it something to be done in the app program or does it need UWP kind of stuff? Thanks
Posted
by CRX.
Last updated
.
Post not yet marked as solved
0 Replies
118 Views
How could I have both pan gesture recognizing (NSPanGestureRecognizer) and mouse capturing (CGAssociateMouseAndMouseCursorPosition(false)) working together? Is it possible at all?
Posted
by wasintw.
Last updated
.
Post not yet marked as solved
0 Replies
133 Views
Background: I'm targeting macOS 11 and up, running macOS 12.4 (21F79), and working with Xcode 13.4 (13F17a). I am building an application to show a list of VMs running under my account on a hosting provider. The VMs have a lot of simple properties like a UUID, a name, a comment field, a power state, and so on. They also have a few properties which are lists of relationships to other things. For example, a single VM can have several disks or several network adapters. The VMs can be organized in folders, so I'm trying to use NSOutlineView bound to a tree controller to show the downloaded data. The problem I have is that when I scroll my outline, it has significant animation hitches. Instruments tells me they are due to expensive commits. I originally built the NSOutlineView programmatically, but I've switched to building it in Interface Builder to get the NSTableCellView reuse behavior without needing to subclass and write my own implementations. I have an NSView subclass for the disk column's views and the network adapter column's views. These views present an objectValue property, which gets modified as expected. They then contain a SwiftUI VStack containing a ForEach which renders a row for each disk/interface. View reuse is working, and the internal SwiftUI view properly observes the field for new or removed disks/interfaces. It also properly observes the disk/interface objects themselves for changes (e.g, renaming). Scrolling still has hitches. The hitches get far worse when I bump the number of columns up to the 20 or so I want to use in the real application. I have built a small example project with an object factory instead of an API connection. The full application has a set of different icons representing different types of disk and network for different performance tiers. For now, I have simplified that to a single icon for each: https://github.com/Zimmie0/TableInOutlineExample My questions: Is there a better way to show a list of items with an image and a string of text per item inside a single NSOutlineView or NSTableView cell? Maybe instead of the SwiftUI stuff in the reusable views, I should be building an NSArrayController, an NSTableView with a single column, and using bindings and value transformers (to get an image from the text field indicating the resource's performance tier)? I'm not sure how to do that, though. I specifically don't want a scroll view and a clip view inside the top-level table cells. The top-level table rows should be tall enough objects are never hidden. I'm not sure how to build an NSTableView in interface builder (to get the NSTableCellView bindings and so on) without the scroll view and clip view around it. Also not sure how to tell the system to show the view from some XIB. Could the problem be related to usesAutomaticRowHeights? I'm aware that hurts performance, but I don't know to what extent. I can determine the row height myself easily enough, but I'm not sure how to tell the outline view that a given row's height needs to change in response to a disk or network adapter being added or removed.
Posted
by Zimmie.
Last updated
.
Post marked as solved
1 Replies
232 Views
I've been working on a macOS app using pure SwiftUI, and I've found a strange behavior that I've no idea how to solve. If you have enabled the System Preferences > Dock & Menu Bar > Minimize windows into application icon flag, when you minimize the window it never launches again when clicking the app icon on the dock. This does not happen with an AppKit application. The issue can be seen here (video + 2 sample projects): https://github.com/xmollv/macOSMinimizeIssueOnSwiftUI Literally they're empty apps, just created them using Xcode's assistant (the SwiftUI one has the steps to repro the issue in the UI). The only solution that I found to launch the SwiftUI one is rick clicking the app icon, and then selecting the window, but of course nobody expects that. My question is: is there any way launch a minimized SwiftUI app when clicking the dock icon?
Posted
by xmollv.
Last updated
.
Post not yet marked as solved
0 Replies
186 Views
I have a macOS app that synchronizes some files in the app's iCloud container, outside the Documents folder. Changes to these files are monitored with a NSMetadataQuery using a search scope of NSMetadataQueryUbiquitousDataScope. The NSMetadataQuery instance uses an operation queue for notifications and I'm calling startQuery on the instance's operation queue. As of macOS 12.3, for a subset of my users, the NSMetadataQueryDidFinishGatheringNotification notification is never sent after starting the query. The NSMetadataQueryDidStartGatheringNotification and NSMetadataQueryGatheringProgressNotification notifications are posted. For those affected who have multiple Macs, each of their Macs seems to be affected by this, as if it is a problem specific to the iCloud container for my app, for that user. I'm unable to reproduce the issue on my personal machine. Has anyone else noticed this behavior since macOS 12.3 was released?
Posted
by tundaware.
Last updated
.
Post not yet marked as solved
1 Replies
188 Views
I hope the table column show the values after I pressed the button. The following is the IBAction Button. - (IBAction)buttonTapped:(id)sender { //Filling the arrays Test_name = [NSArray arrayWithObjects:@"test_item_a",@"b",@"c",@"Current",nil]; Min = [NSArray arrayWithObjects:@"0.5",@"0.7",@"0.8",@"100",nil]; Max = [NSArray arrayWithObjects:@"5",@"5",@"9",@"140",nil]; uint16 index = 0; NSInteger i = 0; //Show_Datagrid this function move the above values to another 3 arrays table_item, //table_min, table_max. And the following code is just to verify the value. [self Show_Datagrid:index]; [self showMeg:@"Button Tapped!2\n"]; [self showMeg:[table_item objectAtIndex:0]]; [self showMeg:@"\n"]; [self showMeg:[table_min objectAtIndex:0]]; [self showMeg:@"\n"]; [self showMeg:Column_test_item.identifier]; //call this function to show the values in tableview. [self tableView:_tableview objectValueForTableColumn:Column_test_item row:i]; } following is the function (Column_test_item.identifier is verified correct.): -(id)tableView:(NSTableView *)aTableView objectValueForTableColumn:(NSTableColumn *)aTableColumn row:(NSInteger)rowIndex { if ([aTableColumn.identifier isEqualToString:@"no"]) return [table_no objectAtIndex:rowIndex]; if ([aTableColumn.identifier isEqualToString:@"item"]) return [table_item objectAtIndex:rowIndex]; if ([aTableColumn.identifier isEqualToString:@"min"]) return [table_min objectAtIndex:rowIndex]; if ([aTableColumn.identifier isEqualToString:@"max"]) return [table_max objectAtIndex:rowIndex]; if ([aTableColumn.identifier isEqualToString:@"value"]) return [table_value objectAtIndex:rowIndex]; if ([aTableColumn.identifier isEqualToString:@"status"]) return [table_status objectAtIndex:rowIndex]; return [table_no objectAtIndex:rowIndex]; } I thnik I called that function in wrong way. Shouldn't I input NSTableColumn parameter Column_test_item directly? Column_test_item is an IBOutlet I connected to the specfic test item column. And how does this function know which column it want to fill up the value, based on the second parameter I input?
Posted
by kp008114.
Last updated
.
Post not yet marked as solved
1 Replies
430 Views
Hi all, I'm currently implementing a feature that performs customized behavior in each desktop (space). As far as I know, Apple does not have an API that can enumerate all spaces under each screen. I've only found a way that can get all spaces, but cannot find any method on how to determine each space belongs to which screen. Can somebody help me out? Thanks in advance.
Posted Last updated
.
Post not yet marked as solved
1 Replies
266 Views
My app observes NSWorkspaceActiveSpaceDidChangeNotification and set wallpaper on the active screen. But I see there is no way to distinguish between spaces on a screen. I'm not sure if I have read the docs thoroughly. I would like to know if it is possible to tell which space is active for the active screen.
Posted
by imneo.
Last updated
.
Post not yet marked as solved
1 Replies
394 Views
By using NSWindowCollectionBehaviorCanJoinAllSpaces, I can make NSWindow show up on every already opened space, however, it won't show up on spaces that were added after app launch. How can I display NSWindow on spaces which were added after app launch? For example SideNotes app it can be shown on all spaces, on already opened ones before app launch and ones which were created after app launch.
Posted
by minhnh94.
Last updated
.
Post not yet marked as solved
0 Replies
128 Views
I have a NSRulerView with a vertical orientation. It works fine from macOS 10.13 to 11.x. In macOS Monterey (12.2.1 here), the ruler view is not receiving drawHashMarksAndLabelsInRect: messages when the associated NSTextView is scrolled vertically. When the parent NSScrollView is resized, the ruler view is correctly refreshed on all macOS versions. [Q] Is it a known bug in macOS Monterey?
Posted Last updated
.
Post not yet marked as solved
0 Replies
139 Views
I want to check the contents of an NSItemProvider to validate a drop action in SwiftUI on macOS. I use the code: let urlProviders = info.itemProviders(for: [.url]) if urlProviders.filter({ $0.canLoadObject(ofClass: NSImage.self) }).some { return true } But I get the compilation error: Instance method 'canLoadObject(ofClass:)' requires that 'NSImage' conform to '_ObjectiveCBridgeable' Is this a bug? The same code works with UIImage when compiling for iOS. Xcode 13.4
Posted
by Gillies.
Last updated
.
Post not yet marked as solved
3 Replies
3.5k Views
In my MacApp I am using custom fonts. These fonts are stored in /Resourcese/Fonts folder and Copy phase is written for the same.I noticed that, if I have used these fonts in Xib and I run the app from Xcode. UI does not show up these fonts unless it is installed in Font Book.However, same build if I have published to AppStore and I download from AppStore, custom fonts does show up.My question is that why does Custom font not show up while we run through Xcode?-Srinivas
Posted Last updated
.
Post not yet marked as solved
0 Replies
192 Views
Hello, I have an app, which runs fine on Monterey, but crashes on Big Sur. However, I am able to run it on Big Sur from XCode only. System Integrity Protection: enabled Crashed Thread: 0 Dispatch queue: com.apple.main-thread Exception Type: EXC_BAD_ACCESS (SIGSEGV) Exception Codes: KERN_INVALID_ADDRESS at 0x000004079c498ca0 Exception Note: EXC_CORPSE_NOTIFY Termination Signal: Segmentation fault: 11 Termination Reason: Namespace SIGNAL, Code 0xb Terminating Process: exc handler [3858] VM Regions Near 0x4079c498ca0: mapped file 11e4ff000-11e607000 [ 1056K] r--/rw- SM=COW Object_id=6ea347b --> MALLOC_NANO 600000000000-600008000000 [128.0M] rw-/rwx SM=PRV Thread 0 Crashed:: Dispatch queue: com.apple.main-thread 0 libobjc.A.dylib 0x00007fff2040f4af objc_release + 31 1 com.apple.UIFoundation 0x00007fff23c1aba8 -[UINibDecoder dealloc] + 119 2 com.apple.AppKit 0x00007fff22fef7ec -[NSNib dealloc] + 28 3 libobjc.A.dylib 0x00007fff2042d20f AutoreleasePoolPage::releaseUntil(objc_object**) + 167 4 libobjc.A.dylib 0x00007fff2040fe30 objc_autoreleasePoolPop + 161 5 com.apple.CoreFoundation 0x00007fff206284b6 _CFAutoreleasePoolPop + 22 6 com.apple.Foundation 0x00007fff213ca0bb -[NSAutoreleasePool drain] + 131 7 com.apple.Foundation 0x00007fff213cb198 _NSAppleEventManagerGenericHandler + 98 8 com.apple.AE 0x00007fff264487f3 0x7fff2643c000 + 51187 9 com.apple.AE 0x00007fff26447f0e 0x7fff2643c000 + 48910 10 com.apple.AE 0x00007fff26440c23 aeProcessAppleEvent + 448 11 com.apple.HIToolbox 0x00007fff288bf8a2 AEProcessAppleEvent + 54 12 com.apple.AppKit 0x00007fff22e6e210 _DPSNextEvent + 2046 13 com.apple.AppKit 0x00007fff22e6c545 -[NSApplication(NSEvent) _nextEventMatchingEventMask:untilDate:inMode:dequeue:] + 1364 14 com.apple.AppKit 0x00007fff22e5e869 -[NSApplication run] + 586 15 com.apple.AppKit 0x00007fff22e32a6c NSApplicationMain + 816 16 com.MyCompany.MyApp. 0x000000010d699799 main + 9 (AppDelegate.swift:15) 17 libdyld.dylib 0x00007fff2058af3d start + 1 There are some pictures(please open them in new tab) from Zombie instrument: Regarding Zombie, it looks like some error happens in this code in @NSApplicationMain class AppDelegate: NSObject, NSApplicationDelegate, Loggable  is the method fileprivate func handleFSE(event: NSAppleEventDescriptor?, replyEvent: NSAppleEventDescriptor?) {     Defaults[.deepLinkData] = event?.paramDescriptor(forKeyword: keyDirectObject)?.stringValue     Defaults[.isFromCUSLaunch] = true     if let window = WindowControllerFrom(storyboard: .cus, withIdentifier: .cusWindow) {       logIfCan(string: "Deeplink data: \(event?.paramDescriptor(forKeyword: keyDirectObject)?.stringValue ?? .empty)")       let cusVC = CUSViewController(event?.paramDescriptor(forKeyword: keyDirectObject)?.stringValue ?? .empty)       window.contentViewController = cusVC       if let screen = NSScreen.main {         window.window?.setFrame(screen.visibleFrame, display: true, animate: true)       }       window.window?.makeKeyAndOrderFront(nil)     }   } WindowControllerFrom returns NSWindowController? and CUSViewController is a class which inherits from AbstractViewController. I would appreciate any idea what can be wrong here, and why there is no problem on Monterey (both M1 and Intel), but crashes on Big Sur?
Posted
by OShv.
Last updated
.
Post marked as solved
5 Replies
1.8k Views
Hi, on my macOS app I would like to set the window background color as the titlebar background color such a way to get a unique color all over the window on both light and dark mode. But I cannot find the way to read the titlebar background color nor I found a standard color name for that. How can I do that?
Posted Last updated
.
Post not yet marked as solved
0 Replies
90 Views
I want to use the Fn modifier key if available, and if not, offer the user a different modifier. But only offer it to users without a Fn key. Some external keyboard do not have a Fn key. Is there any way to detect whether the active keyboard has a physical Fn key? (I am not asking how to detect a Fn key press) I'm looking for a solution in Swift that works within a sandboxed app. I already tried asking on Stack Overflow.
Posted Last updated
.