直奔核心:

#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];
}

总结:本文通过实现一个简单的浏览器,说明了uiwebview的方法和属性,相信通过这个例子,应该明白uiwebview的使用了。

有疑问可通过新浪微博私信给我:http://weibo.com/hanjunqiang

iOS中 UIWebView加载网络数据 技术分享的更多相关文章

  1. 关于IOS中UIWebView 加载HTML内容

    NSString *strContent=[info objectForKey:@"newContent"]; { NSArray *paths = NSSearchPathFor ...

  2. iOS开发-UIWebView加载本地和网络数据

    UIWebView是内置的浏览器控件,可以用它来浏览网页.打开文档,关于浏览网页榜样可以参考UC,手机必备浏览器,至于文档浏览的手机很多图书阅读软件,UIWebView是一个混合体,具体的功能控件内置 ...

  3. UITableView加载网络数据的优化

    UITableView加载网络数据的优化 效果 源码 https://github.com/YouXianMing/Animations // // TableViewLoadDataControll ...

  4. iOS中wkwebview加载本地html的要点

    项目中有些页面,我采用了html页面开发,然后用wkwebview加载的设计.在加载过程中遇见了一些问题,在这里进行一些记载和讨论.如有不同意见欢迎进行评论沟通. 问题时候这样的: 在webview的 ...

  5. android122 zhihuibeijing 新闻中心NewsCenterPager加载网络数据实现

    新闻中心NewsCenterPager.java package com.itheima.zhbj52.base.impl; import java.util.ArrayList; import an ...

  6. Flink 中定时加载外部数据

    社区中有好几个同学问过这样的场景: flink 任务中,source 进来的数据,需要连接数据库里面的字段,再做后面的处理 这里假设一个 ETL 的场景,输入数据包含两个字段 “type, useri ...

  7. Android之ListView&Json加载网络数据

    使用到的主要内容: 1.Json 解析网络数据 2.异步任务加载图片和数据 3.ListView 的内存空间优化(ConvertView)和运行时间优化(ViewHolder) 4.ListView ...

  8. IOS中图片加载的一些注意点

    图片的加载: [UIImage imageNamed:@"home"] //加载 png图片 在ios中获取一张图片只需要写图片名即可 不需要写后缀 默认都是加载.png的图片 但 ...

  9. iOS中webView加载URL需要处理特殊字符

    今天在项目中遇到webView加载URL时,因为URL中有特殊字符,导致页面无法加载,而且在- (BOOL)webView:(UIWebView )webView shouldStartLoadWit ...

随机推荐

  1. SCNN车道线检测--(SCNN)Spatial As Deep: Spatial CNN for Traffic Scene Understanding(论文解读)

    Spatial As Deep: Spatial CNN for Traffic Scene Understanding 收录:AAAI2018 (AAAI Conference on Artific ...

  2. 荣耀10带来AI版WPS,玩转潮酷新功能

    图书馆里,想把喜欢的句子和情节留存, 无奈摘抄需要时间,拍下来又很容易遗忘在相册? 课堂偷偷拍摄的课件, 模糊一片难以辨认? 开会培训收集的PPT照片, 总有那么几页对焦失败? 这些当时起劲,后来就& ...

  3. django之模板显示静态文件

    由于django的模板渲染机制,图片不能直接引用,否则不会显示. <img src="/static/img/logo.jpg"> 可以看出图片的大小轮廓,但并不显示内 ...

  4. 《深入理解mybatis原理》 MyBatis的架构设计以及实例分析

    作者博客:http://blog.csdn.net/u010349169/article/category/2309433 MyBatis是目前非常流行的ORM框架,它的功能很强大,然而其实现却比较简 ...

  5. 地址四级联动的vue组件

    一.效果图如下: 二.思路 主要在vue中结合 mint-ui组件的Picker和Popup方法,负责对json地址进行展示: 三.代码地址 四.说明 address4.json最好是在点击父组件的地 ...

  6. JAVA局部内部类

    在刚刚学到的android开发中了解到Button的onClick是通过局部内部类的方式实现的,具体的原理我以后再去了解,只是遇到问题总是想知道为什么,不要告诉我这是规则,死记住就可以了. 问题是局部 ...

  7. 剑指架构师系列-MySQL调优

    介绍MySQL的调优手段,主要包括慢日志查询分析与Explain查询分析SQL执行计划 1.MySQL优化 1.慢日志查询分析 首先需要对慢日志进行一些设置,如下: SHOW VARIABLES LI ...

  8. R语言do.call 函数用法详解

    虽然R语言有类型很丰富的数据结构,但是很多时候数据结构比较复杂,那么基本就会用到list这种结构的数据类型.但是list对象很难以文本的形式导出,因此需要一个函数能快速将复杂的list结构扁平化成da ...

  9. Matlab中数据的存储方式

    简介 MATLAB提供了丰富的算法以及一个易于操作的语言,给算法研发工作者提供了很多便利.然而MATLAB在执行某些任务的时候,执行效率偏低,测试较大任务量时可能会引起较长时间的等待.未解决这个问题, ...

  10. Ubuntu 16.04 + ROS Kinetic 机器人操作系统学习镜像分享与使用安装说明

    Ubuntu 16.04 + ROS Kinetic 镜像分享与使用安装说明 内容概要:1 网盘文件介绍  2 镜像制作  3 系统使用与安装 ---- 祝ROS爱好者和开发者新年快乐:-) ---- ...