1

我正在尝试从我的应用程序向 twitter 发布一些内容,并且由于 iOs 11 不幸的是旧方式不再适用,所以我正在实施 twitterKit 并发现一些峰值。

当我没有安装应用程序时,它会运行下面的完成块,这很奇怪,因为我必须自己手动关闭警报,因为警报没有任何按钮可以执行此操作。

但我真正的问题是我安装了 twitter 应用程序并且我已登录。但我无法使用 twitter 工具包检测到它。当我按下分享到推特按钮时,应用程序切换到新视图,它是否要求我将我的应用程序连接到我的推特(如果我没有登录,我有一个登录名和密码框,但结果总是同样...)当我按“连接”时,视图返回到我的应用程序并且没有任何反应,永远不会调用完成块...我正在使用 iOs 11 和 x-code 9 但我已经尝试过与 iOs 10 相同的方法,我得到相同的结果。从未检测到 Twitter 登录。这是我正在运行的代码,任何帮助将不胜感激:

if (Twitter.sharedInstance().sessionStore.hasLoggedInUsers()) {
    // App must have at least one logged-in user to compose a Tweet
    let composer = TWTRComposerViewController.emptyComposer()
    present(composer, animated: false, completion: {
        print("This code never runs")
    })
} else {
    // Log in
    Twitter.sharedInstance().logIn { session, error in
        if session != nil {
            // Log in succeeded / Never happens
            let composer = TWTRComposerViewController.emptyComposer()
            composer.delegate = self
            self.present(composer, animated: true, completion: {
                print ("This code never runs")
            })
        } else {
            let alert = UIAlertController(title: "No Twitter Accounts Available", message: "You must log in before presenting a composer.", preferredStyle: .alert)

            //Only happens if I don't have the twitter app installed on my device
            self.present(alert, animated: false, completion: {
                print ("not loggued in")

                /*
                 manual dismission of the prompt as it don't have
                 any button
                 */
                sleep(3)
                alert.dismiss(animated: true, completion: nil)

            })
        }
    }
}

在控制台中,我收到此错误: [Snapshotting] 对至少未渲染一次的视图(0x105977000,UIKeyboardImpl)进行快照需要 afterScreenUpdates:YES。

编辑:我解决了它在appDelegate中添加这个方法:

func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey: Any] = [:]) -> Bool { return Twitter.sharedInstance().application(app, open: url, options: options) }

4

1 回答 1

1

正如您所发现的,当应用程序从 twitter 重定向回来时,您需要让 TwitterKit 处理重新打开应用程序:

func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool {
    let twtrHandled = TWTRTwitter.sharedInstance().application(app, open: url, options: options)
    return twtrHandled
}

如果你有几个可以处理 URL 的工具包,这就是我处理它的方式(这里我也使用 facebook SDK 和 Branch SDK):

func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool {
    let branchHandled = Branch.getInstance().application(app, open: url, options: options)
    let fbHandled = SDKApplicationDelegate.shared.application(app, open: url, options: options)
    let twtrHandled = TWTRTwitter.sharedInstance().application(app, open: url, options: options)
    return branchHandled || fbHandled || twtrHandled
}
于 2018-03-26T08:37:45.217 回答