accountsWithAccountType return 空数组

accountsWithAccountType return empty array

我正在关注 CS193P 并试图从截屏视频中复制。在一个截屏视频中,有一个获取 twitter API 并将其显示为 tableView 的示例。但是当我尝试它时,accountsWithAccountType return 空数组。结果,它导致 else 条件,并显示为 Couldn't discover Twitter account type。我的代码如下

let accountStore = ACAccountStore()
        let twitterAccountType = accountStore.accountTypeWithAccountTypeIdentifier(ACAccountTypeIdentifierTwitter)
        accountStore.requestAccessToAccountsWithType(twitterAccountType, options: nil) { (granted, _) in

            if granted {
                //  test is empty array 
                let test = accountStore.accountsWithAccountType(twitterAccountType)
                println(test)
                if let account = accountStore.accountsWithAccountType(twitterAccountType)?.last as? ACAccount {
                    twitterAccount = account
                    self.performTwitterRequest(request, handler: handler)
                } else {
                    let error = "Couldn't discover Twitter account type."
                    self.log(error)
                    handler(error)
                }
            } else {
                let error = "Access to Twitter was not granted."
                self.log(error)
                handler(error)
            }
        }

当我尝试打印 twitterAccountType 时,它显示 Twitter (com.apple.twitter)。顺便说一句,我还从 link 下载了它的工作版本并进行了尝试。它仍然显示相同的错误。

看起来您在设置中没有 Twitter 帐户。

转到“设置”>“Twitter”>“使用您的 Twitter ID 登录”

然后重试。

我在设备和模拟器上进行了测试。添加 twitter 帐户后找到它正在工作。