With iOS 9, I was forced to change my old NSURLConnection with NSURLSession. I had a working code to upload an image from my phone to my server, with a piece of php code to store the image. There are plenty of nice tutorials out there, like this one: http://www.raywenderlich.com/67081/cookbook-using-nsurlsession
// 1
NSURL *url = [NSURL URLWithString:@"http://example.com/insertImage.php"];
NSURLSessionConfiguration *config = [NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession *session = [NSURLSession sessionWithConfiguration:config];
// 2
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];
request.HTTPMethod = @"POST";
// 3
NSDictionary *dictionary = @{@"key1": @"value1"};
NSError *error = nil;
NSData *data = [NSJSONSerialization dataWithJSONObject:dictionary
options:kNilOptions error:&error];
if (!error) {
// 4
NSURLSessionUploadTask *uploadTask = [session uploadTaskWithRequest:request
fromData:data completionHandler:^(NSData *data,NSURLResponse *response,NSError *error) {
// Handle response here
}];
// 5
[uploadTask resume];
}
This seems pretty simple and nice, but I have some problems. How do I embedd an image into this code? How do I change the name of the image? Which delegats do I need? and most importantly, How does the server side look like? I simply just want to store the image in a folder named "uploads".