0

我正在尝试通过 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 提供的示例演示应用程序,它运行良好。

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

4

1 回答 1

0

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

于 2020-05-06T12:19:30.780 回答