置顶] iOS中 UIWebView加载网络数据 技术分享

[置顶] iOS中 UIWebView加载网络数据 技术分享 

直奔核心:

[objc]  view plain  copy
  1. #import "TechnologyDetailViewController.h"  
  2. #define kScreenWidth [UIScreen mainScreen].bounds.size.width  
  3. #define kScreenHeight [UIScreen mainScreen].bounds.size.height  
  4. @interface TechnologyDetailViewController ()  
  5. @property (nonatomic,retain)UIWebView *webView;//显示详情页面  
  6. @end  

[objc]  view plain  copy
  1. @implementation TechnologyDetailViewController  
  2. - (void)dealloc  
  3. {  
  4.     self.webView = nil;  
  5.     self.ids = nil;  
  6.     [super dealloc];  
  7. }  
  8.   
  9. - (void)viewDidLoad {  
  10.     [super viewDidLoad];  
  11.     //设置背景  
  12.     self.view.backgroundColor = [UIColor yellowColor];  
  13.     //调用请求网络数据  
  14.     [self readDataFromNetWork];  
  15.     //    self.automaticallyAdjustsScrollViewInsets = NO;  
  16.     [self.view addSubview:self.webView];  
  17. }  


懒加载UIWebView

[objc]  view plain  copy
  1. //懒加载  
  2. - (UIWebView *)webView{  
  3.     if (!_webView) {  
  4.         self.webView = [[[UIWebView alloc]initWithFrame:CGRectMake(0,0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height)]autorelease];  
  5.         self.webView.backgroundColor = [UIColor orangeColor];  
  6.           
  7.     }  
  8.     return [[_webView retain ]autorelease];  
  9. }  


核心代码如下:

[objc]  view plain  copy
  1. //解析数据  
  2. - (void)readDataFromNetWork{  
  3.     NSString *str = [NSString stringWithFormat:@"http://c.3g.163.com/nc/article/%@/full.html",self.ids];  
  4.     NSURL *url = [NSURL URLWithString:str];  
  5.     NSURLRequest *request = [NSURLRequest requestWithURL:url];  
  6.     [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {  
  7.         NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];  
  8.           
  9.           
  10.         NSString *body = dic[self.ids][@"body"];  
  11.         NSString * title = dic[self.ids][@"title"];  
  12.         NSString *digest = dic[self.ids][@"digest"];  
  13.         NSString *ptime = dic[self.ids][@"ptime"];  
  14.         NSString * source = dic[self.ids][@"source"];  
  15.         NSArray * img = dic[self.ids][@"img" ] ;  
  16.         //        NSLog(@"%@",img);  
  17.         if (img.count) {  
  18.             for (int i = 0; i < img.count; i ++) {  
  19.                   
  20.                   
  21.                 NSString *imgString = [NSString stringWithFormat:@"<p style=\"text-align:center\"><img src=\"%@\" /></p>",img[i][@"src"]];  
  22.                 NSLog(@"imString  = %@",imgString);  
  23.                   
  24.                 imgString = [imgString stringByReplacingOccurrencesOfString:@"<!--IMG#%d-->" withString:imgString];  
  25.                 body = [imgString stringByAppendingString:body];  
  26.                   
  27.                   
  28.             }  
  29.         }  
  30.         //        NSLog(@"body =  %@",body);  
  31.           
  32.         //借助第三方进行排版  
  33.         NSString *filePath = [[NSBundle mainBundle]pathForResource:@"topic_template" ofType:@"html"];  
  34.         NSString *htmlString = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];  
  35.           
  36.         htmlString = [htmlString stringByReplacingOccurrencesOfString:@"__BODY__" withString:body];  
  37.         htmlString = [htmlString stringByReplacingOccurrencesOfString:@"__TITLE__" withString:title];  
  38.         htmlString = [htmlString stringByReplacingOccurrencesOfString:@"__AUTHOR__" withString:source];  
  39.         htmlString = [htmlString stringByReplacingOccurrencesOfString:@"__DIGEST__" withString:digest];  
  40.         htmlString = [htmlString stringByReplacingOccurrencesOfString:@"__TIME__" withString:ptime];  
  41.         htmlString = [htmlString stringByReplacingOccurrencesOfString:@"__PTIME__" withString:ptime];  
  42.         [self.webView loadHTMLString:htmlString baseURL:nil];  
  43.           
  44.           
  45.     }];  
  46.       
  47. }  


