Как скачать docx, pdf, image, pptx или любой файл с сайта, который запрашивает учетные данные для аутентификации. Я пытался передать учетные данные и данные в nsdata, это нечто иное, но хранить его, который задерживает файл, созданный локально кто-нибудь помочь в коде для загрузки любого типа файла.
код выглядит следующим образом:
по этой кнопке щелкается вызывается из другого файла
DownloadingFile.h
#import "MyAppDelegate.h"
@interface DownloadingFile : NSObject
{
NSMutableData *webData;
NSMutableString *soapResults;
NSURLConnection *conn;
BOOL *elementFound;
BOOL isDoneParsing;
MyAppDelegate *mydelegate;
NSString *userCd,*passWord,*siteUrl;
}
@property (nonatomic ,retain) MyAppDelegate *mydelegate;
-(void)buttonClicked;
-(bool)getIsDone;
@end
DownloadingFile.m
#import "DownloadingFile.h"
#import "iExploreAppDelegate.h"
@implementation DownloadingFile
@synthesize mydelegate;
- (void)buttonClicked
{
mydelegate=(MyAppDelegate *)[[UIApplication sharedApplication] delegate];
userCd=[[NSString alloc] initWithString:[mydelegate getUserId]];
passWord=[[NSString alloc] initWithString:[mydelegate getPassword]];
NSLog(@"In Downloading; ");
NSURL *url =[NSURL URLWithString:[@"http://abcdef.xyz.com/Docs/NewFolder/myFile.docx" stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:url];
[req setValue:[NSString stringWithFormat:@"bytes=%ld-",0] forHTTPHeaderField:@"Range"];
[req addValue: @"docx" forHTTPHeaderField:@"Content-Type"];
[req setHTTPMethod:@"POST"];
//---set the headers---
conn = [[NSURLConnection alloc] initWithRequest:req delegate:self startImmediately:YES];
if (conn) {
NSLog(@"connection done ");
webData = [[NSMutableData data] init];
}
}
-(void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge {
if([challenge previousFailureCount] == 0) {
NSURLCredential *newCredential;
newCredential=[NSURLCredential credentialWithUser:userCd password:passWord persistence:NSURLCredentialPersistenceNone];
NSLog(@"Crediantials done ");
[[challenge sender] useCredential:newCredential forAuthenticationChallenge:challenge];
} else {
[[challenge sender] cancelAuthenticationChallenge:challenge];
NSError* error = [NSError errorWithDomain:@"SoapRequest" code:403 userInfo: [NSDictionary dictionaryWithObjectsAndKeys: @"Could not authenticate this request", NSLocalizedDescriptionKey,nil]];
NSLog(@"Credentials are not valid");
[mydelegate loginFailled:false];
}
}
-(void) connection:(NSURLConnection *) connection
didReceiveResponse:(NSURLResponse *) response {
//[webData setLength: 0];
}
-(void) connection:(NSURLConnection *) connection
didReceiveData:(NSData *) data {
NSLog(@"recevied data %@",data);
webData=[NSMutableData dataWithData:data];
[webData appendData:data];
}
-(void) connection:(NSURLConnection *) connection
didFailWithError:(NSError *) error {
[webData release];
[connection release];
}
-(void) connectionDidFinishLoading:(NSURLConnection *) connection {
NSLog(@"Did Finish Loading done ");
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *pdfPath = [documentsDirectory stringByAppendingPathComponent:@"FileName.docx"];
[webData writeToFile:pdfPath atomically:YES];
[connection release];
}