上传使用backgroundSessionConfiguration和NSURLSessionUploadTask导致应用程序崩溃

我尝试使用NSURLSessionUploadTask新的花哨的iOS 7背景上传,它似乎工作时,我用defaultSessionConfiguration运行,但一旦我尝试backgroundSessionConfiguration它崩溃在我调用uploadTaskWithRequest的行:

下面是代码示例。 奇怪的是,虽然网上有无数的downloadTaskWithRequest例子,但我找不到一个将背景和上传结合在一起的例子。

//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:@"onboarding-4@2x.png"]; 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]; 

这段代码在uploadTaskWithRequest行处崩溃:…就在最后到达简历行之前。

奇怪的是,当我使用backgroundSessionConfiguration以外的任何configurationtypes时,这似乎工作正常。 需要帮助!

提前致谢。

好吧,这只是我在这里愚蠢而不彻底:

1)我会设置一个exception断点,以获取堆栈跟踪,防止我看到实际的exception错误打印 – 哎呀。

2)不能使用具有backgroundSessionConfiguration的完成callback的uploadTaskWithRequest版本(并不奇怪,但仍然没有很好的文档)。

3)将你的PNG数据写入/ var / …,并用file:/// var / …将它提供给uploadTaskWithRequest(这样做很尴尬,因为你经常不需要在一个序列的命令)

很高兴在这里提出一个NSUrlSessionUploadTask示例代码,因为在整个interwebs中似乎有零个。 LMK如果有人想要的话。

根据要求,后台上传的例子。 一定要根据需要实现NSURLSessionDelegate和NSURLSessionTaskDelegate。

 NSMutableArray *unsentPhotos = (NSMutableArray*)[sendingMessage objectForKey:@"unsentPhotos"]; TMessage *message = (TMessage*)[sendingMessage objectForKey:@"message"]; message.sendStatus = MS_PENDING_IMAGE_UPLOAD; for (int i = 0; i < [unsentPhotos count]; i++) { NSString *fileName = [unsentPhotos objectAtIndex:i]; NSLog(@"Initiating file upload for image %@", fileName); NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *srcImagePath = [NSString stringWithFormat:@"%@/messageCache/%@", [paths objectAtIndex:0], fileName]; NSString *dataSrcImagePath = [srcImagePath stringByAppendingString:@".tmp"]; //Encode file to data NSData *imageData = [NSData dataWithContentsOfFile:srcImagePath]; if (!([imageData writeToFile:dataSrcImagePath atomically:YES])) { NSLog(@"Failed to save file."); } //Prepare upload request NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"https://blah.com"]]; [request setHTTPMethod:@"PUT"]; [request setValue:globalAPIToken forHTTPHeaderField:@"access_token"]; [request setValue:[AppDelegate getMyUserID] forHTTPHeaderField:@"userid"]; [request setValue:[NSString stringWithFormat:@"%d", message.teamID] forHTTPHeaderField:@"teamId"]; [request setValue:[NSString stringWithFormat:@"%d", message.emailID] forHTTPHeaderField:@"messageId"]; [request setValue:fileName forHTTPHeaderField:@"fileName"]; [request setValue:@"1" forHTTPHeaderField:@"basefile"]; if (i == 0) { //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; NSURLSessionConfiguration *config = [NSURLSessionConfiguration backgroundSessionConfiguration: [NSString stringWithFormat:@"testSession.foo.%d", randomNumber]]; config.HTTPMaximumConnectionsPerHost = 1; session = [NSURLSession sessionWithConfiguration:config 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"]; } uploadTask = [session uploadTaskWithRequest:request fromFile:[NSURL URLWithString:[NSString stringWithFormat:@"file://%@", dataSrcImagePath]]]; [uploadTask resume]; }