Post

Replies

Boosts

Views

Activity

Content size of NSWindow returns zero frame after setting view on macOS 15.0 and xCode 16.1
I would like to show a nswindow at a position on screen base on height of the nswindow and its content view. I received zero width and height on macOS 15.0 and xCode 16.1 however they were returned correct width and height on previous macOS version. import Cocoa import SwiftUI class AppDelegate: NSObject, NSApplicationDelegate { private var window: NSWindow! func applicationDidFinishLaunching(_ aNotification: Notification) { window = NSWindow( contentRect: .zero, styleMask: [.miniaturizable, .closable, .resizable], backing: .buffered, defer: false) window.title = "No Storyboard Window" window.contentView = NSHostingView(rootView: ContentView()) // a swiftui view window.center() let windowFrame = window.frame print("window Frame \(windowFrame)") // print width and height zero here window.makeKeyAndOrderFront(nil) } } struct ContentView: View { var body: some View { VStack { Image(systemName: "globe") .imageScale(.large) .foregroundStyle(.tint) Text("Hello, world!") } .padding() } } I tried window.layoutIfNeeded() after setting contentview but it didn't work How can I get the frame after setting contentview for nswindow on macOS 15.0?
1
0
222
2w
How to keep a socket server in an iOS app alive when app goes to background or suspended?
Hi all, My application requires to create a WebSocket server on an iOS application for other devices can connect and transfer data with my application. I used Vapor library to create a socket server and it works well when the application is in the foreground. I am trying to keep the server alive when my app moves to the background or the suspended state so that my app and other devices can continue to communicate with each other. Is there any ways to achieve that? I tried to turn on a mode: "Audio, Airplay, and Picture in Picture" in background modes section in Signing & Capabilities and then my application can still communicate with clients when it is background mode. But my application is an application for user can edit image and send it to other devices through sockets and it does not have audio, airplay,.. feature. Is it ok to publish the app to the app store in the future? Thank you!
2
0
1.1k
Apr ’24
How to listen for Privacy & Security > Automation > System Events status changes in MacOS Swift
My project (an non-sandbox app) was written in Swift on MacOS and it can execute the Apple script successfully. I would like to listen for Privacy & Security > Automation > System Events status changes when the user changes it in System Settings to disable or enable my app feature in MacOS. My app can receive Accessibility changes through this notification "com.apple.accessibility.api" Are there any system notifications for my app to receive Automation > System Events status changes? Thank you!
2
1
741
Feb ’24
How to implement ping an IP in MacOS Swift?
Hi all, As far as my research, I found there are two ways to ping an IP Address (ipv4 and ipv6) in MacOS in Swift. SimplePing Run command line in code like below func doPing() { let task = Process() task.executableURL = URL(fileURLWithPath: "/sbin/ping") task.arguments = [ "-c", "4", "10.1.141.100" ] let outputPipe = Pipe() task.standardOutput = outputPipe let outputHandle = outputPipe.fileHandleForReading outputHandle.readabilityHandler = { pipe in if let ouput = String(data: pipe.availableData, encoding: .utf8) { if !ouput.isEmpty { log += ouput print("----> ouput: \(ouput)") } } else { print("Error decoding data: \(pipe.availableData)") } } task.launch() } So which way is better and which way should I implement for the project?
3
0
674
Jan ’24
How to get external display information after choosing use as separate as display via screen mirroring on MacOS?
I would like to get some information of the connected display such as vendor number, eisaId, … after connecting the external display via “screen mirroring” -> “use as Separate Display” When the same display was connected through HDMI port or extend mode in screen mirroring, the information is not identical: HDMI Other display found - ID: 19241XXXX, Name: YYYY (Vendor: 19ZZZ, Model: 57WWW) Screen mirroring - extend mode Other display found - ID: 41288XX, Name: AAA (Vendor: 163ZYYBBB, Model: 16ZZWWYYY) I tried to get display information with the below method. func configureDisplays() { var onlineDisplayIDs = [CGDirectDisplayID](repeating: 0, count: 16) var displayCount: UInt32 = 0 guard CGGetOnlineDisplayList(16, &onlineDisplayIDs, &displayCount) == .success else { os_log("Unable to get display list.", type: .info) return } for onlineDisplayID in onlineDisplayIDs where onlineDisplayID != 0 { let name = DisplayManager.getDisplayNameByID(displayID: onlineDisplayID) let id = onlineDisplayID let vendorNumber = CGDisplayVendorNumber(onlineDisplayID) let modelNumber = CGDisplayModelNumber(onlineDisplayID) let serialNumber = CGDisplaySerialNumber(onlineDisplayID) if !DEBUG_SW, DisplayManager.isAppleDisplay(displayID: onlineDisplayID) { let appleDisplay = AppleDisplay(id, name: name, vendorNumber: vendorNumber, modelNumber: modelNumber, serialNumber: serialNumber, isVirtual: isVirtual, isDummy: isDummy) os_log("Apple display found - %{public}@", type: .info, "ID: \(appleDisplay.identifier), Name: \(appleDisplay.name) (Vendor: \(appleDisplay.vendorNumber ?? 0), Model: \(appleDisplay.modelNumber ?? 0))") } else { let otherDisplay = OtherDisplay(id, name: name, vendorNumber: vendorNumber, modelNumber: modelNumber, serialNumber: serialNumber, isVirtual: isVirtual, isDummy: isDummy) os_log("Other display found - %{public}@", type: .info, "ID: \(otherDisplay.identifier), Name: \(otherDisplay.name) (Vendor: \(otherDisplay.vendorNumber ?? 0), Model: \(otherDisplay.modelNumber ?? 0))") } } } Can we have the same display information when connect with an external display via HDMI port and extend mode in Screen Mirroring?
0
0
624
Aug ’23
How to perform TLS Handshake with NWConnection after connected TCP to Server
Currently, I have a client by using NWConnection for a socket connection to a server in local network. My server address is ***.***.***.***:YYYY The client can connected to the server with the code below: func connect() { let connection = NWConnection(host: .init("***.***.***.***"), port: .init(integerLiteral: YYYY), using: NWParameters(tls: nil, tcp: .init())) connection.stateUpdateHandler = { state in print(state) if state == .ready { receiveData() } } connection.start(queue: .global()) } private func receiveData() { self.connection?.receive(minimumIncompleteLength: 1, maximumLength: 8192) { [weak self] (data, context, isComplete, error) in guard let self = self else { return } if let error = error { self.socketConnectionStateCallBack(.onError(connection: self, error: error)) return } if let connection = connection, connection.state == .ready && isComplete == false, let data = data, !data.isEmpty { self.socketConnectionStateCallBack(.onDataReceived(connection: self, data: data)) } } } The stateUpdateHandler callback with state == .ready and there is a receive method in that block also, so the client receive an encrypted String from the server. At this time, the client should do TSL handshake with server. (I have a certificate file) I already tried configuring TLS in NWParameters: func createTLSParameters(allowInsecure: Bool, queue: DispatchQueue) -> NWParameters { let options = NWProtocolTLS.Options() sec_protocol_options_set_verify_block(options.securityProtocolOptions, { (sec_protocol_metadata, sec_trust, sec_protocol_verify_complete) in let trust = sec_trust_copy_ref(sec_trust).takeRetainedValue() var error: CFError? if SecTrustEvaluateWithError(trust, &error) { sec_protocol_verify_complete(true) } else { if allowInsecure == true { sec_protocol_verify_complete(true) } else { sec_protocol_verify_complete(false) } } }, queue) return NWParameters(tls: options) } but received the errors: 2023-06-26 13:44:52.793596+0700 TestNWConnection[8571:237696] [boringssl] boringssl_context_handle_fatal_alert(1991) [C1:4][0x7f9807c051f0] write alert, level: fatal, description: protocol version 2023-06-26 13:44:52.793784+0700 TestNWConnection[8571:237696] [boringssl] boringssl_context_error_print(1981) [C1:4][0x7f9807c051f0] Error: 140290895852456:error:100000f7:SSL routines:OPENSSL_internal:WRONG_VERSION_NUMBER:/AppleInternal/Library/BuildRoots/9c39860a-c3e2-11ed-88f7-863efbbaf80d/Library/Caches/com.apple.xbs/Sources/boringssl/ssl/tls_record.cc:242: 2023-06-26 13:44:52.794547+0700 TestNWConnection[8571:237696] [boringssl] boringssl_session_handshake_incomplete(88) [C1:4][0x7f9807c051f0] SSL library error 2023-06-26 13:44:52.794617+0700 TestNWConnection[8571:237696] [boringssl] boringssl_session_handshake_error_print(43) [C1:4][0x7f9807c051f0] Error: 140290895852456:error:100000f7:SSL routines:OPENSSL_internal:WRONG_VERSION_NUMBER:/AppleInternal/Library/BuildRoots/9c39860a-c3e2-11ed-88f7-863efbbaf80d/Library/Caches/com.apple.xbs/Sources/boringssl/ssl/tls_record.cc:242: 2023-06-26 13:44:52.794660+0700 TestNWConnection[8571:237696] [boringssl] nw_protocol_boringssl_handshake_negotiate_proceed(771) [C1:4][0x7f9807c051f0] handshake failed at state 12288: not completed waiting(-9836: bad protocol version) 2023-06-26 13:44:52.833700+0700 TestNWConnection[8571:238121] [tcp] tcp_input [C1:5] flags=[R.] seq=764001948, ack=1321044260, win=506 state=CLOSED rcv_nxt=764000508, snd_una=1321044252 So can you help me to perform TLS Handshake with NWConnection after connected TCP to Server? Many thanks!
8
0
1.2k
Jun ’23