admin 管理员组文章数量: 1184232
-( void )touchesBegan:( NSSet < UITouch *> *)touches withEvent:( UIEvent *)event{
[ self demo6 ];
}
-( void )demo6{
// 访问本地服务器
// NSString *urlstr =@" ";
NSString *urlstr = @" 出庄记 (56).pages" ;
// 在 url 中不能出现汉字,否则 url 值为空,如有汉字,需进行转换
urlstr = [urlstr stringByAddingPercentEscapesUsingEncoding : NSUTF8StringEncoding ];
NSURL *url = [ NSURL URLWithString :urlstr];
//cachePolicy: 网络缓存策略:玫举常量
//timeoutInterval: 请求超时时间 : 默认情况:最大 1 分钟,一般 15 秒
NSMutableURLRequest *request = [ NSMutableURLRequest requestWithURL :url cachePolicy : 0 timeoutInterval : 15 ];
// 告诉服务器,客户端的软件环境
// [request setValue:@"iphone" forHTTPHeaderField:@"User-Agent"]; // 简单界面
[request setValue : @"iphone appleWebKit" forHTTPHeaderField : @"User-Agent" ]; // 与 demo3 界面一样
// 浏览器直接加载网络请求 , 适配屏幕
UIWebView *web = [[ UIWebView alloc ] initWithFrame : self . view . bounds ];
[web loadRequest :request];
[ self . view addSubview :web];
}
-( void )demo5{
// 访问优酷
//1. 创建一个可变的网络请求
NSURL *url = [ NSURL URLWithString : @"" ];
NSMutableURLRequest *request = [ NSMutableURLRequest requestWithURL :url];
// 告诉服务器,客户端的软件环境
// [request setValue:@"iphone" forHTTPHeaderField:@"User-Agent"]; // 简单界面
[request setValue : @"iphone appleWebKit" forHTTPHeaderField : @"User-Agent" ]; // 与 demo3 界面一样
// 浏览器直接加载网络请求 , 适配屏幕
UIWebView *web = [[ UIWebView alloc ] initWithFrame : self . view . bounds ];
[web loadRequest :request];
[ self . view addSubview :web];
}
-( void )demo4{
// 访问百度首页
//1. 创建一个可变的网络请求
NSURL *url = [ NSURL URLWithString : @"" ];
NSMutableURLRequest *request = [ NSMutableURLRequest requestWithURL :url];
// 告诉服务器,客户端的软件环境
// [request setValue:@"iphone" forHTTPHeaderField:@"User-Agent"]; // 简单界面
[request setValue : @"iphone appleWebKit" forHTTPHeaderField : @"User-Agent" ]; // 与 demo3 界面一样
//2. 发送网络请求
[ NSURLConnection sendAsynchronousRequest :request queue :[ NSOperationQueue mainQueue ] completionHandler :^( NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {
UIWebView *web = [[ UIWebView alloc ] initWithFrame : self . view . bounds ];
[web loadData :data MIMEType : nil textEncodingName : nil baseURL :url]; // 显示图片
[ self . view addSubview :web];
}];
}
-( void )demo3{
// 访问百度首页
//1. 创建一个网络请求
NSURL *url = [ NSURL URLWithString : @"" ];
NSURLRequest *request = [ NSURLRequest requestWithURL :url];
// 浏览器直接加载网络请求 , 适配屏幕
UIWebView *web = [[ UIWebView alloc ] initWithFrame : self . view . bounds ];
[web loadRequest :request];
[ self . view addSubview :web];
}
-( void )demo2{
// 打印沙盒路径
NSLog ( @"%@" , NSHomeDirectory ());
// 访问百度首页
//1. 创建一个网络请求
NSURL *url = [ NSURL URLWithString : @"" ];
NSURLRequest *request = [ NSURLRequest requestWithURL :url];
//2. 发送网络请求
[ NSURLConnection sendAsynchronousRequest :request queue :[ NSOperationQueue mainQueue ] completionHandler :^( NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {
UIWebView *web = [[ UIWebView alloc ] initWithFrame : self . view . bounds ];
// [web loadData:data MIMEType:nil textEncodingName:nil baseURL:nil]; // 百度图片显示
[web loadData :data MIMEType : nil textEncodingName : nil baseURL :url]; // 显示图片
[ self . view addSubview :web];
}];
}
-( void )demo1{
// 访问百度首页
//1. 创建一个网络请求
NSURL *url = [ NSURL URLWithString : @"" ];
NSURLRequest *request = [ NSURLRequest requestWithURL :url];
//2. 发送网络请求
[ NSURLConnection sendAsynchronousRequest :request queue :[ NSOperationQueue mainQueue ] completionHandler :^( NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {
// 网络响应
NSLog ( @"response %@" ,response);
// 网络返回的数据
NSString *html = [[ NSString alloc ] initWithData :data encoding : NSUTF8StringEncoding ];
NSLog ( @"data %@" ,html);
// 在本地保存百度首页
[data writeToFile : @"/Users/liaojianguo/Desktop/baidu.html" atomically : YES ];
// 网络错误
NSLog ( @"connectionError %@" ,connectionError);
}];
}
版权声明:本文标题:玩转iOS:学会User-Agent中的换行和URL汉字转换,提升网络效率 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.roclinux.cn/p/1772579428a3556954.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论