======================================================================================================

实在看不懂再看下demol例子:

UIWebView的loadRequest可以用来加载一个url地址,它需要一个NSURLRequest参数。我们定义一个方法用来加载url。在UIWebViewDemoViewController中定义下面方法:


[objc]  view plain  copy
  1. - (void)loadWebPageWithString:(NSString*)urlString  
  2. {  
  3.     NSURL *url =[NSURL URLWithString:urlString];  
  4.     NSLog(urlString);  
  5.     NSURLRequest *request =[NSURLRequest requestWithURL:url];  
  6.     [webView loadRequest:request];  
  7. }  


在界面上放置3个控件,一个textfield、一个button、一个uiwebview,布局如下:


在代码中定义相关的控件:webView用于展示网页、textField用于地址栏、activityIndicatorView用于加载的动画、buttonPress用于按钮的点击事件。


[objc]  view plain  copy
  1. @interface UIWebViewDemoViewController :UIViewController<UIWebViewDelegate> {     
  2.     IBOutlet UIWebView *webView;  
  3.     IBOutlet UITextField *textField;  
  4.     UIActivityIndicatorView *activityIndicatorView;  
  5.        
  6. }  
  7. - (IBAction)buttonPress:(id) sender;  
  8. - (void)loadWebPageWithString:(NSString*)urlString;  
  9. @end  


使用IB关联他们。

设置UIWebView,初始化UIActivityIndicatorView

[objc]  view plain  copy
  1. - (void)viewDidLoad  
  2. {  
  3.     [super viewDidLoad];  
  4.     webView.scalesPageToFit =YES;  
  5.     webView.delegate =self;  
  6.     activityIndicatorView = [[UIActivityIndicatorView alloc]   
  7.                              initWithFrame : CGRectMake(0.0f0.0f32.0f32.0f)] ;  
  8.     [activityIndicatorView setCenterself.view.center] ;  
  9.     [activityIndicatorView setActivityIndicatorViewStyle: UIActivityIndicatorViewStyleWhite] ;   
  10.     [self.view addSubview : activityIndicatorView] ;  
  11.     [self buttonPress:nil];  
  12.     // Do any additional setup after loading the view from its nib.  
  13. }  


UIWebView主要有下面几个委托方法:

1、- (void)webViewDidStartLoad:(UIWebView *)webView;开始加载的时候执行该方法。
2、- (void)webViewDidFinishLoad:(UIWebView *)webView;加载完成的时候执行该方法。
3、- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error;加载出错的时候执行该方法。

我们可以将activityIndicatorView放置到前面两个委托方法中。

[objc]  view plain  copy
  1. - (void)webViewDidStartLoad:(UIWebView *)webView  
  2. {  
  3.     [activityIndicatorView startAnimating] ;  
  4. }  
  5. - (void)webViewDidFinishLoad:(UIWebView *)webView  
  6. {  
  7.     [activityIndicatorView stopAnimating];  
  8. }  


buttonPress方法很简单,调用我们开始定义好的loadWebPageWithString方法就行了:

[objc]  view plain  copy
  1. - (IBAction)buttonPress:(id) sender  
  2. {  
  3.     [textField resignFirstResponder];  
  4.     [self loadWebPageWithString:textField.text];  
  5.        
  6. }  


当请求页面出现错误的时候,我们给予提示:

[objc]  view plain  copy
  1. - (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error  
  2. {  
  3.     UIAlertView *alterview = [[UIAlertView alloc] initWithTitle:@"" message:[error localizedDescription delegate:nil cancelButtonTitle:nil otherButtonTitles:@"OK", nil nil];  
  4.     [alterview show];  
  5.     [alterview release];  
  6. }  


总结:本文通过实现一个简单的浏览器,说明了uiwebview的方法和属性,相信通过这个例子,应该明白uiwebview的使用了。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值