2014-12-04 77 views
1

此應用程序使用Dropbox Core API將文件移動到Dropbox進行備份存儲。上傳和下載過程運行良好。代表電話正確,並允許我驗證上傳和下載過程,當他們成功。但是,當上傳失敗時,我無法以編程方式訪問錯誤信息。訪問Dropbox核心API錯誤信息iOS 8 Xcode

例如,如果我刪除要上載的文件,則此代碼(委託方法)會生成以下控制檯信息,其中包含我想要的數據 - 我無法以編程方式獲取它。

-(void)restClient:(DBrestClient *)client uploadFileFailedWithError:(NSError *)error { 
NSLog(@"File upload failed with error: %@,error); 
NSLog(@"error data: %@", [error.userInfo objectForKey:@"sourcePath"]); 
} 

2014年12月3日20:33:03.178 BiopLog [43089:4749399] [警告] DropboxSDK: 文件不存在 (/path/Documents/BiopBak123.sqlite-shm)

2014-12-03 20:33:03.297 BiopLog[43089:4749399] File upload failed with 
error: Error Domain=dropbox.com Code=1001 "The operation couldn’t be 
completed. (dropbox.com error 1001.)" UserInfo=0x7fa9e2f814f0 
{sourcePath=/path/Documents/BiopBak123.sqlite-shm, 
destinationPath=/BiopBak123.sqlite-shm 
+0

你「的NSLog(@」的錯誤數據:?......」期待權以何種方式它失敗 – Greg 2014-12-04 22:34:52

+0

它不會失敗我只需要能夠提取的文件名與比較顯然,我可以嘗試解析整個錯誤信息,但這看起來很不錯,我不得不相信有一種方法可以調用sourcePath - 我已經嘗試了許多猜測,但還沒有能夠這樣做,只是爲了完整性,這對於成功方法來說很簡單 – user2698617 2014-12-05 02:39:53

+0

那麼當你的「NSLog(@」錯誤數據:...「行被運行時會發生什麼呢?你引用的日誌似乎只顯示前一行。 – Greg 2014-12-05 02:42:21

回答

0

我決定通過解析錯誤數據來做到這一點。在這種情況下,我的代碼會創建文件名,並且我始終使用文件名中相同的基數和相同數量的字符。這工作,但我仍然想知道更優雅的方法。

- (void)restClient:(DBRestClient *)client uploadFileFailedWithError:(NSError *)error { 

    //parse the error data for the file name 
    NSString *fullErrorData = [NSString stringWithFormat:@"%@",error]; 
    NSString *answer; 
    NSString *message; 

    if ([fullErrorData containsString:@"FileBase"]) { 
     NSRange range = [fullErrorData rangeOfString:@"FileBase"]; 
     NSRange newRange = {range.location,21};//the known length 
     answer = [fullErrorData substringWithRange:newRange]; 

     message = [NSString stringWithFormat: @"The upload for file %@ failed. The remnants will be automatically deleted. You may receive an error message about the deletion - dismiss it.", answer]; 
    } else { 
     message = @"Could not determine the file upload that failed."; 
    } 

    UIAlertView *uploadAlert = [[UIAlertView alloc]initWithTitle:@"Upload Failure" message:message delegate:nil cancelButtonTitle:@"Understood" otherButtonTitles: nil]; 

    [uploadAlert show]; 

    //Add code to clear any remnants 

}//restClient:uploadFileFailedWithError