我使用NSURLConnection通過https進行身份驗證,工作正常,但使用NSData下載文件時,我無法訪問內容。如何通過經過驗證的網址下載文件?如何通過iOS中的認證網址下載文件?
這裏就是我試圖讀取下載的文件的內容(這裏不包括認證碼),服務器說,「用戶未授權」:
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
NSFileManager *fileManager = [NSFileManager defaultManager];
// Attempt to open the file and write the downloaded data to it
if (![fileManager fileExistsAtPath:documentsDirectory2]) { //download path
[fileManager createFileAtPath:documentsDirectory2 contents:nil attributes:nil];
}
// Append data to end of file
NSFileHandle *fileHandle = [NSFileHandle fileHandleForWritingAtPath:documentsDirectory2];
[fileHandle seekToEndOfFile];
[fileHandle writeData:data];
[fileHandle closeFile];
//data.txt exists, read me its content
NSArray *paths8 = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory8 = [paths8 objectAtIndex:0];
NSString *filePath8 = [documentsDirectory8 stringByAppendingPathComponent:@"data.txt"];
NSString *content8 = [NSString stringWithContentsOfFile:filePath8 encoding:NSUTF8StringEncoding error:NULL];
NSLog(@"WHATS INSIDE DATA.TXT? =%@", content8);
[self.responseData appendData:data];
}
可以顯示一些代碼 –
肯定的是,它已經更新。 – sunny
如果你的錯誤是'user not authorized',那麼你可能應該包含你的驗證代碼.... – lnafziger