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 の Web サイトでいくつかの情報を見つけようとします。しかし、私はまだそれを行う方法を本当に混乱しています。Twitter には、私が今やりたい仕事をするために POST statuses/update_with_media という API がありました。残念ながら、Twitter はその API を破棄し、新しい API を使用しています。そのため、実際に私のものと同様の質問をいくつか見つけましたが、それらはすべてobjective-cを使用しているか、その古いtwitter APIを使用しています。私には何も役に立ちません。多くの調査に多くの時間を費やしているため、addMultipartData を使用して作業を行う必要があるように見えますが、これらのパラメーターを入力する方法がわからないか、これも間違った方向である可能性があります。

4

2 に答える 2

-1

update.json を使用して、イメージに addMultipartData と NSData を提供する必要があります。これが私にとってうまくいくものです:

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

            }
    })
}

アニメーション GIF がサポートされるように SLRequest を使用する方法を説明する GitHub の対応するプロジェクトを含むチュートリアルをブログに掲載しました。迅速/

注: このコードとブログ投稿は、非推奨の「update_with_media」の代わりに単に「update」を使用することに関する maml のコメントに基づいて更新されています。

于 2015-07-19T15:47:21.563 に答える