详解iPhone开发应用中如何使用Web视图

移动开发 iOS
iPhone开发应用中如何使用Web视图是本文要介绍的内容,主要是来了解并学习iphone开发中的Web视图如何来使用,具体内容看本文详解。

iPhone开发应用中如何使用Web视图是本文要介绍的内容,主要是来了解并学习iphone开发中的Web视图如何来使用,具体内容看本文详解。

一、使用UIWebView 将web content 嵌入到应用上。

API提供了三种方法:

  1. - (void)loadRequest:(NSURLRequest *)request;  
  2.  
  3.  - (void)loadHTMLString:(NSString *)string baseURL:(NSURL *)baseURL;  
  4.  
  5. - (void)loadData:(NSData *)data MIMEType:(NSString *)  
  6.             MIMEType textEncodingName:(NSString *)textEncodingName baseURL:(NSURL *)baseURL; 

1、直接给出url地址即可将web content载入。

  1. NSString *path = @"http://theo2life.com";  
  2. NSURL *url = [[NSURL alloc] initWithString:path];  
  3. [self.webView loadRequest:[NSURLRequest requestWithURL:url]]; 

2、将本地html文件内容嵌入webView

  1. NSString *resourcePath = [ [NSBundle mainBundle] resourcePath];  
  2. NSString *filePath  = [resourcePath stringByAppendingPathComponent:@"test.html"];  
  3. NSString *htmlstring =[[NSString alloc] initWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];   
  4. [self.webView loadHTMLString:htmlstring  baseURL:[NSURL fileURLWithPath: [[NSBundle mainBundle]  bundlePath]]];  

如果不从html文件载入你也可以这样:

  1. NSString *HTMLData = @"<img src=\"test2.png\" />ddd";  
  2. [self.webView loadHTMLString:HTMLData baseURL:[NSURL fileURLWithPath: [[NSBundle mainBundle]  bundlePath]]];  
  3. baseURL:[NSURL fileURLWithPath: [[NSBundle mainBundle]  bundlePath]] 

这段指出HTMLData所引用的其他文件资源的基本路径,如果baseURL:nil图片信息将不会显示出来~

3、同2,更详细的给出了web content的编码方式。

二、其他操作:

如果载入的web布局大过ipad尺寸发现超出的部分会是空白,则设置webView.scalesPageToFit = YES;让web content布局适应webView。

小结:详解iPhone开发应用中如何使用Web视图的内容介绍完了,希望通过本文的学习能对你有所帮助!

责任编辑:zhaolei 来源: 互联网
相关推荐

2011-08-12 10:04:24

iPhone开发视图

2011-08-10 17:37:00

iPhoneASIHTTPRequ

2011-08-12 14:33:06

iPhone缓存文件

2011-08-15 11:37:20

iPhone开发Mask

2011-08-11 17:32:51

iPhone视图

2011-07-19 09:58:36

2011-07-19 09:46:38

2011-08-02 16:28:40

iPhone Web开发 事件

2011-08-15 18:02:32

iPhone开发表视图

2011-07-27 10:16:41

iPhone SQLite 数据库

2011-08-08 13:50:29

iPhone开发 NSOperatio 多线程

2011-07-27 11:14:37

iPhone UITableVie

2011-08-18 15:56:08

iPhone开发内存

2011-08-05 16:10:31

iPhone 网络 NSURLConne

2011-08-02 17:27:06

iPhone应用 剪切技巧

2011-07-29 10:51:41

iPhone 全屏显示 视图

2011-08-08 14:07:49

iPhone开发 字体

2011-08-19 14:14:14

iPhone应用

2011-07-06 17:48:30

iPhone Xcode 模拟器

2011-07-18 14:39:53

iPhone SDK UIKit
点赞
收藏

51CTO技术栈公众号