Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
168 views
in Technique[技术] by (71.8m points)

ios - Uploads using backgroundSessionConfiguration and NSURLSessionUploadTask cause app to crash

I'm trying out the new fancy iOS 7 background uploading using NSURLSessionUploadTask and it seems to work when I run with defaultSessionConfiguration, but once I try backgroundSessionConfiguration it crashes at the line where I call uploadTaskWithRequest:

Here is the code sample below. Oddly, while there are myriad downloadTaskWithRequest examples online, I cannot find a single one that combines background and uploading together.

//Create a session w/ background settings
NSURLSessionConfiguration *config = [NSURLSessionConfiguration backgroundSessionConfiguration:@"identifierString.foo"];
NSURLSession *upLoadSession = [NSURLSession sessionWithConfiguration:config delegate:self delegateQueue:nil];

//Create a file to upload
UIImage *image = [UIImage imageNamed:@"[email protected]"];
NSData *imageData = UIImagePNGRepresentation(image);
NSFileManager *fileManager = [NSFileManager defaultManager];
NSArray *URLs = [fileManager URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask];
NSString *documentsDirectory = [[URLs objectAtIndex:0] absoluteString];
NSString *filePath = [documentsDirectory stringByAppendingString:@"testfile.png"];
[imageData writeToFile:filePath atomically:YES];

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"https://file.upload/destination"]];
[request setHTTPMethod:@"PUT"];
NSURLSessionUploadTask *uploadTask = [upLoadSession uploadTaskWithRequest:request fromFile:[NSURL URLWithString:filePath] completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
    //code
}];

[uploadTask resume];

This code is crashing at the line with uploadTaskWithRequest: ... just before it gets to the resume line at the end.

Oddly, this seems to work OK when I use any config type other than backgroundSessionConfiguration. Help needed!

Thanks in advance.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Okay, so this was kind of just me being foolish and not thorough here:

1) I'd set an exception breakpoint to get stack traces that was preventing me from see the actual exception error printout -- oops.

2) Can't use version of uploadTaskWithRequest that has a completion callback for a backgroundSessionConfiguration (not surprising but still not well documented).

3) Write your PNG data to /var/... and provide it to uploadTaskWithRequest with file:///var/... (this is just awkward because you don't often need to convert between the two for a single sequence of commands)

Happy to put up a NSUrlSessionUploadTask sample code here, since there seems to be zero of them on the entire interwebs. LMK if anyone wants that.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...