`
coolerbaosi
  • 浏览: 730964 次
文章分类
社区版块
存档分类
最新评论

xcode UIWebView 装载本地页面或是远程页面的过程

 
阅读更多

很多网友碰到如题这样的问题,特别是对于一个iphone初学者,今天我整理了一下,分享给大家

1.建立一个单视图工程

2.在controller的接口文件添加如下代码。

#import <UIKit/UIKit.h>

@interface leekexiViewController : UIViewController
//添加的
{
    UIWebView* web;
}
@end

3.在IB界面拖入一个UIWebView控件

4.在controller的实现文件添加如下代码。

   //这部分是实现远程调用
    web=[[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 460)];
    NSURL *url =[NSURL URLWithString:@"http://192.168.20.166:8080/LOMO/test/touch.html"]; 
    NSURLRequest *request =[NSURLRequest requestWithURL:url]; 
    [web setUserInteractionEnabled: YES ]; //是否支持交互
    [web loadRequest:request];  
    [self.view addSubview:web];
    web.delegate = self;


记住两种情况不要一起用,如果非要一起用,那么要控制好web.alpha(透明度)
   //这部分是实现本地调用,本地的文件放在一个www的文件里面
    web=[[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 460)];
    [web setUserInteractionEnabled: YES ]; //是否支持交互
    NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"index" ofType:@"html" inDirectory:@"www"]];
    [web loadRequest:[NSURLRequest requestWithURL:url]];
    [web setOpaque:NO];
    [self.view addSubview:web];
    web.delegate = self; 

5.如果是远程控制的,那么确保你的Web服务器在运行中。



分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics