Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何使用swift上的SLRequest类将图像和文本发布到twitter? func twitterssender(photoImported:UIImage)->Void{ let account=ACAccountStore() 让accountType=account.accountTypeWithAccountTypeIdentifier( ACAccountTypeIdentifierWitter) account.requestAccessToAccountsWithType(accountType,选项:nil, 完成:{(成功:Bool,错误:NSError!)->中的Void 如果成功{ 让我们来计数= account.accountsWithAccountType(accountType) 如果arrayOfAccounts.count>0{ 让twitterAccount=arrayOfAccounts.last as!ACAccount var message=Dictionary() 消息[“状态”]=“我的应用程序测试5” 让imageData=UIImageJPEG表示(photoImported,0.9) 让imageString=imageData.base64EncodedStringWithOptions(NSDataBase64EncodingOptions.allZeros) 消息[“媒体ID”]=imageString 让requestURL=NSURL(字符串: "https://api.twitter.com/1.1/statuses/update.json") 让postRequest=SLRequest(对于服务类型: SLServiceTypeTwitter, requestMethod:SLRequestMethod.POST, URL:requestURL, 参数:消息) addMultipartData(imageData,名称为:“oauth_*”,类型为:“应用程序/八位字节流”,文件名为:“image.jpg”) postRequest.account=twitterAccount postRequest.performRequestWithHandler({ (响应数据:NSData!, urlResponse:NSHTTPURLResponse!, 错误:n错误!)->在中无效 如果让err=error{ println(“错误:\(err.localizedDescription)”) } println(“Twitter HTTP响应\(urresponse.statusCode)”) }) } } }) }_Ios_Swift_Twitter_Slrequest - Fatal编程技术网 在中无效 如果让err=error{ println(“错误:\(err.localizedDescription)”) } println(“Twitter HTTP响应\(urresponse.statusCode)”) }) } } }) },ios,swift,twitter,slrequest,Ios,Swift,Twitter,Slrequest" /> 在中无效 如果让err=error{ println(“错误:\(err.localizedDescription)”) } println(“Twitter HTTP响应\(urresponse.statusCode)”) }) } } }) },ios,swift,twitter,slrequest,Ios,Swift,Twitter,Slrequest" />

Ios 如何使用swift上的SLRequest类将图像和文本发布到twitter? func twitterssender(photoImported:UIImage)->Void{ let account=ACAccountStore() 让accountType=account.accountTypeWithAccountTypeIdentifier( ACAccountTypeIdentifierWitter) account.requestAccessToAccountsWithType(accountType,选项:nil, 完成:{(成功:Bool,错误:NSError!)->中的Void 如果成功{ 让我们来计数= account.accountsWithAccountType(accountType) 如果arrayOfAccounts.count>0{ 让twitterAccount=arrayOfAccounts.last as!ACAccount var message=Dictionary() 消息[“状态”]=“我的应用程序测试5” 让imageData=UIImageJPEG表示(photoImported,0.9) 让imageString=imageData.base64EncodedStringWithOptions(NSDataBase64EncodingOptions.allZeros) 消息[“媒体ID”]=imageString 让requestURL=NSURL(字符串: "https://api.twitter.com/1.1/statuses/update.json") 让postRequest=SLRequest(对于服务类型: SLServiceTypeTwitter, requestMethod:SLRequestMethod.POST, URL:requestURL, 参数:消息) addMultipartData(imageData,名称为:“oauth_*”,类型为:“应用程序/八位字节流”,文件名为:“image.jpg”) postRequest.account=twitterAccount postRequest.performRequestWithHandler({ (响应数据:NSData!, urlResponse:NSHTTPURLResponse!, 错误:n错误!)->在中无效 如果让err=error{ println(“错误:\(err.localizedDescription)”) } println(“Twitter HTTP响应\(urresponse.statusCode)”) }) } } }) }

