码迷,mamicode.com
首页 > 移动开发 > 详细

IOS开发-UI学习-UIWebView,简单浏览器的制作

时间:2016-04-01 23:30:00      阅读:234      评论:0      收藏:0      [点我收藏+]

标签:

制作一个简单的浏览器,包含网址输入框,Search按钮,前进、回退按钮,UIWebView就这几个简单的控件。

UITextField:用来输入网址;

UIbuttom:实现前进,后退,搜索等功能;

UIWebView:实现网页展示。

 

准备工作:

右键Info.plist并Open As       Source Code,打开之后添加以下代码段:

 

1     <key>NSAppTransportSecurity</key>
2     <dict>
3     <key>NSAllowsArbitraryLoads</key>
4     <true/>
5     </dict>

 

以上代码段功能:有些网址为Http,要搞成Https,具体原理以后探索出来了再补充。

 

言归正传,实现浏览器功能的具体代码如下:

 1 #import "ViewController.h"
 2 
 3 @interface ViewController (){
 4     
 5 //    定义全局变量,各个控件
 6     UIWebView *mywebview;
 7     UIButton *backbutton;
 8     UIButton *goforbutton;
 9     UITextField *urlField;
10     UIButton *searchButton;
11 }
12 
13 @end
14 
15 @implementation ViewController
16 
17 - (void)viewDidLoad {
18     [super viewDidLoad];
19     
20 //    添加浏览器view,
21     mywebview = [[UIWebView alloc]initWithFrame:CGRectMake(0, 70, self.view.frame.size.width, self.view.frame.size.height-120)];
22     [self.view addSubview:mywebview];
23     
24     
25 //    网址输入框
26     urlField = [[UITextField alloc]initWithFrame:CGRectMake(0, 20, self.view.frame.size.width-100, 50)];
27     urlField.borderStyle = UITextBorderStyleRoundedRect;
28     urlField.text= mywebview.request.URL.absoluteString;
29     [self.view addSubview:urlField];
30     
31     
32     
33 //    search button
34     searchButton = [[UIButton alloc]initWithFrame:CGRectMake(self.view.frame.size.width-100, 20, 100, 50)];
35     searchButton.backgroundColor = [UIColor redColor];
36     [searchButton setTitle:@"Search" forState:UIControlStateNormal];
37     [searchButton addTarget:self action:@selector(search:) forControlEvents:UIControlEventTouchUpInside];
38     [self.view addSubview:searchButton];
39     
40     
41 //    返回按键
42     backbutton = [[UIButton alloc]initWithFrame:CGRectMake(0, self.view.frame.size.height-50, 100, 50)];
43     backbutton.backgroundColor = [UIColor redColor];
44     [backbutton setTitle:@"返回" forState:UIControlStateNormal];
45     [backbutton addTarget:self action:@selector(backFbution:) forControlEvents:UIControlEventTouchUpInside];
46     [self.view addSubview:backbutton];
47     
48 //    前进按钮
49     goforbutton = [[UIButton alloc]initWithFrame:CGRectMake(self.view.frame.size.width-100, self.view.frame.size.height-50, 100, 50)];
50     goforbutton.backgroundColor = [UIColor redColor];
51     [goforbutton setTitle:@"前进" forState:UIControlStateNormal];
52     [goforbutton addTarget:self action:@selector(goFobution:) forControlEvents:UIControlEventTouchUpInside];
53     [self.view addSubview:goforbutton];
54     
55     
56 }
57 
58 
59 //返回按钮绑定事件
60 -(void)backFbution:(id)sender{
61     [mywebview goBack];
62 }
63 
64 
65 //前进按钮绑定事件
66 -(void)goFobution:(id)sender{
67     [mywebview goForward];
68 }
69 
70 
71 //搜索按钮绑定事件
72 -(void)search:(id)sender{
73     [mywebview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:urlField.text]]];
74 }
75 
76 
77 - (void)didReceiveMemoryWarning {
78     [super didReceiveMemoryWarning];
79 }
80 
81 @end

 

运行效果如下(请无视UI所用颜色。。。。。):

技术分享

存在一个问题,就是我在百度里面随便点击一个网址链接之后,可以打开网址,但打开之后浏览器的网址输入框中的网址依然是之前那个,没有改变为当前网站地址。这个问题遗留下来。。。

 

IOS开发-UI学习-UIWebView,简单浏览器的制作

标签:

原文地址:http://www.cnblogs.com/jiwangbujiu/p/5346573.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!