Background Uploading Task - ios

I have been stucked in this from last week. But I did not get the solution.
I want to access all photos from camera roll and want to save all photos on remote server with the help of NSURLSessionUploadTask. For this I am using following code:
NSString *boundary = #"----WebKitFormBoundarycC4YiaUFwM44F6rT";
NSMutableData *body = [NSMutableData data];
[body appendData:[[NSString stringWithFormat:#"--%#\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"%#\"; filename=\"%#.jpeg\"\r\n", #"upload",self.pictureName] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[#"Content-Type: image/jpeg\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:self.imageData];
[body appendData:[[NSString stringWithFormat:#"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"--%#--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
NSURLSessionConfiguration *config = [NSURLSessionConfiguration defaultSessionConfiguration];
config.HTTPMaximumConnectionsPerHost = 1;
config.HTTPAdditionalHeaders = #{
#"api-key" : #"55e76dc4bbae25b066cb",
#"Accept" : #"application/json",
#"Content-Type" : [NSString stringWithFormat:#"multipart/form-data; boundary=%#", boundary]
};
NSURLSession *upLoadSession = [NSURLSession sessionWithConfiguration:config delegate:self delegateQueue:nil];
NSURL *url = self.uploadURL;
NSLog(#"URL=%#",url);
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];
[request setHTTPMethod:#"PUT"];
[request setHTTPBody:body];
// 3
self.uploadTask = [upLoadSession uploadTaskWithRequest:request fromData:nil];
// self.uploadTask = [upLoadSession uploadTaskWithRequest:request fromFile:tmpFileUrl];
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
// 5
[_uploadTask resume];
All thing is working fine in foreground. But as I enter in background, It stop working. Where is the problem?
What I will have to do for uploading images in background using NSURLSessionUploadTask?

Your session config is not specified to run in the background.
NSURLSessionConfiguration *config = [NSURLSessionConfiguration backgroundSessionConfiguration:#"com.test.backgroundSession"];
NSURLSession *upLoadSession = [NSURLSession sessionWithConfiguration:config delegate:self delegateQueue:nil];
Implement as above.

For upload task to work in background, you have to use uploadTaskWithRequest:fromFile: instead of uploadTaskWithRequest:fromData:
If you use NSData to supply the files, it will work while the app is active and will stop once the app is in background.

Related

iOS not able to upload images on Mobile Data/4G network where in Wifi it is working fine

I am using NSURLSesssion upload task to upload images to server,sending in Bytes.It is working fine in wifi.But when i upload using MobileData/4G LTE network it shows -1001 error code.
1.Increased server side timeout to 4 minutes.
2.Tried with small kb size image
Still issue exists only on 4GLTE networks.
Note:Same service API works in Andriod.
Please let me know what will be the issue.?
NSMutableURLRequest * request = [[NSMutableURLRequest alloc] init];
NSString *boundary = #"---------------------------14737809831466499882746641449";
[request setCachePolicy:cachePolicy];
[self prepareMethod:methodURL methodType:methodType dataInBody:dataInBody contentType:contentType withRequest:request];
[request setValue:[NSString stringWithFormat:#"multipart/form-data; boundary=%#", boundary] forHTTPHeaderField:#"Content-Type"];
[NSURLRequest allowsAnyHTTPSCertificateForHost:#“yyyy.abc.com"];
NSMutableData * bodyData = [[NSMutableData alloc] init];
params = [[[NSUserDefaults standardUserDefaults]objectForKey:#"userparams"]mutableCopy];
for (NSString *param in params)
{
if (![param isEqualToString:#"file"]) {
[bodyData appendData:[[NSString stringWithFormat:#"--%#\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[bodyData appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"%#\"\r\n\r\n", param] dataUsingEncoding:NSUTF8StringEncoding]];
[bodyData appendData:[[NSString stringWithFormat:#"%#\r\n", [params objectForKey:param]] dataUsingEncoding:NSUTF8StringEncoding]];
}
}
// add image data
[bodyData appendData:[[NSString stringWithFormat:#"--%#\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[bodyData appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"%#\"; filename=\"image.jpeg\"\r\n", #"file"] dataUsingEncoding:NSUTF8StringEncoding]];
[bodyData appendData:[#"Content-Type: image/jpeg\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[bodyData appendData:[params objectForKey:#"file"]];
[bodyData appendData:[[NSString stringWithFormat:#"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[bodyData appendData:[[NSString stringWithFormat:#"--%#--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
//NSURLSessionConfiguration *sessionConfiguration = [NSURLSessionConfiguration defaultSessionConfiguration];
// sessionConfiguration.timeoutIntervalForRequest = 120.0;
NSURLSession *session = [NSURLSession sharedSession];
NSURLSessionUploadTask* uploadtask = [session uploadTaskWithRequest:request fromData:bodyData completionHandler:^(NSData *data, NSURLResponse *response, NSError *error){
if (!error)
{
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse*)response;
HttpResponse * response = [[HttpResponse alloc] initWithHttpURLResponse:httpResponse withData:data];
dispatch_async(dispatch_get_main_queue(), ^{
if ([delegate respondsToSelector:#selector(connectionFinishedLoading:)]) {
[delegate connectionFinishedLoading:response];
}
});
}
else
{
dispatch_async(dispatch_get_main_queue(), ^{
if ([delegate respondsToSelector:#selector(connectionFailedWithError:)])
{
[delegate connectionFailedWithError:error];
}
});
}
}];
[uploadtask resume];
}

How to post an image using multipart in NSURLSession ios?

Hi i am trying to upload an image through multipart to server in objective c using NSURLSession. But i am getting few errors like
org.apache.commons.fileupload.FileUploadException: Header section has more than 10240 bytes (maybe it is not properly terminated)
Below is my objective c code:
{
NSString *boundaryConstant = #"----------V2ymHFg03ehbqgZCaKO6jy";
NSString *fileParamConstant = #"photo";
NSMutableURLRequest *req = [[NSMutableURLRequest alloc]initWithURL:[NSURL URLWithString:url]];
[req setHTTPMethod:#"POST"];
NSString *contentType = [NSString stringWithFormat:#"multipart/form-data;boundary=%#",boundaryConstant];
[req setValue:contentType forHTTPHeaderField:#"Content-Type"];
NSMutableData *body = [NSMutableData data];
if (imageData) {
[body appendData:[[NSString stringWithFormat:#"--%#\r\n", boundaryConstant] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"%#\"; filename=\"%#\"\r\n", fileParamConstant, fileName] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:imageData];
[body appendData:[[NSString stringWithFormat:#"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"--%#--\r\n", boundaryConstant] dataUsingEncoding:NSUTF8StringEncoding]];
NSString *bodyLength = [NSString stringWithFormat:#"%zu",[body length]];
[req setValue:bodyLength forHTTPHeaderField:#"Content-Length"];
[req setHTTPBody:body];
NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration] delegate:nil delegateQueue:nil];
NSURLSessionUploadTask *uploadTask = [session uploadTaskWithRequest:req fromData:body completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
NSLog(#"STRING %#", [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]);
}];
[uploadTask resume];
}
}
Any help would be greatly appreciated.
Thanks

How to convert tinypng compress image api to objective c

I want to integrate tinypng api into one of my iOS app. But I can not find any materials on their website on how to write objective-c code or convert from curl command to upload the file for processing. The curl api is below:
curl --user api:YOUR_API_KEY \
--data-binary #unoptimized.png -i https://api.tinify.com/shrink
I have tried on writing http post code for it,
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:TINY_PNG_HOST]
cachePolicy:NSURLRequestReloadIgnoringCacheData
timeoutInterval:8.f];
request.HTTPMethod = #"POST";
NSString *base64encodedKey = [[[NSString stringWithFormat:#"api:%#", apiKey] dataUsingEncoding:NSUTF8StringEncoding] base64EncodedStringWithOptions:NSDataBase64EncodingEndLineWithCarriageReturn];
NSString *auth = [NSString stringWithFormat:#"Basic %#", base64encodedKey];
[request setValue:auth forHTTPHeaderField:#"Authorization"];
NSData *imageData = UIImageJPEGRepresentation(obj.imageIcon, 1);
NSString *boundary = #"---------------------------14737809831466499882746641449";
NSString *contentType = [NSString stringWithFormat:#"multipart/form-data; boundary=%#",boundary];
[request addValue:contentType forHTTPHeaderField: #"Content-Type"];
NSMutableData *body = [NSMutableData data];
[body appendData:[[NSString stringWithFormat:#"rn--%#rn",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[#"Content-Disposition: form-data; name=\"wigi_item_image\";filename=\"item.jpg\"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[#"Content-Type: image/jpeg\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[#"Content-Transfer-Encoding: binary\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:imageData]];
[body appendData:[[NSString stringWithFormat:#"rn--%#--rn",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPBody:body];
NSURLSessionConfiguration *defaultConfigObject = [NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession *defaultSession = [NSURLSession sessionWithConfiguration: defaultConfigObject delegate: self delegateQueue: [NSOperationQueue mainQueue]];
NSURLSessionDataTask * dataTask = [defaultSession dataTaskWithRequest:request];
[dataTask resume];
but always got a failed result. I think something was wrong with uploading image through setHTTPBody method.
how can I solve the problem?

Share Extension not uploading full size images

i am developeing share extension for my iOS application. i had really done every thing but the problem is that my code is only working for small images but when i upload image taken from device camer then uploading fails and only text get uploded.
- (void)performUploadWith:(NSDictionary *)parameters imageFile:(UIImage *)image{
NSString *boundary = #"SportuondoFormBoundary";
// NSURLSessionConfiguration *configuration= [NSURLSessionConfiguration backgroundSessionConfigurationWithIdentifier:configurationName];
// NSURLSession *session=[NSURLSession sessionWithConfiguration:configuration delegate:self delegateQueue:nil];
// NSMutableURLRequest *request;//[NSURLRequest pho]
NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
configuration.HTTPAdditionalHeaders = #{
#"api-key" : #"55e76dc4bbae25b066cb",
#"Accept" : #"application/json",
#"Content-Type" : [NSString stringWithFormat:#"multipart/form-data; boundary=%#", boundary]
};
NSURLSession *session=[NSURLSession sessionWithConfiguration:configuration delegate:self delegateQueue:nil];
NSMutableData *body = [NSMutableData data];
for (NSString *key in parameters)
{
[body appendData:[[NSString stringWithFormat:#"--%#\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"%#\"\r\n\r\n", key] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"%#\r\n", [parameters objectForKey:key]] dataUsingEncoding:NSUTF8StringEncoding]];
}
NSData *imageData = UIImageJPEGRepresentation(image, 0.8);
NSLog(#"imageDATE, %#", imageData);
if (imageData)
{
[body appendData:[[NSString stringWithFormat:#"--%#\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"%#\"; filename=\"image.jpg\"\r\n", #"file"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[#"Content-Type: image/jpg\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:imageData];
[body appendData:[[NSString stringWithFormat:#"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
}
[body appendData:[[NSString stringWithFormat:#"--%#--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
// Data uploading task. We could use NSURLSessionUploadTask instead of NSURLSessionDataTask if we needed to support uploads in the background
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:#"%#%#",kURLBase,kURLAddPostDL]];
NSLog(#"url %#",url);
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:10000];
request.HTTPMethod = #"POST";
request.HTTPBody = body;
NSURLSessionUploadTask *upload=[session uploadTaskWithRequest:request fromData:request.HTTPBody];
[upload resume];
}
i update my code to this and i check on server my request never reaches to server. I am using this code
NSInteger randomNumber = arc4random() % 1000000;
NSURLSessionConfiguration *config = [NSURLSessionConfiguration backgroundSessionConfigurationWithIdentifier:[NSString stringWithFormat:#"testSession.foo.%d", randomNumber]];
config.sharedContainerIdentifier=kGroupNameToShareData;
session = [NSURLSession sessionWithConfiguration:config delegate:self delegateQueue:nil];
NSURLSessionUploadTask *uploadTask = [session uploadTaskWithRequest:request fromFile:[NSURL URLWithString:[NSString stringWithFormat:#"file://%#", dataSrcImagePath]]];
[uploadTask resume];
Extensions aren't allowed their own cache disk space. Need to share with application
Uploading large image need cache disk space,so you upload failed.
You need create url session with following codes:
let configName = "com.shinobicontrols.ShareAlike.BackgroundSessionConfig"
let sessionConfig = NSURLSessionConfiguration.backgroundSessionConfigurationWithIdentifier(configName)
// Extensions aren't allowed their own cache disk space. Need to share with application
sessionConfig.sharedContainerIdentifier = "group.ShareAlike"
let session = NSURLSession(configuration: sessionConfig)
and then setting the app group both extension target and containing app.
and more infomations you can refer following link:
http://www.shinobicontrols.com/blog/posts/2014/07/21/ios8-day-by-day-day-2-sharing-extension
You should be using a background NSURLSession since you are meant to dismiss the sharing UI as soon as possible by calling completeRequestByReturningItems on your NSExtensionContext instance.
The problem is i am not using background session
NSURLSessionConfiguration *configuration= [NSURLSessionConfiguration backgroundSessionConfigurationWithIdentifier:configurationName];
- (void)performUploadWith3:(NSDictionary *)parameters imageFile:(UIImage *)image
{
NSString *fileName = #"image.file";
NSString *boundary= #"Boundary+2EB36F87257DBBD4";
NSURL *storeURL = [[NSFileManager defaultManager] containerURLForSecurityApplicationGroupIdentifier:kGroupNameToShareData];
storeURL=[storeURL URLByAppendingPathComponent:fileName];
NSLog(#"sotre url %#",storeURL);
NSData *imageData = UIImageJPEGRepresentation(image, 0.8);
imageData =[self appendParams:parameters andImage:image bondary:boundary];
if (!([imageData writeToFile:[storeURL path] atomically:YES]))
{
NSLog(#"Failed to save file.");
}
NSString *string=#"URL here";
NSURL *url = [NSURL URLWithString:string];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];
[request setHTTPMethod:#"POST"];
NSString *contentType = [NSString stringWithFormat:#"multipart/form-data; boundary=%#", boundary];
[request setValue:contentType forHTTPHeaderField:#"Content-Type"];
for (NSString *key in parameters)
{
[request setValue:[parameters objectForKey:key] forHTTPHeaderField:key];
}
if (session == nil)
{
//If this is the first upload in this batch, set up a new session
//Each background session needs a unique ID, so get a random number
NSInteger randomNumber = arc4random() % 1000000;
sessionConfiguration = [NSURLSessionConfiguration backgroundSessionConfigurationWithIdentifier:[NSString stringWithFormat:#"and unique name"]];
sessionConfiguration.sharedContainerIdentifier=kGroupNameToShareData;
// config.HTTPMaximumConnectionsPerHost = 1;
session = [NSURLSession sessionWithConfiguration:sessionConfiguration delegate:self delegateQueue:nil];
//Set the session ID in the sending message structure so we can retrieve it from the
//delegate methods later
// [sendingMessage setValue:session.configuration.identifier forKey:#"sessionId"];
NSLog(#"config %#",sessionConfiguration.sharedContainerIdentifier);
}
if([[NSFileManager defaultManager]fileExistsAtPath:[storeURL path]])
{
NSURLSessionUploadTask *uploadTask = [session uploadTaskWithRequest:request fromFile:storeURL];
[uploadTask resume];
}
else
NSLog(#"file does not exsit");
NSLog(#"desc %#",[session sessionDescription]);
// NSLog(#"state %ld", [uploadTask state]);
}

asynchronous upload with NSURLSession will not work but synchronous NSURLConnection does

edit:
I need to upload a file asynchronously from an iPhone to a Python server-side process. I'd like to do the request asynchronously so that I can display a busy animation while it's working.
The request needs to include the username, password and file as 'multipart/form-data'.
I can get it working synchronously using NSURLConnection with the code looking like this::
-(void) uploadDatabase{
Database *databasePath = [[Database alloc] init];
NSString *targetPath = [databasePath getPathToDatabaseInDirectory];
NSData *dbData = [NSData dataWithContentsOfFile:targetPath];
NSString *url = #"http://mydomain.com/api/upload/";
//NSString *username = [[NSUserDefaults standardUserDefaults] stringForKey:USERNAME];
NSString *username = #"user";
NSString *password = #"pass";
NSMutableURLRequest *request = [self createRequestForUrl:url withUsername:username andPassword:password andData:dbData];
NSURLResponse *response;
NSError *error;
NSData *result = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
NSString *stringResult = [[NSString alloc] initWithData:result encoding:NSUTF8StringEncoding];
NSLog(#"**server info %#", stringResult);}
// Request construction
-(NSMutableURLRequest*) createRequestForUrl: (NSString*)urlString withUsername:(NSString*)username andPassword:(NSString*)password andData:(NSData*)dbData
{NSURL *url = [NSURL URLWithString:urlString];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:60.0];
[request setHTTPMethod:#"POST"];
NSString *boundary = #"BOUNDARY_STRING";
NSString *contentType = [NSString stringWithFormat:#"multipart/form-data; boundary=%#", boundary];
[request addValue:contentType forHTTPHeaderField:#"Content-Type"];
NSMutableData *body = [NSMutableData data];
if(dbData != NULL)
{
//only send these methods when transferring data as well as username and password
[body appendData:[[NSString stringWithFormat:#"\r\n--%#\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"file\"; filename=\"dbfile\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[#"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:dbData]];
}
[body appendData:[[NSString stringWithFormat:#"\r\n--%#\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"username\"\r\n\r\n%#", username] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"\r\n--%#\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"password\"\r\n\r\n%#", password] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"\r\n--%#\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPBody:body];
return request;}
However, when I try to do this asynchronously using NSURLSession it doesn't seem to work properly. The code with NSURLSession looks like this:
-(void)uploadDatabase{
Database *databasePath = [[Database alloc] init];
NSString *targetPath = [databasePath getPathToDatabaseInDirectory];
NSURL *phonedbURL = [NSURL URLWithString:targetPath];
NSString *url = #"http://mydomain.com/api/upload/";
NSString *username = #"user";
NSString *password = #"pass";
NSMutableURLRequest *request = [self createRequestForUrl:url withUsername:username andPassword:password andData:NULL];
NSURLSessionConfiguration *defaultConfigObject = [NSURLSessionConfiguration defaultSessionConfiguration];
self.uploadSession = [NSURLSession sessionWithConfiguration:defaultConfigObject delegate:self delegateQueue:Nil];
NSLog(#"the url = %#",url);
NSURLSessionUploadTask *uploadTask = [self.uploadSession uploadTaskWithRequest:request fromFile:phonedbURL];
[uploadTask resume];}
I'm struggling to see what I'm doing differently though as it seems this should work.
Is using NSURLSession the right way to do asynchronous requests? and I'm new to NSURLSession so do I have to change my NSURLMutableRequest for NSURLSession requests rather than NSURLConnection?
Thanks in advance for any help!
You are correct, that if you just want to make your request asynchronous, you should retire sendSynchronousRequest. While we once would have recommended sendAsynchronousRequest, effective iOS 9, NSURLConnection is formally deprecated and one should favor NSURLSession.
Once you start using NSURLSession, you might find yourself drawn to it. For example, one can use a [NSURLSessionConfiguration backgroundSessionConfiguration:], then have uploads progress even after the app has gone into background. (You have to write a few delegate methods, so for simplicity's sake, I've stayed with a simple foreground upload below.) It's just a question of your business requirements, offsetting the new NSURLSession features versus the iOS 7+ limitation it entails.
By the way, any conversation about network requests in iOS/MacOS is probably incomplete without a reference to AFNetworking. It greatly simplifies creation of these multipart requests and definitely merits investigation. They have NSURLSession support, too (but I haven't used their session wrappers, so can't speak to it). But AFNetworking is undoubtedly worthy of your consideration. You can enjoy some of the richness of the delegate-base API (e.g. progress updates, cancelable requests, dependencies between operations, etc.), offering far greater control that available with convenience methods (like sendSynchronousRequest), but without dragging you through the weeds of the delegate methods themselves.
Regardless, if you're really interested in how to do uploads with NSURLSession, see below.
If you want to upload via NSURLSession, it is a slight shift in thinking, namely, separating the configuration of the request (and its headers) in the NSMutableURLRequest from the creation of the the body of the request (which you now specify during the instantiation of the NSURLSessionUploadTask). The body of the request that you now specify as part of the upload task can be either a NSData, a file, or a stream (I use a NSData below, because we're building a multipart request):
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setHTTPMethod:#"POST"];
NSString *boundary = [self boundaryString];
[request addValue:[NSString stringWithFormat:#"multipart/form-data; boundary=%#", boundary] forHTTPHeaderField:#"Content-Type"];
NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration];
NSData *fileData = [NSData dataWithContentsOfFile:path];
NSData *data = [self createBodyWithBoundary:boundary username:#"rob" password:#"password" data:fileData filename:[path lastPathComponent]];
NSURLSessionUploadTask *task = [session uploadTaskWithRequest:request fromData:data completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
NSAssert(!error, #"%s: uploadTaskWithRequest error: %#", __FUNCTION__, error);
// parse and interpret the response `NSData` however is appropriate for your app
}];
[task resume];
And the creation of the NSData being sent is much like your existing code:
- (NSData *) createBodyWithBoundary:(NSString *)boundary username:(NSString*)username password:(NSString*)password data:(NSData*)data filename:(NSString *)filename
{
NSMutableData *body = [NSMutableData data];
if (data) {
//only send these methods when transferring data as well as username and password
[body appendData:[[NSString stringWithFormat:#"--%#\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"file\"; filename=\"%#\"\r\n", filename] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"Content-Type: %#\r\n\r\n", [self mimeTypeForPath:filename]] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:data];
[body appendData:[#"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
}
[body appendData:[[NSString stringWithFormat:#"--%#\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"username\"\r\n\r\n%#\r\n", username] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"--%#\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"password\"\r\n\r\n%#\r\n", password] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"--%#--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
return body;
}
You hardcoded the boundary and the mime type, which is fine, but the above happens to use the following methods:
- (NSString *)boundaryString
{
NSString *uuidStr = [[NSUUID UUID] UUIDString];
// If you need to support iOS versions prior to 6, you can use
// Core Foundation UUID functions to generate boundary string
//
// adapted from http://developer.apple.com/library/ios/#samplecode/SimpleURLConnections
//
// NSString *uuidStr;
//
// CFUUIDRef uuid = CFUUIDCreate(NULL);
// assert(uuid != NULL);
//
// NSString *uuidStr = CFBridgingRelease(CFUUIDCreateString(NULL, uuid));
// assert(uuidStr != NULL);
//
// CFRelease(uuid);
return [NSString stringWithFormat:#"Boundary-%#", uuidStr];
}
- (NSString *)mimeTypeForPath:(NSString *)path
{
// get a mime type for an extension using MobileCoreServices.framework
CFStringRef extension = (__bridge CFStringRef)[path pathExtension];
CFStringRef UTI = UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, extension, NULL);
assert(UTI != NULL);
NSString *mimetype = CFBridgingRelease(UTTypeCopyPreferredTagWithClass(UTI, kUTTagClassMIMEType));
assert(mimetype != NULL);
CFRelease(UTI);
return mimetype;
}

Resources