NSURL 统一资源定位符
就是在整个互联网中只会有这一个资源
// 1. url -》 确定要访问的资源
// 提示:很多大公司都会提供一个 m 开头的主机,为手机服务
// 提示:很多大公司都会提供一个 m 开头的主机,为手机服务
NSURL *url = [NSURL URLWithString:@"http://m.baidu.com"];
// 3. 发送网络连接,获取服务器返回的二进制数据
// 一定记住:所有的网络访问都要用异步的,因为网络访问是耗时的
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
//网络访问结束后,执行的代码
/**
参数:
1. response 服务器的响应
2. data服务器返回的二进制数据,网络上传输的都市二进制的数据
3. connectionError 任何一次网络访问都会可能出现错误,网络编程一定要处理出错的
NSLog(@"%@", data);
[data writeToFile:@"/Users/teacher/Desktop/123" atomically:YES];
// 将 data 转换成 字符串
[data writeToFile:@"/Users/teacher/Desktop/123" atomically:YES];
// 将 data 转换成 字符串
// 在日常开发中,如果没有特殊需求,统一使用 NSUTF8StringEncoding
NSString *html = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
//提问:NSString和字符串(淡村的字符串内容有什么区别)?
NSString是OC 中用来表达字符串的一个类.NSString 的对象能够表达字符串的内容,还有字符串的操作方法
提问:目前的演示而言,NSString $ NSData有什么区别?
NSString& NSData 中保存的内容是完全一样的
NSData 提供的方法针对二进制数据操作的,提供的展现形式是二进制(十六显示可以短点)
NSData 主要是用来在网络传输,或者写入文件的,并不是用来展示的
// 设置 http 请求头字段,告诉服务器附加信息
// 告诉服务器客户端是 iPhone,服务器发现是 iPhone 了,返回漂亮界面了
// 告诉服务器客户端是 iPhone,服务器发现是 iPhone 了,返回漂亮界面了
[request setValue:@"iPhone AppleWebKit" forHTTPHeaderField:@"User-Agent"];
创建网络首页,
一共有三种方法:
第一种
// 访问网络
// 1.创建网路请求
// http://www.baidu.com" 服务器的地址: IP 地址!域名==IP地址,域名是 IP 地址的一个速记符号!
NSURL *url = [NSURL URLWithString:@"http://www.baidu.com"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
// 2. 发送请求
// 1.创建网路请求
// http://www.baidu.com" 服务器的地址: IP 地址!域名==IP地址,域名是 IP 地址的一个速记符号!
NSURL *url = [NSURL URLWithString:@"http://www.baidu.com"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
// 2. 发送请求
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
UIWebView *web = [[UIWebView alloc] initWithFrame:self.view.frame];
// 浏览器直接加载 二进制数据! MIMEType :文件类型 == "Content-Type" = "text/html; textEncodingName :编码方式 baseURL:基准地址
[web loadData:data MIMEType:nil textEncodingName:nil baseURL:url];
[self.view addSubview:web];
}];
}
第二种方法:
// 访问网络
// 1.创建网路请求
// http://www.baidu.com" 服务器的地址: IP 地址!域名==IP地址,域名是 IP 地址的一个速记符号!
NSURL *url = [NSURL URLWithString:@"http://www.baidu.com"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
// 2. 发送请求
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
//
UIWebView *web = [[UIWebView alloc] initWithFrame:self.view.frame];
// 浏览器直接加载 二进制数据! MIMEType :文件类型 == "Content-Type" = "text/html; textEncodingName :编码方式 baseURL:基准地址
[web loadData:data MIMEType:nil textEncodingName:nil baseURL:url];
[self.view addSubview:web];
}];
NSURL *url = [NSURL URLWithString:@"http://www.baidu.com"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
// 2. 发送请求
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
//
UIWebView *web = [[UIWebView alloc] initWithFrame:self.view.frame];
// 浏览器直接加载 二进制数据! MIMEType :文件类型 == "Content-Type" = "text/html; textEncodingName :编码方式 baseURL:基准地址
[web loadData:data MIMEType:nil textEncodingName:nil baseURL:url];
[self.view addSubview:web];
}];
}
给页面设置好看的页面
{
// 访问网络
// 1.创建网路请求!
// 从外界告诉服务器,我是用 iPhone 手机请求的数据!如果需要自己设置网络请求! 可变的网络请求
NSURL *url = [NSURL URLWithString:@"http://www.baidu.com"];
// 可变的网络请求!
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
// 告诉服务器,客户端的软件环境
// [request setValue:@"iPhone" forHTTPHeaderField:@"User-Agent"]; // 直接这样写,会出来一些简单的界面
// User-Agent :通过改变这个量,可以得到自己想要的一些界面!
// 通过设置这个值,可以欺骗服务器,给我们返回不同的数据!
// 现在,一些大公司的页面都做成 响应式(自动适配PC 端 和 移动端) 的!
[request setValue:@"iPhone AppleWebKit" forHTTPHeaderField:@"User-Agent"];
// 2. 发送网络请求
// 2. 发送请求
// 访问网络
// 1.创建网路请求!
// 从外界告诉服务器,我是用 iPhone 手机请求的数据!如果需要自己设置网络请求! 可变的网络请求
NSURL *url = [NSURL URLWithString:@"http://www.baidu.com"];
// 可变的网络请求!
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
// 告诉服务器,客户端的软件环境
// [request setValue:@"iPhone" forHTTPHeaderField:@"User-Agent"]; // 直接这样写,会出来一些简单的界面
// User-Agent :通过改变这个量,可以得到自己想要的一些界面!
// 通过设置这个值,可以欺骗服务器,给我们返回不同的数据!
// 现在,一些大公司的页面都做成 响应式(自动适配PC 端 和 移动端) 的!
[request setValue:@"iPhone AppleWebKit" forHTTPHeaderField:@"User-Agent"];
// 2. 发送网络请求
// 2. 发送请求
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
[data writeToFile:@"/Users/apple/Desktop/baidu.html" atomically:YES];
UIWebView *web = [[UIWebView alloc] initWithFrame:self.view.frame];
// 浏览器直接加载 二进制数据! MIMEType :文件类型 == "Content-Type" = "text/html; textEncodingName :编码方式 baseURL:基准地址
[web loadData:data MIMEType:nil textEncodingName:nil baseURL:url];
[self.view addSubview:web];
}];
UIWebView *web = [[UIWebView alloc] initWithFrame:self.view.frame];
// 浏览器直接加载 二进制数据! MIMEType :文件类型 == "Content-Type" = "text/html; textEncodingName :编码方式 baseURL:基准地址
[web loadData:data MIMEType:nil textEncodingName:nil baseURL:url];
[self.view addSubview:web];
}];
}
// 访问网络
NSString *urlString = @"/Users/cheng/Desktop/课堂笔记 3.html";
// 1.创建网路请求!
// 从外界告诉服务器,我是用 iPhone 手机请求的数据!如果需要自己设置网络请求! 可变的网络请求
// 在 url 中,不能出现汉字,只能是 ASCII 吗! 如果 url 中出现了 汉字等特殊符号,必须使用百分号转译!
urlString = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSString *urlString = @"/Users/cheng/Desktop/课堂笔记 3.html";
// 1.创建网路请求!
// 从外界告诉服务器,我是用 iPhone 手机请求的数据!如果需要自己设置网络请求! 可变的网络请求
// 在 url 中,不能出现汉字,只能是 ASCII 吗! 如果 url 中出现了 汉字等特殊符号,必须使用百分号转译!
urlString = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *url = [NSURL URLWithString:urlString];