Search code examples
objective-cswiftmacosnsworkspace

openUrl command works in Obj-C but not Swift


Our codebase is still very much a hybrid codebase, but I've been trying to do more work in Swift. Unfortunately I'm running into a weird situation where a command that works fine in Obj-C absolutely refuses to run in Swift.

I'm trying to send a file from one of our desktop products to another of our products. All of the URL schemes are set up correctly. This command in Obj-C works perfectly:

[[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:@"ourotherapp://v1/import?filePath=/Users/gi.joe/Documents/test.wav"]];

However, in trying to use the Swift version, this one absolutely refuses to execute. No exception, no clue, no nothing:

NSWorkspace.shared.open(URL(fileURLWithPath: "ourotherapp://v1/import?filePath=/Users/gi.joe/Documents/test.wav"))

Is there some difference in the way NSWorkspace works in Swift as opposed to Obj-C? I've looked over the Apple docs but maybe I missed something?


Solution

  • I see that you tried two different calls in Obj C and Swift.

    I haven't tried the below code, maybe this example helps you:

    if let checkURL = NSURL(string: "https://google.com") {
        if NSWorkspace.shared().open(checkURL as URL) {
            print("URL Successfully Opened")
        }
    } else {
        print("Invalid URL")
    }