2014-11-17 41 views
0

我正在製作一個應用程序,用戶登錄到網站並下載一些pdf文件。從重定向URL使用NSURLSessionDownloadTask下載PDF文件

我能夠使用URLSession成功登錄到網站。

現在我的問題是與該文件的下載。

該網站使用的URL與下面的「http://www.someurl.com/getfile.asp?FILE=12345」 這顯然重定向到嵌入在獲取URL實際的pdf文件,

試圖用NSURLSessionDownloadTask的URL,但無法下載PDF文件。

如何從響應中獲取實際URL並下載文件?

這裏是我的嘗試:注意我改變的URL只是爲了問這個問題

//1 create NSURL to the login page 
NSURL *tempURL = [NSURL URLWithString:@"http://www.someurl.com/login.asp"]; 
// 2 create Mutable request with header and username and password 
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:tempURL cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:1.0]; 
[request setHTTPMethod:@"POST"]; 
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"content-type"]; 
//this is hard coded based on your suggested values, obviously you'd probably need to make this more dynamic based on your application's specific data to send 
NSString *postString = @"userid=abcdef&passwd=123456789&submit=Login"; 

NSData *data = [postString dataUsingEncoding:NSUTF8StringEncoding]; 
[request setHTTPBody:data]; 
[request setValue:[NSString stringWithFormat:@"%lu", (unsigned long)[data length]] forHTTPHeaderField:@"Content-Length"]; 
//3 Create NSURLSession to login 
NSURLSession *session = [NSURLSession sharedSession]; 
//4 Create Session Data task to download the html page required 
NSURLSessionDataTask *postDataTask = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) 
             { 

              NSURL *fileURL = [NSURL URLWithString:@"http://www.someurl.com/file.asp?File=20904"]; 

              NSURLSessionDownloadTask *pdfDownload = [[NSURLSession sharedSession] downloadTaskWithURL:fileURL completionHandler:^(NSURL *location, NSURLResponse *response, NSError *error) { 

               NSLog(@"%@", location.description); 

               NSFileManager *fileManager = [NSFileManager defaultManager]; 
               NSError *error1; 

               //getting application's document directory path 
               NSArray * tempArray = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
               NSString *docsDir = [tempArray objectAtIndex:0]; 

               //adding a new folder to the documents directory path 
               NSString *appDir = [docsDir stringByAppendingPathComponent:@"/PDFs/"]; 

               //Checking for directory existence and creating if not already exists 
               if(![fileManager fileExistsAtPath:appDir]) 
               { 
                [fileManager createDirectoryAtPath:appDir withIntermediateDirectories:NO attributes:nil error:&error1]; 
               } 

               //retrieving the filename from the response and appending it again to the path 
               //this path "appDir" will be used as the target path 
               appDir = [appDir stringByAppendingFormat:@"/%@",[[pdfDownload response] suggestedFilename]]; 

               //checking for file existence and deleting if already present. 
               if([fileManager fileExistsAtPath:appDir]) 
               { 
                NSLog([fileManager removeItemAtPath:appDir error:&error1][email protected]"deleted":@"not deleted"); 
               } 

               //moving the file from temp location to app's own directory 
               BOOL fileCopied = [fileManager moveItemAtPath:[location path] toPath:appDir error:&error1]; 
               NSLog(fileCopied ? @"Yes" : @"No"); 
               NSLog(@"%@",appDir); 
              }]; 

              [pdfDownload resume]; 

             }]; 
[postDataTask resume]; 

} 

當我登錄這個我得到空 的NSLog(@ 「%@」,APPDIR);

+0

你有一個路徑組件追加到目錄,而不是一個格式。 'appDir = [appDir stringByAppendingPathComponent:@「myFile.pdf」];' –

+0

P.S.當你使用'stringByAppendingPathComponent'時,你不需要使用'/' –

+1

你應該只使用'response'就像塊的參數一樣不需要訪問'pdfDownload' –

回答

2

您必須將路徑組件添加到目錄,而不是格式。試試這個

appDir = [appDir stringByAppendingPathComponent:[NSString stringWithFormat:@"%@",[response suggestedFilename]]]; 

請記住,你並不需要使用「/」,當您使用stringByAppendingPathComponent: