Iphone 如何从internet下载docx、pdf、image、pptx或任何文件

Iphone 如何从internet下载docx、pdf、image、pptx或任何文件,iphone,objective-c,ipad,Iphone,Objective C,Ipad,如何从要求认证凭据的站点下载docx、pdf、image、pptx或任何文件我尝试在nsdata中传递凭据和静态数据是另外一回事,但是它存储了本地创建的文件,任何人都可以在代码中帮助下载任何类型的文件 代码如下: 在此按钮中,从其他文件调用Clicked 下载文件.h #import "MyAppDelegate.h" @interface DownloadingFile : NSObject { NSMutableData *webData; NSMutableStr

如何从要求认证凭据的站点下载docx、pdf、image、pptx或任何文件我尝试在nsdata中传递凭据和静态数据是另外一回事,但是它存储了本地创建的文件,任何人都可以在代码中帮助下载任何类型的文件

代码如下: 在此按钮中,从其他文件调用Clicked 下载文件.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
下载文件.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];

}

你应该查看一个HttpRequest。


你应该查看一个HttpRequest。


我刚刚编写了一个快速应用程序,使用苹果的
NSURLConnection
进行测试,这是一个简单而健壮的解决方案。我下载了几百KB的Word和PowerPoint文件。对于身份验证,我使用了.htaccess。工作起来很有魅力。这是相关代码

- (IBAction)clickedDownload:(id)sender {
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://myserver.com/downloadTest/testfile.pptx"]];
    NSURLConnection *connection = [NSURLConnection connectionWithRequest:request delegate:self];
    [connection start];
}

-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
   // inform the user
}

-(void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge {
    if ([challenge previousFailureCount] == 0) {        
        NSURLCredential *newCredential;
        newCredential = [NSURLCredential credentialWithUser:@"theUsername"
                                                   password:@"thePassword"
                                                persistence:NSURLCredentialPersistenceNone];
        [[challenge sender] useCredential:newCredential
               forAuthenticationChallenge:challenge];
    } else {
        [[challenge sender] cancelAuthenticationChallenge:challenge];
        // inform the user that password is incorrect
    }
}

-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
    receivedData = [[NSMutableData alloc] init];
}

-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
    [receivedData appendData:data]; 
}

-(void)connectionDidFinishLoading:(NSURLConnection *)connection {
    NSURL *docDirectory = [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
    NSURL *filePath = [docDirectory URLByAppendingPathComponent:@"testfile.pptx"];
    [receivedData writeToURL:filePath atomically:YES];
    [receivedData release];
    connection = nil;
}

现在我在~/Library/Application Support/iPhone Simulator/4.3.2/Applications/[unique Application code]/Documents中打开了这些文件-非常好用

我刚刚编写了一个快速应用程序,使用苹果的
NSURLConnection
进行测试,这是一个简单而健壮的解决方案。我下载了几百KB的Word和PowerPoint文件。对于身份验证,我使用了.htaccess。工作起来很有魅力。这是相关代码

- (IBAction)clickedDownload:(id)sender {
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://myserver.com/downloadTest/testfile.pptx"]];
    NSURLConnection *connection = [NSURLConnection connectionWithRequest:request delegate:self];
    [connection start];
}

-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
   // inform the user
}

-(void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge {
    if ([challenge previousFailureCount] == 0) {        
        NSURLCredential *newCredential;
        newCredential = [NSURLCredential credentialWithUser:@"theUsername"
                                                   password:@"thePassword"
                                                persistence:NSURLCredentialPersistenceNone];
        [[challenge sender] useCredential:newCredential
               forAuthenticationChallenge:challenge];
    } else {
        [[challenge sender] cancelAuthenticationChallenge:challenge];
        // inform the user that password is incorrect
    }
}

-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
    receivedData = [[NSMutableData alloc] init];
}

-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
    [receivedData appendData:data]; 
}

-(void)connectionDidFinishLoading:(NSURLConnection *)connection {
    NSURL *docDirectory = [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
    NSURL *filePath = [docDirectory URLByAppendingPathComponent:@"testfile.pptx"];
    [receivedData writeToURL:filePath atomically:YES];
    [receivedData release];
    connection = nil;
}
现在我在~/Library/Application Support/iPhone Simulator/4.3.2/Applications/[unique Application code]/Documents中打开了这些文件-非常好用