企业应用篇(包含网络、数据库、安全等)
一、网络
1、使用NSURLConnection获得网络数据(NSURL和NSURLRequest基于网络HTTP编程)
(1)最简单的情况下
NSURL可以引用网络或本地资源,是URL的一个包装,使用URL,需要创建一个NSURL对象,然后从NSURL获得数据:
NSURL *url = [NSURL URLWithString:urlString];
NSData *data = [NSData dataWithContentsOfURL:url];
NSURLRequest通常结合UIWebView一起使用,首先创建一个NSURL,然后转换为NSURLRequest,最终在UIWebView中读取网页内容:
NSURL *url = [NSURL fileURLWithPath:path];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[webView loadRequest:request];
(2)需要一种更特定的方式处理HTTP数据时(异步请求/POST数据)
NSURLMutableRequest/NSURLConnection
NSURL* url = [ NSURL URLWithString:@" http:// www. apple. com"];
NSMutableURLRequest* request = [[ NSMutableURLRequest alloc] initWithURL:url cachePolicy: NSURLRequestReloadIgnoringLocalCacheData timeoutInterval: 60];//初始化NSMutableURLRequest,同时指定cache的使用方式和超时时间
receivedData = [[ NSMutableData alloc] init];
[request setHTTPMethod:@" GET"];
[request addValue:@" text/ html" forHTTPHeaderField:@" Content- Type"];
NSURLConnection* conn = [[ NSURLConnection alloc] initWithRequest: request delegate:self];