Ios 如何使用swift上的SLRequest类将图像和文本发布到twitter? func twitterssender(photoImported:UIImage)->Void{ let account=ACAccountStore() 让accountType=account.accountTypeWithAccountTypeIdentifier( ACAccountTypeIdentifierWitter) account.requestAccessToAccountsWithType(accountType,选项:nil, 完成:{(成功:Bool,错误:NSError!)->中的Void 如果成功{ 让我们来计数= account.accountsWithAccountType(accountType) 如果arrayOfAccounts.count>0{ 让twitterAccount=arrayOfAccounts.last as!ACAccount var message=Dictionary() 消息[“状态”]=“我的应用程序测试5” 让imageData=UIImageJPEG表示(photoImported,0.9) 让imageString=imageData.base64EncodedStringWithOptions(NSDataBase64EncodingOptions.allZeros) 消息[“媒体ID”]=imageString 让requestURL=NSURL(字符串: "https://api.twitter.com/1.1/statuses/update.json") 让postRequest=SLRequest(对于服务类型: SLServiceTypeTwitter, requestMethod:SLRequestMethod.POST, URL:requestURL, 参数:消息) addMultipartData(imageData,名称为:“oauth_*”,类型为:“应用程序/八位字节流”,文件名为:“image.jpg”) postRequest.account=twitterAccount postRequest.performRequestWithHandler({ (响应数据:NSData!, urlResponse:NSHTTPURLResponse!, 错误:n错误!)->在中无效 如果让err=error{ println(“错误:\(err.localizedDescription)”) } println(“Twitter HTTP响应\(urresponse.statusCode)”) }) } } }) },ios,swift,twitter,slrequest,Ios,Swift,Twitter,Slrequest,我的代码的问题是,只有文本可以发布而没有图像。我搜索了很多,试图在twitter的网站上找到一些信息。但我还是很困惑怎么做。Twitter曾经有一个名为POST STATUS/update_和_媒体的API来完成我现在想做的工作。不幸的是,twitter放弃了这个API,使用了一个新的API。因此,我确实发现了一些与我类似的问题,但它们要么使用objective-c,要么使用旧的twitter API。没有什么对我有帮助。由于大量的研究耗费了我大量的时间,看起来我需要使用addMultipart

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

您需要使用update.json并使用addMultipartData和NSData提供图像。以下是对我有效的方法:

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)")
                    })
                }
            }
    })

}
func tweetWithImage(数据:NSData)
{
let account=ACAccountStore()
让accountType=account.accountTypeWithAccountTypeIdentifier(
ACAccountTypeIdentifierWitter)
account.requestAccessToAccountsWithType(accountType,选项:nil,
完成:{(成功:Bool,错误:NSError!)->中的Void
如果成功{
让我们来计数=
account.accountsWithAccountType(accountType)
如果arrayOfAccounts.count>0{
让twitterAccount=arrayOfAccounts.first作为!ACAccount
var message=Dictionary()
消息[“状态”]=“使用图像测试推文”
让requestURL=NSURL(字符串:
"https://api.twitter.com/1.1/statuses/update.json")
让postRequest=SLRequest(对于服务类型:
SLServiceTypeTwitter,
requestMethod:SLRequestMethod.POST,
URL:requestURL,
参数:消息)
postRequest.account=twitterAccount
addMultipartData(数据,名称为:“媒体”,类型:nil,文件名:nil)
postRequest.performRequestWithHandler({
(响应数据:NSData!,
urlResponse:NSHTTPURLResponse!,
错误:n错误!)->在中无效
如果让err=error{
println(“错误:\(err.localizedDescription)”)
}
println(“Twitter HTTP响应\(urresponse.statusCode)”)
})
}
}
其他的
{
//你想在这里干什么就干什么
}
})
}
我在我的博客上放了一个教程,其中包含GitHub中相应的项目,说明了如何使用SLRequest,以便支持动画GIF…您可以在这里看到:

注意:此代码和博客帖子是根据maml关于使用简单的“更新”而不是不推荐的“使用媒体更新”的评论更新的。

func twitter(){
let account=ACAccountStore()
让accountType=account.accountTypeWithAccountTypeIdentifier(
ACAccountTypeIdentifierWitter)
account.requestAccessToAccountsWithType(accountType,选项:nil,完成:{(成功:Bool,错误:NSError!)->中的Void
如果成功{
让arrayOfAccounts=account.accountsWithAccountType(accountType)
如果arrayOfAccounts.count>0{
让twitterAccount=arrayOfAccounts.last as!ACAccount
var message=Dictionary()
消息[“状态”]=self.txtPostDesc.text!//textbox
让imageData=UIImagePNGRepresentation(self.imagePost)//pickerview添加
让imageString=imageData!.base64EncodedStringWithOptions(NSDataBase64EncodingOptions())
消息[“媒体ID”]=imageString
让requestURL=NSURL(字符串:https://upload.twitter.com/1/statuses/update_with_media.json")
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

            }
    })
}
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")
                })

            }
        }
    })

}