From everything I read you can activate it with with a button so I put it right after the button action that starts the http POST and upload, but right now it starts spinning right after the function has finished, after the upload is complete, the opposite of what I would expect.
I'm new to Swift so please explain your answer as you would to a nubie. Many thanks.
@IBAction func upload(sender: AnyObject) {
myActivityIndicator.startAnimating()
var imageData = UIImagePNGRepresentation(myImageView.image)
if imageData != nil{
var request = NSMutableURLRequest(URL: NSURL(string:"http://www.example.com/upload.php")!)
var session = NSURLSession.sharedSession()
request.HTTPMethod = "POST"
var boundary = NSString(format: "---------------------------14737809831466499882746641449")
var contentType = NSString(format: "multipart/form-data; boundary=%@",boundary)
// println("Content Type \(contentType)")
request.addValue(contentType, forHTTPHeaderField: "Content-Type")
var body = NSMutableData.alloc()
// Title
body.appendData(NSString(format: "\r\n--%@\r\n",boundary).dataUsingEncoding(NSUTF8StringEncoding)!)
body.appendData(NSString(format:"Content-Disposition: form-data; name=\"title\"\r\n\r\n").dataUsingEncoding(NSUTF8StringEncoding)!)
body.appendData("Hello World".dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: true)!)
// Image
body.appendData(NSString(format: "\r\n--%@\r\n", boundary).dataUsingEncoding(NSUTF8StringEncoding)!)
body.appendData(NSString(format:"Content-Disposition: form-data; name=\"profile_img\"; filename=\"img.jpg\"\r\n").dataUsingEncoding(NSUTF8StringEncoding)!)
// println("body=\(body)")
body.appendData(NSString(format: "Content-Type: application/octet-stream\r\n\r\n").dataUsingEncoding(NSUTF8StringEncoding)!)
body.appendData(imageData)
body.appendData(NSString(format: "\r\n--%@\r\n", boundary).dataUsingEncoding(NSUTF8StringEncoding)!)
request.HTTPBody = body
var returnData = NSURLConnection.sendSynchronousRequest(request, returningResponse: nil, error: nil)
var returnString = NSString(data: returnData!, encoding: NSUTF8StringEncoding)
println("returnString \(returnString)")
//self.myActivityIndicator.stopAnimating()
//self.myImageView.image = nil
}
}
try this
http://ijoshsmith.com/2014/07/29/a-swift-app-that-calls-json-web-services/ http://www.raywenderlich.com/79149/grand-central-dispatch-tutorial-swift-part-1