iOS网络编程(九)

1.多线程下载文件思路:

开几个子线程来进行下载,通过设定请求头来下载各自的部分,通过NSFilehandle来把数据写进文件,这个类里有在任意位置写数据的方法。

2.文件的压缩和解压缩:第三方解压缩框架——ZipArchive

需要引入libz.dylib框架

导入头文件Main.h

创建压缩文件

+ (BOOL)createZipFileAtPath:(NSString *)path

withFilesAtPaths:(NSArray *)paths;

+ (BOOL)createZipFileAtPath:(NSString *)path

withContentsOfDirectory:(NSString *)directoryPath;

解压

+ (BOOL)unzipFileAtPath:(NSString *)path

toDestination:(NSString *)destination


3.NSURLConnection和Runloop

#import"ViewController.h"

@interfaceViewController()

@end

@implementationViewController

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent*)event

{

[selfnewThreadDelegate2];

}

-(void)delegate1

{

NSURLRequest*request = [NSURLRequestrequestWithURL:[NSURLURLWithString:@"http://120.25.226.186:32812/login?username=123&pwd=123&type=JSON"]];

//设置代理

//代理方法:默认是在主线程中调用的

NSURLConnection*connect = [NSURLConnectionconnectionWithRequest:requestdelegate:self];

//设置代理方法在哪个线程中调用

//[NSOperationQueue alloc]init]]开子线程

//[NSOperationQueue mainQueue]不能这样设置

[connectsetDelegateQueue:[[NSOperationQueuealloc]init]];

//[connect setDelegateQueue:[NSOperationQueue mainQueue]];

NSLog(@"-------");

}

-(void)delegate2

{

NSURLRequest*request = [NSURLRequestrequestWithURL:[NSURLURLWithString:@"http://120.25.226.186:32812/login?username=123&pwd=123&type=JSON"]];

//设置代理

//代理方法:默认是在主线程中调用的

NSURLConnection*connect = [[NSURLConnectionalloc]initWithRequest:requestdelegate:selfstartImmediately:NO];

[connectsetDelegateQueue:[[NSOperationQueuealloc]init]];

//开始发送请求

[connectstart];

NSLog(@"-------");

}

-(void)newThreadDelegate1

{

dispatch_async(dispatch_get_global_queue(0,0), ^{

NSURLRequest*request = [NSURLRequestrequestWithURL:[NSURLURLWithString:@"http://120.25.226.186:32812/login?username=123&pwd=123&type=JSON"]];

//设置代理

//代理方法:默认是在主线程中调用的

//该方法内部其实会将connect对象作为一个source添加到当前的runloop中,指定运行模式为默认

NSURLConnection*connect = [NSURLConnectionconnectionWithRequest:requestdelegate:self];

//设置代理方法在哪个线程中调用

[connectsetDelegateQueue:[[NSOperationQueuealloc]init]];

//[[NSRunLoop currentRunLoop] runMode:UITrackingRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:1000]];

[[NSRunLoopcurrentRunLoop]run];

NSLog(@"---%@----",[NSThreadcurrentThread]);

});

}

-(void)newThreadDelegate2

{

dispatch_async(dispatch_get_global_queue(0,0), ^{

NSURLRequest*request = [NSURLRequestrequestWithURL:[NSURLURLWithString:@"http://120.25.226.186:32812/login?username=123&pwd=123&type=JSON"]];

//设置代理

//代理方法:默认是在主线程中调用的

NSURLConnection*connect = [[NSURLConnectionalloc]initWithRequest:requestdelegate:selfstartImmediately:NO];

[connectsetDelegateQueue:[[NSOperationQueuealloc]init]];

//开始发送请求

//如如果connect对象没有添加到runloop中,那么该方法内部会自动的添加到runloop

//注意:如果当前的runloop没有开启,那么该方法内部会自动获得当前线程对应的runloop对象并且开启

[connectstart];

NSLog(@"---%@----",[NSThreadcurrentThread]);

});

}

#pragma mark ----------------------

#pragma mark NSURLConnectionDataDelegate

-(void)connection:(NSURLConnection*)connection didReceiveResponse:(NSURLResponse*)response

{

NSLog(@"didReceiveResponse---%@",[NSThreadcurrentThread]);

}

@end

你可能感兴趣的:(iOS网络编程(九))