2
func twitterSender(photoImported: UIImage) ->Void {
    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
                    var message = Dictionary<String, AnyObject>()
                    message["status"] = "My app test 5"
                    let imageData = UIImageJPEGRepresentation(photoImported, 0.9)
                    let imageString = imageData.base64EncodedStringWithOptions(NSDataBase64EncodingOptions.allZeros)
                    message["media_ids"] = imageString
                    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.addMultipartData(imageData, withName: "oauth_*", type: "application/octet-stream", filename: "image.jpg")
                    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)")
                    })
                }
            }
    })

}

我的代码的问题是只能发布没有图像的文本。我搜索了很多,并尝试在 twitter 的网站上找到一些信息。但我仍然很困惑如何去做。Twitter 曾经有一个名为 POST statuses/update_with_media 的 API 来完成我现在想做的工作。不幸的是,twitter 放弃了那个 API 并使用了一个新的。所以我确实发现了一些与我类似的问题,但所有这些问题要么使用objective-c,要么使用旧的twitter API。没有什么对我有帮助。由于大量研究花费了我很多时间,看起来我需要使用 addMultipartData 来完成这项工作,但我不知道如何填充这些参数,或者这可能是一个错误的方向。

4

2 回答 2

1
func twitter(){
    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
        var message = Dictionary<String, AnyObject>()
        message["status"] = self.txtPostDesc.text! //textbox
        let imageData = UIImagePNGRepresentation(self.imagePost)//pickerview add
        let imageString = imageData!.base64EncodedStringWithOptions(NSDataBase64EncodingOptions())
        message["media_ids"] = imageString
        let requestURL = NSURL(string: "https://upload.twitter.com/1/statuses/update_with_media.json")
        let postRequest = SLRequest(forServiceType: SLServiceTypeTwitter, requestMethod: SLRequestMethod.POST, URL: requestURL, parameters: message)

        postRequest.addMultipartData(imageData, withName: "media", type: nil, filename: nil)
            postRequest.account = twitterAccount

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

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

            }
        }
    })

}
于 2016-04-27T09:23:45.630 回答
-1

您需要使用 update.json 并使用带有 NSData 的 addMultipartData 提供图像。这对我有用:

func tweetWithImage(data:NSData)
{

    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.first as! ACAccount
                    var message = Dictionary<String, AnyObject>()
                    message["status"] = "Test Tweet with image"

                    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.addMultipartData(data, withName: "media", type: nil, filename: nil)

                    postRequest.performRequestWithHandler({
                        (responseData: NSData!,
                        urlResponse: NSHTTPURLResponse!,
                        error: NSError!) -> Void in
                        if let err = error {
                            println("Error : \(err.localizedDescription)")
                        }
                        println("Twitter HTTP response \(urlResponse.statusCode)")

                    })
                }
            }
            else
            {
                // do what you want here

            }
    })
}

我在我的博客上放了一个教程,并在 GitHub 中展示了使用 SLRequest 以便支持动画 GIF 的相应项目......你可以在这里看到它:http: //www.iosinsight.com/twitter-integration-with-迅速/

注意:此代码和博客文章是根据 maml 关于使用简单的“更新”而不是弃用的“update_with_media”的评论更新的。

于 2015-07-19T15:47:21.563 回答