大佬教程收集整理的这篇文章主要介绍了iphone – 如何从互联网上下载docx,pdf,图像,pptx或任何文件,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
@L_489_5@如下:
在这个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_667_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]; }
- (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,请注明来意。