• webView(简单的浏览器)


     1 #import "MJViewController.h"
     2 
     3 @interface MJViewController () <UISearchBarDelegate, UIWebViewDelegate>
     4 
     5 @property (weak, nonatomic) IBOutlet UIWebView *webView;
     6 @property (weak, nonatomic) IBOutlet UIBarButtonItem *backButton;
     7 @property (weak, nonatomic) IBOutlet UIBarButtonItem *forwarButton;
     8 
     9 @end
    10 
    11 @implementation MJViewController
    12 
    13 - (void)viewDidLoad
    14 {
    15     [super viewDidLoad];
    16 
    17     [self loadString:@"http://m.baidu.com"];
    18 }
    19 
    20 // 让浏览器加载指定的字符串,使用m.baidu.com进行搜索
    21 - (void)loadString:(NSString *)str
    22 {
    23     // 1. URL 定位资源,需要资源的地址
    24     NSString *urlStr = str;
    25     if (![str hasPrefix:@"http://"]) {
    26         urlStr = [NSString stringWithFormat:@"http://m.baidu.com/s?word=%@", str];
    27     }
    28     
    29     NSURL *url = [NSURL URLWithString:urlStr];
    30     
    31     // 2. 把URL告诉给服务器,请求,从m.baidu.com请求数据
    32     NSURLRequest *request = [NSURLRequest requestWithURL:url];
    33     
    34     // 3. 发送请求给服务器
    35     [self.webView loadRequest:request];
    36 }
    37 
    38 #pragma mark - 搜索栏代理
    39 // 开始搜索
    40 - (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
    41 {
    42     NSLog(@"%@", searchBar.text);
    43     [self loadString:searchBar.text];
    44     
    45     [self.view endEditing:YES];
    46 }
    47 
    48 // 文本改变
    49 - (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
    50 {
    51     NSLog(@"searchText - %@", searchText);
    52 }
    53 
    54 #pragma mark - WebView代理方法
    55 #pragma mark 完成加载,页面链表数据会更新
    56 - (void)webViewDidFinishLoad:(UIWebView *)webView
    57 {
    58     // 根据webView当前的状态,来判断按钮的状态
    59     self.backButton.enabled = webView.canGoBack;
    60     self.forwarButton.enabled = webView.canGoForward;
    61 }
    62 
    63 @end
  • 相关阅读:
    程序清单 8-8 exec函数实例,a.out是程序8-9产生的可执行程序
    程序清单8-9 回送所有命令行参数和所有环境字符串
    程序清单8-3 8-4 演示不同的exit值
    C和指针 3.9作用域、存储类型示例
    程序4-6 utime函数实例
    程序4-5 打开一个文件,然后unlink
    C和指针笔记 3.8 static关键字
    C和指针笔记 3.7 存储类型
    C和指针笔记 3.6链接属性
    python爬虫<urlopen error [Errno 10061] >
  • 原文地址:https://www.cnblogs.com/liuguan/p/5060134.html
Copyright © 2020-2023  润新知