在 MacOS 上无法识别 CallBackURL

CallBackURL not recognized on MacOS

我正在尝试通过 Matt Donnelly (https://github.com/mattdonnelly/Swifter) 的 SwifterMac 功能在我的 MacOS 中启用 Twitter。

我在我的 AppDelegate 代码中实现了以下内容:

        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)
        }
    }

当我启动应用程序时,safari 会提示我使用有效的 Twitter 身份验证屏幕,但是当我单击授权时,我会收到消息

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

我假设有些东西没有正确注册,但我无法弄清楚缺少什么。我已经测试了 Swifter 提供的示例演示应用程序,它工作正常。

而且我已经检查我的应用程序沙盒是否设置为允许传入和传出通信(网络)。

看来我错过了 CFBundleURLName 的 plist 条目.. 详情在这里 - http://iosdevelopertips.com/cocoa/launching-your-own-application-via-a-custom-url-scheme.html