我偶尔会收到错误
***由于未捕获的异常'NSInvalidArgumentException'终止应用程序,原因:'无法读取文件:///var/mobile/Applications/D743821C-6F34-4E97-8FBA-D7EAD6738E38/Documents/contacts.zip < / p>
***第一次抛出调用堆栈: (0x2ef1ef83 0x39799ccf 0x2eb5f7c3 0x2eb5f2dd 0x39c8181f 0x39c87677 0x2eb5f289 0x2eb9897f 0x172cb9 0x17bac3 0x5b769 0x160d99 0x69f2d 0x698cb 0x1709c7 0x17a9a3 0x39c81833 0x39c88ad7 0x39c88d29 0x39dc3bd3 0x39dc3a98) libc ++ abi.dylib:以NSException类型的未捕获异常终止
该计划落在
行Info.uploadTask = [self.session uploadTaskWithRequest:request fromFile:url];
当我在设备容器中查看时,我看到该文件不存在。
我不明白
为什么程序失败,即使它在try catch块中
NSString *path = file.myPath;
if ([[NSFileManager defaultManager] fileExistsAtPath:path]) {
@try {
NSURL *url = [NSURL fileURLWithPath:path];
Info.uploadTask = [self.session uploadTaskWithRequest:request fromFile:url];
}
@catch (NSException *exception) {
BLog("error Reading file:%@",path);
return;
}
} else {
BLog(@"file not found:%@",path);
return;
}
答案 0 :(得分:1)
所以我遇到了完全相同的问题 - 由于某种原因,NSURLSession无法读取文件。但是,我能够使用您拥有的相同代码捕获异常。为方便起见,我创建了一个类别:
@implementation NSURLSession (DMAdditions)
- (NSURLSessionUploadTask * _Nullable)crashFreeUploadTaskWithRequest:(NSURLRequest *)request fromFile:(NSURL *)fileURL {
@try {
return [self uploadTaskWithRequest:request fromFile:fileURL];
}
@catch (NSException *exception) {
NSLog(@"We crashed: %@", exception);
return nil;
}
}
@end