1

In the app, i created the signature as UIImage and i stored the image into the iPhone camera roll, now i need to upload the signature file into server with "sign" as a parameter. I use the following code

@IBAction func saveSignature(sender: AnyObject) {
    // Getting the Signature Image from self.drawSignatureView using the method getSignature().
    let signatureImage = self.drawSignatureView.getSignature() //signatureImage is UIImage

    // API
    let myUrl = NSURL(string: "http://moneymonkey.tokiiyo.com/api/signature");

    let request = NSMutableURLRequest(URL:myUrl!);
    request.HTTPMethod = "POST";

    let param = [
        "api_key"  : "AiK58j67",
        "api_secret"    : "a#9rJkmbOea90-",
        "phone"    : "\(mobile)",
        "sign"  : ""     //Here what string need to pass as parameter
    ]

    let boundary = generateBoundaryString()

    request.setValue("multipart/form-data; boundary=\(boundary)", forHTTPHeaderField: "Content-Type")

    let imageData = UIImagePNGRepresentation(signatureImage)

    if(imageData==nil)  { return; }

    request.HTTPBody = createBodyWithParameters(param, filePathKey: "file", imageDataKey: imageData!, boundary: boundary)   //here what does file means

    let task = NSURLSession.sharedSession().dataTaskWithRequest(request) {
        data, response, error in

        if error != nil {
            print("error=\(error)")
            return
        }

        // You can print out response object
        print("******* response = \(response)")

        // Print out response body
        let responseString = NSString(data: data!, encoding: NSUTF8StringEncoding)
        print("****** response data = \(responseString!)")
        do{

            var json = try NSJSONSerialization.JSONObjectWithData(data!, options: .MutableContainers) as? NSDictionary
            dispatch_async(dispatch_get_main_queue(),{
            });
        }
        catch
        {

            // report error
            print("Oops!! Something went wrong\(error)")
        } 
    }

    task.resume()

    // Saving signatureImage from the line above to the Photo Roll.
    // The first time you do this, the app asks for access to your pictures.
    UIImageWriteToSavedPhotosAlbum(signatureImage, nil,
        nil, nil)
    print("Signature Image:\(signatureImage)")  
}

func createBodyWithParameters(parameters: [String: String]?, filePathKey: String?, imageDataKey: NSData, boundary: String) -> NSData {
    let body = NSMutableData();

    if parameters != nil {
        for (key, value) in parameters! {
            body.appendString("--\(boundary)\r\n")
            body.appendString("Content-Disposition: form-data; name=\"\(key)\"\r\n\r\n")
            body.appendString("\(value)\r\n")
        }
    }

    let filename = "signature.png"

    let mimetype = "image/png"

    body.appendString("--\(boundary)\r\n")
    body.appendString("Content-Disposition: form-data; name=\"\(filePathKey!)\"; filename=\"\(filename)\"\r\n")
    body.appendString("Content-Type: \(mimetype)\r\n\r\n")
    body.appendData(imageDataKey)
    body.appendString("\r\n")



    body.appendString("--\(boundary)--\r\n")

    return body
}

func generateBoundaryString() -> String {
    return "Boundary-\(NSUUID().UUIDString)"
}

Whether i'm at the right path. Please any one suggest me right approach. Thanks in advance

Mahalakshmi.J
  • 189
  • 2
  • 14
  • It is not clear what you are asking. Are you asking how to pass the parameter "sign", or are you asking if this code is the way to upload files in general? If you are asking how to have that parameter end up on the server you are doing it right, it's up to the server to read it. – Maury Markowitz Mar 29 '16 at 10:55

0 Answers0