Twitter分享不在设备上工作

时间:2015-06-25 07:26:16

标签: ios swift twitter slcomposeviewcontroller

我的推特共享正在模拟器上运行,但不在设备上(iPhone 6)。

if SLComposeViewController.isAvailableForServiceType(SLServiceTypeTwitter){
    let url = self.articles?[indexPath.row]["article_url"].string!
    let targetURL = NSURL(string: url!)
    let articleTitle = self.articles?[indexPath.row]["title"].string!
    let authorName = self.articles?[indexPath.row]["author_name"].string!
    let articleID = self.articles?[indexPath.row]["article_id"].int!

    var twitterSheet:SLComposeViewController = SLComposeViewController(forServiceType: SLServiceTypeTwitter)
    var twitterMessage = String(authorName!) + " " + String(articleTitle!) + " @yazarappio"

    twitterSheet.setInitialText(twitterMessage)
    twitterSheet.addURL(targetURL)
    self.presentViewController(twitterSheet, animated: true, completion: nil)
    UIPasteboard.generalPasteboard().string = url
}

在Xcode上我对这段代码没有问题,但是为了在设备上运行,我添加了这段代码:

let account = ACAccountStore()
let accountType = account.accountTypeWithAccountTypeIdentifier(
            ACAccountTypeIdentifierTwitter)

account.requestAccessToAccountsWithType(accountType, options: nil, 
    completion: {(success: Bool, error: NSError!) -> Void in

    if success {
        let arrayOfAccounts = 
        account.accountsWithAccountType(accountType)

        if arrayOfAccounts.count > 0 {
            let twitterAccount = arrayOfAccounts.last as! ACAccount
            let message = ["status" : "My first post from iOS 8"]
            let requestURL = NSURL(string: 
        "https://api.twitter.com/1.1/statuses/update.json")
            let postRequest = SLRequest(forServiceType: 
            SLServiceTypeTwitter, 
            requestMethod: SLRequestMethod.POST, 
            URL: requestURL, 
            parameters: message)

            postRequest.account = twitterAccount

            postRequest.performRequestWithHandler({
            (responseData: NSData!, 
             urlResponse: NSHTTPURLResponse!, 
             error: NSError!) -> Void in

                if let err = error {
                    println("Error : \(err.localizedDescription)")
                }
                println("Twitter HTTP response \(urlResponse.statusCode)")
            })
        }
    }
})

它有效,我可以从iOS设备发布。但我找不到添加targetURL和presentViewController的方法

1 个答案:

答案 0 :(得分:0)

如果您在iPhone设置中为Twitter添加了帐户,请检查您的设备