如何使用以下方法发布此处

时间:2017-05-13 09:49:14

标签: ios url post swift3

我想用swift 3.0发布图片 - 我看过这个视频

https://www.youtube.com/watch?v=krZzC6abaoE

但这适用于Swift 2.0

这是我的代码由于行

而无效
let uploadScriptUrl = URL(String : "http://www.swiftdeveloperblog.com/http-post-example-script/")

我无法运行该应用

  func uploadFiles() {

    let imageData = UIImageJPEGRepresentation(customKindImage.image!, 1)

    if (imageData == nil ) {

        print("There is Not Any Images to upload!")



        let uploadScriptUrl = URL(String : "http://www.swiftdeveloperblog.com/http-post-example-script/")
        var request = NSMutableURLRequest(url: uploadScriptUrl!)

        request.HTTPMethod = "POST"

        request.setValue("Keep-Alive" , forHTTPHeaderField : "Connection")


        var configuration = URLSessionConfiguration.default

        var session = URLSession(configuration : configuration , delegate : self as? URLSessionDelegate , delegateQueue : OperationQueue.main)

        var task = session.uploadTaskWithRequest(request , fromData : imageData)

        task.resume()

    }


}

我在ViewDidLoad中使用此方法进行图像并且该图像将被上传

        if customKind.text == "خدمات تایپ" {

            customKindImage.image = UIImage(named:"Key.png")


        } else if customKind.text == "دیگر خدمات" {

            customKindImage.image = UIImage(named: "doc.png")

        } else if customKind.text == "خدمات ترجمه"  {

            customKindImage.image = UIImage(named:"Translate.png")

        }

1 个答案:

答案 0 :(得分:1)

问题是init(string:)而不是init(String:),此处s也很小,来自swift 3 URLRequest的方法的属性被命名为{{1}不是httpMethod

现在使用Swift 3使用URLRequest而不是HTTPMethod的可变对象,而不是使用uploadTask(with:from:completionHandler:)代理使用完成块。

NSMutableURLRequest