HTML5   发布时间:2022-04-27  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了iphone – 如何从互联网上下载docx,pdf,图像,pptx或任何文件大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
如何从要求提供身份验证凭据的站点下载docx,pdf,image,pptx或任何文件我尝试传递凭证和nsdata中的数据仍然是其他内容,但它存储了保留本地创建的文件可以任何人帮助下载任何类型文件代码.

代码如下:
在这个buttonClicked从其他文件调用
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 sethttpR_490_11845@ethod:@"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];

}

解决方法

我刚刚编写了一个快速的应用程序来测试使用Apple的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-中打开文件很棒!

大佬总结

以上是大佬教程为你收集整理的iphone – 如何从互联网上下载docx,pdf,图像,pptx或任何文件全部内容,希望文章能够帮你解决iphone – 如何从互联网上下载docx,pdf,图像,pptx或任何文件所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。