swiftmacostwittercallbackurl

CallBackURL not recognized on MacOS


I am trying to enable Twitter in my MacOS via the SwifterMac feature from Matt Donnelly (https://github.com/mattdonnelly/Swifter).

I have implemented the following in my AppDelegate code:

        let swifter = Swifter(consumerKey: "MYVALIDKEY", consumerSecret: "MYVALIDSECRET")

        override init(){
            super.init()

            let callbackUrl = URL(string: "registeredcallback://success")!
            swifter.authorize(withCallback: callbackUrl, success: { _, _ in
                self.swifter.postTweet(status: "test",success: { status in
                    print("successfully tweeted! \(status.description)")
                }, failure: { error in
                    print("error tweeting! \(error)")
                })
            }, failure: failureHandler)
        }

        func applicationDidFinishLaunching(_ notification: Notification) {
            NSAppleEventManager.shared().setEventHandler(self, andSelector: #selector(AppDelegate.handleEvent(_:withReplyEvent:)), forEventClass: AEEventClass(kInternetEventClass), andEventID: AEEventID(kAEGetURL))
            LSSetDefaultHandlerForURLScheme("registeredcallback" as CFString, Bundle.main.bundleIdentifier! as CFString)
            let contentView = ContentView()
            swifter.postTweet(status: "test",success: { status in
                      print("successfully tweeted! \(status.description)")
                  }, failure: { error in
                      print("error tweeting! \(error)")
                  }
            )
            // Create the window and set the content view.
            window = NSWindow(
                contentRect: NSRect(x: 0, y: 0, width: 400, height: 640 ),
                styleMask: [.titled, .closable, .miniaturizable, .fullSizeContentView],
                backing: .buffered, defer: false)
            window.center()
            window.setFrameAutosaveName("Main Window")
            window.contentView = NSHostingView(rootView: contentView)
            window.makeKeyAndOrderFront(nil)
        }

        @objc func handleEvent(_ event: NSAppleEventDescriptor!, withReplyEvent: NSAppleEventDescriptor!) {
            guard let callbackUrl = URL(string: "registeredcallback://success") else { return }
            guard let urlString = event.paramDescriptor(forKeyword: AEKeyword(keyDirectObject))?.stringValue else { return }
            guard let url = URL(string: urlString) else { return }
            Swifter.handleOpenURL(url, callbackURL: callbackUrl)
        }
    }

When I launch the app, safari prompts me with a valid twitter authentication screen, but when I click authorize, I receive the message

Safari can't open "registeredcallback://success?oauth_token=VALIDTOKEN&oautho_verifier=ANOTHERVALIDVALUE" because macOS doesn't recognize Interest addresses starting with "registeredcallback:".

I assume that something is not registered correctly, however I can't figure out what is missing. I have tested the sample demo app that is provided with Swifter and it works fine.

And I have checked that my app sandboxing is set to allow both incoming and outgoing communications (Network).


Solution

  • Looks like I missed the plist entries for the CFBundleURLName.. details are here - http://iosdevelopertips.com/cocoa/launching-your-own-application-via-a-custom-url-scheme.html