fork download
  1.  
  2. func myImageUploadRequest()
  3. {
  4.  
  5. let myUrl = URL(string: "https://m...content-available-to-author-only...i.com/api/v2/upload-single-photo-restaurant");
  6. let request = NSMutableURLRequest(url:myUrl!);
  7. request.httpMethod = "POST";
  8. let device_id = UIDevice.current.identifierForVendor!.uuidString
  9. print("lat is:",lat!)
  10. print("lng is:",lng!)
  11. var param:[String:Any] = [
  12. "lat" : lat!,
  13. "lng" : lng!,
  14. "device_id" : device_id,
  15. "name" : photoName.text!,
  16. "datetime" : Const.getDate()
  17. ] as [String : Any]
  18.  
  19.  
  20. let boundary = "----WebKitFormBoundary7MA4YWxkTrZu0gW"
  21. request.setValue("application/json", forHTTPHeaderField: "Accept")
  22. request.setValue("multipart/form-data; boundary=\(boundary)", forHTTPHeaderField: "Content-Type")
  23. let imageData = UIImageJPEGRepresentation(imgView.image!, 1)
  24. if(imageData==nil) { return; }
  25. request.httpBody = createBodyWithParameters(parameters: param as? [String : String], filePathKey: "photo", imageDataKey: imageData! as NSData, boundary: boundary) as Data
  26.  
  27. let task = URLSession.shared.dataTask(with: request as URLRequest) {
  28. data, response, error in
  29.  
  30. if error != nil {
  31. print("error=\(String(describing: error))")
  32. return
  33. }
  34.  
  35. // You can print out response object
  36. print("******* response = \(String(describing: response))")
  37.  
  38. // Print out reponse body
  39. let responseString = NSString(data: data!, encoding: String.Encoding.utf8.rawValue)
  40. print("****** response data = \(responseString!)")
  41.  
  42. do {
  43. let json = try JSONSerialization.jsonObject(with: data!, options: []) as? NSDictionary
  44.  
  45. print(json!)
  46.  
  47. DispatchQueue.main.async(execute: {
  48. self.imgView.image = UIImage(named: "img")
  49. let msg = json?.object(forKey: "message")
  50. UIViewController.removeSpinner(spinner: self.sv!)
  51. self.stackViewUpload.isHidden=true
  52. self.alertWithMsg(msg: msg as! String)
  53. })
  54. }catch{
  55. print(error)
  56. }
  57. }
  58. task.resume()
  59. }
  60. func createBodyWithParameters(parameters: [String: String]?, filePathKey: String?, imageDataKey: NSData, boundary: String) -> NSData {
  61. let body = NSMutableData();
  62.  
  63. if parameters != nil {
  64. for (key, value) in parameters! {
  65. body.appendString(string: "--\(boundary)\r\n")
  66. body.appendString(string: "Content-Disposition: form-data; name=\"\(key)\"\r\n\r\n")
  67. body.appendString(string: "\(value)\r\n")
  68. }
  69. }
  70.  
  71. let filename = "user-profile.jpg"
  72. let mimetype = "image/jpg"
  73.  
  74. body.appendString(string: "--\(boundary)\r\n")
  75. body.appendString(string: "Content-Disposition: form-data; name=\"\(filePathKey!)\"; filename=\"\(filename)\"\r\n")
  76. body.appendString(string: "Content-Type: \(mimetype)\r\n\r\n")
  77. body.append(imageDataKey as Data)
  78. body.appendString(string: "\r\n")
  79.  
  80. body.appendString(string: "--\(boundary)--\r\n")
  81.  
  82. return body
  83.  
  84. }
  85. extension NSMutableData {
  86.  
  87. func appendString(string: String) {
  88. let data = string.data(using: String.Encoding.utf8, allowLossyConversion: true)
  89. append(data!)
  90. }
  91. }
  92.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.swift:85:15: error: use of undeclared type 'NSMutableData'
    extension NSMutableData {
              ^~~~~~~~~~~~~
prog.swift:60:135: error: use of undeclared type 'NSData'
        func createBodyWithParameters(parameters: [String: String]?, filePathKey: String?, imageDataKey: NSData, boundary: String) -> NSData {
                                                                                                                                      ^~~~~~
prog.swift:60:106: error: use of undeclared type 'NSData'
        func createBodyWithParameters(parameters: [String: String]?, filePathKey: String?, imageDataKey: NSData, boundary: String) -> NSData {
                                                                                                         ^~~~~~
prog.swift:5:25: error: use of unresolved identifier 'URL'
            let myUrl = URL(string: "https://mangerici.com/api/v2/upload-single-photo-restaurant");
                        ^~~
prog.swift:6:27: error: use of unresolved identifier 'NSMutableURLRequest'
            let request = NSMutableURLRequest(url:myUrl!);
                          ^~~~~~~~~~~~~~~~~~~
prog.swift:8:29: error: use of unresolved identifier 'UIDevice'
            let device_id = UIDevice.current.identifierForVendor!.uuidString
                            ^~~~~~~~
prog.swift:9:29: error: use of unresolved identifier 'lat'
            print("lat is:",lat!)
                            ^~~
Swift.Float:2:15: note: did you mean 'Float'?
public struct Float {
              ^
prog.swift:10:29: error: use of unresolved identifier 'lng'
            print("lng is:",lng!)
                            ^~~
Swift._log:1:13: note: did you mean '_log'?
public func _log(_ x: Float) -> Float
            ^
Swift._log:1:13: note: did you mean '_log'?
public func _log(_ x: Double) -> Double
            ^
prog.swift:12:29: error: use of unresolved identifier 'lat'
                    "lat" : lat!,
                            ^~~
Swift.Float:2:15: note: did you mean 'Float'?
public struct Float {
              ^
prog.swift:13:29: error: use of unresolved identifier 'lng'
                    "lng" : lng!,
                            ^~~
Swift._log:1:13: note: did you mean '_log'?
public func _log(_ x: Float) -> Float
            ^
Swift._log:1:13: note: did you mean '_log'?
public func _log(_ x: Double) -> Double
            ^
prog.swift:15:30: error: use of unresolved identifier 'photoName'
                    "name" : photoName.text!,
                             ^~~~~~~~~
prog.swift:16:34: error: use of unresolved identifier 'Const'
                    "datetime" : Const.getDate()
                                 ^~~~~
Swift.CInt:2:18: note: did you mean 'CInt'?
public typealias CInt = Int32
                 ^
prog.swift:23:29: error: use of unresolved identifier 'UIImageJPEGRepresentation'
            let imageData = UIImageJPEGRepresentation(imgView.image!, 1)
                            ^~~~~~~~~~~~~~~~~~~~~~~~~
prog.swift:23:55: error: use of unresolved identifier 'imgView'
            let imageData = UIImageJPEGRepresentation(imgView.image!, 1)
                                                      ^~~~~~~
prog.swift:27:24: error: use of unresolved identifier 'URLSession'
            let task = URLSession.shared.dataTask(with: request as URLRequest) {
                       ^~~~~~~~~~
prog.swift:61:24: error: use of unresolved identifier 'NSMutableData'
            let body = NSMutableData();
                       ^~~~~~~~~~~~~
prog.swift:88:28: error: value of type 'String' has no member 'data'
                let data = string.data(using: String.Encoding.utf8, allowLossyConversion: true)
                           ^~~~~~ ~~~~
prog.swift:89:17: error: use of unresolved identifier 'append'
                append(data!)
                ^~~~~~
<unknown>:0: error: error opening input file 'prog.o' (No such file or directory
)
clang: error: no such file or directory: 'prog.o'
clang: error: no such file or directory: '@prog.autolink'
stdout
Standard output is empty