iOS中 UIWebView加载网络数据 技术分享
直奔核心:
#import "TechnologyDetailViewController.h" #define kScreenWidth [UIScreen mainScreen].bounds.size.width #define kScreenHeight [UIScreen mainScreen].bounds.size.height @interface TechnologyDetailViewController () @property (nonatomic,retain)UIWebView *webView;//显示详情页面 @end
@implementation TechnologyDetailViewController
- (void)dealloc
{
self.webView = nil;
self.ids = nil;
[super dealloc];
}
- (void)viewDidLoad {
[super viewDidLoad];
//设置背景
self.view.backgroundColor = [UIColor yellowColor];
//调用请求网络数据
[self readDataFromNetWork];
// self.automaticallyAdjustsScrollViewInsets = NO;
[self.view addSubview:self.webView];
}
懒加载UIWebView
//懒加载
- (UIWebView *)webView{
if (!_webView) {
self.webView = [[[UIWebView alloc]initWithFrame:CGRectMake(0,0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height)]autorelease];
self.webView.backgroundColor = [UIColor orangeColor];
}
return [[_webView retain ]autorelease];
}
核心代码如下:
//解析数据
- (void)readDataFromNetWork{
NSString *str = [NSString stringWithFormat:@"http://c.3g.163.com/nc/article/%@/full.html",self.ids];
NSURL *url = [NSURL URLWithString:str];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
NSString *body = dic[self.ids][@"body"];
NSString * title = dic[self.ids][@"title"];
NSString *digest = dic[self.ids][@"digest"];
NSString *ptime = dic[self.ids][@"ptime"];
NSString * source = dic[self.ids][@"source"];
NSArray * img = dic[self.ids][@"img" ] ;
// NSLog(@"%@",img);
if (img.count) {
for (int i = 0; i < img.count; i ++) {
NSString *imgString = [NSString stringWithFormat:@"<p style=\"text-align:center\"><img src=\"%@\" /></p>",img[i][@"src"]];
NSLog(@"imString = %@",imgString);
imgString = [imgString stringByReplacingOccurrencesOfString:@"<!--IMG#%d-->" withString:imgString];
body = [imgString stringByAppendingString:body];
}
}
// NSLog(@"body = %@",body);
//借助第三方进行排版
NSString *filePath = [[NSBundle mainBundle]pathForResource:@"topic_template" ofType:@"html"];
NSString *htmlString = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];
htmlString = [htmlString stringByReplacingOccurrencesOfString:@"__BODY__" withString:body];
htmlString = [htmlString stringByReplacingOccurrencesOfString:@"__TITLE__" withString:title];
htmlString = [htmlString stringByReplacingOccurrencesOfString:@"__AUTHOR__" withString:source];
htmlString = [htmlString stringByReplacingOccurrencesOfString:@"__DIGEST__" withString:digest];
htmlString = [htmlString stringByReplacingOccurrencesOfString:@"__TIME__" withString:ptime];
htmlString = [htmlString stringByReplacingOccurrencesOfString:@"__PTIME__" withString:ptime];
[self.webView loadHTMLString:htmlString baseURL:nil];
}];
}
======================================================================================================
实在看不懂再看下demol例子:
UIWebView的loadRequest可以用来加载一个url地址,它需要一个NSURLRequest参数。我们定义一个方法用来加载url。在UIWebViewDemoViewController中定义下面方法:
- (void)loadWebPageWithString:(NSString*)urlString
{
NSURL *url =[NSURL URLWithString:urlString];
NSLog(urlString);
NSURLRequest *request =[NSURLRequest requestWithURL:url];
[webView loadRequest:request];
}
在界面上放置3个控件,一个textfield、一个button、一个uiwebview,布局如下:
在代码中定义相关的控件:webView用于展示网页、textField用于地址栏、activityIndicatorView用于加载的动画、buttonPress用于按钮的点击事件。
@interface UIWebViewDemoViewController :UIViewController<UIWebViewDelegate> {
IBOutlet UIWebView *webView;
IBOutlet UITextField *textField;
UIActivityIndicatorView *activityIndicatorView;
}
- (IBAction)buttonPress:(id) sender;
- (void)loadWebPageWithString:(NSString*)urlString;
@end
使用IB关联他们。
设置UIWebView,初始化UIActivityIndicatorView:
- (void)viewDidLoad
{
[super viewDidLoad];
webView.scalesPageToFit =YES;
webView.delegate =self;
activityIndicatorView = [[UIActivityIndicatorView alloc]
initWithFrame : CGRectMake(0.0f, 0.0f, 32.0f, 32.0f)] ;
[activityIndicatorView setCenter: self.view.center] ;
[activityIndicatorView setActivityIndicatorViewStyle: UIActivityIndicatorViewStyleWhite] ;
[self.view addSubview : activityIndicatorView] ;
[self buttonPress:nil];
// Do any additional setup after loading the view from its nib.
}
UIWebView主要有下面几个委托方法:
1、- (void)webViewDidStartLoad:(UIWebView *)webView;开始加载的时候执行该方法。
2、- (void)webViewDidFinishLoad:(UIWebView *)webView;加载完成的时候执行该方法。
3、- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error;加载出错的时候执行该方法。
我们可以将activityIndicatorView放置到前面两个委托方法中。
- (void)webViewDidStartLoad:(UIWebView *)webView
{
[activityIndicatorView startAnimating] ;
}
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
[activityIndicatorView stopAnimating];
}
buttonPress方法很简单,调用我们开始定义好的loadWebPageWithString方法就行了:
- (IBAction)buttonPress:(id) sender
{
[textField resignFirstResponder];
[self loadWebPageWithString:textField.text];
}
当请求页面出现错误的时候,我们给予提示:
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
UIAlertView *alterview = [[UIAlertView alloc] initWithTitle:@"" message:[error localizedDescription] delegate:nil cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
[alterview show];
[alterview release];
}
有疑问可通过新浪微博私信给我:http://weibo.com/hanjunqiang
iOS中 UIWebView加载网络数据 技术分享的更多相关文章
- 关于IOS中UIWebView 加载HTML内容
NSString *strContent=[info objectForKey:@"newContent"]; { NSArray *paths = NSSearchPathFor ...
- iOS开发-UIWebView加载本地和网络数据
UIWebView是内置的浏览器控件,可以用它来浏览网页.打开文档,关于浏览网页榜样可以参考UC,手机必备浏览器,至于文档浏览的手机很多图书阅读软件,UIWebView是一个混合体,具体的功能控件内置 ...
- UITableView加载网络数据的优化
UITableView加载网络数据的优化 效果 源码 https://github.com/YouXianMing/Animations // // TableViewLoadDataControll ...
- iOS中wkwebview加载本地html的要点
项目中有些页面,我采用了html页面开发,然后用wkwebview加载的设计.在加载过程中遇见了一些问题,在这里进行一些记载和讨论.如有不同意见欢迎进行评论沟通. 问题时候这样的: 在webview的 ...
- android122 zhihuibeijing 新闻中心NewsCenterPager加载网络数据实现
新闻中心NewsCenterPager.java package com.itheima.zhbj52.base.impl; import java.util.ArrayList; import an ...
- Flink 中定时加载外部数据
社区中有好几个同学问过这样的场景: flink 任务中,source 进来的数据,需要连接数据库里面的字段,再做后面的处理 这里假设一个 ETL 的场景,输入数据包含两个字段 “type, useri ...
- Android之ListView&Json加载网络数据
使用到的主要内容: 1.Json 解析网络数据 2.异步任务加载图片和数据 3.ListView 的内存空间优化(ConvertView)和运行时间优化(ViewHolder) 4.ListView ...
- IOS中图片加载的一些注意点
图片的加载: [UIImage imageNamed:@"home"] //加载 png图片 在ios中获取一张图片只需要写图片名即可 不需要写后缀 默认都是加载.png的图片 但 ...
- iOS中webView加载URL需要处理特殊字符
今天在项目中遇到webView加载URL时,因为URL中有特殊字符,导致页面无法加载,而且在- (BOOL)webView:(UIWebView )webView shouldStartLoadWit ...
随机推荐
- SQL Server数据库账号密码变更后导致vCenter Server无法访问数据库
SQL Server数据库账号密码变更后导致vCenter Server无法访问数据库 1.1状况描述: 若SQL Server数据库的账号(这里以sa为例)密码发生了变更,那么连接数据的客户端vCe ...
- Mybatis之一级缓存,二级缓存
一级缓存:Mybatis的一级缓存在session上,只要通过session查过的数据,都会放在session上,下一次再查询相同id的数据,都直接冲缓存中取出来,而不用到数据库里去取了. 二级缓存: ...
- IntelliJ IDEA安装配置
1. 从官网安装最新版IntelliJ Idea软件. 2. 激活使用 http://www.3322.cc/soft/37661.html 3. 配置eclipse快捷键 File-->Set ...
- 1-学习GPRS_Air202(Air202开发板介绍)
记得自己第一次实现远程通信是在学校里用SIM900A实现的,随着WIFI模块的普及自己就开始用WIFI模块了,当然WIFI模块已经用的很... WIFI模块要想实现远程控制必须连接路由器,其实在做王哥 ...
- 转载c++常忘的知识点
C++的一些知识点比较零碎,下面清单的形式做一些记录与归纳,以供参考. 1.赋值操作符重载(深复制): (1)由于目标对象可能引用了以前的一些数据,所以应该先delete这些数据: (2)注意到对象可 ...
- Node.js C/C++ 插件
插件 Addons 是动态链接的共享对象.他提供了 C/C++ 类库能力.这些API比较复杂,他包以下几个类库: V8 JavaScript, C++ 类库.用来和 JavaScript 交互,比如创 ...
- Python能做些什么?
前言 网上搜集到的一些python能做什么的资料,利用python能做很多事情,我们可以在多门课程中都使用Python作为我们的教学语言.比如,计算机网络.数据结构.人工智能.图像处理.软件分析与测试 ...
- cassandra 3.x官方文档(5)---探测器
写在前面 cassandra3.x官方文档的非官方翻译.翻译内容水平全依赖本人英文水平和对cassandra的理解.所以强烈建议阅读英文版cassandra 3.x 官方文档.此文档一半是翻译,一半是 ...
- 20160223.CCPP体系详解(0033天)
程序片段(01):MyArray.h+MyArray.c+main.c 内容概要:数组库 ///MyArray.h #pragma once #define DT int//类型通用 typedef ...
- 软件测试之BUG分析定位概述(QA如何分析定位BUG)
你是否遇到这样的场景? QA发现问题后找到DEV说: 不好了,你的程序出问题了! DEV(追查半小时之后): 唉,是你们测试环境配置的问题 唉,是你们数据不一致 唉,是你们**程序版本不对 唉,是** ...