[iOS基础控件 - 7.0] UIWebView


//
// ViewController.m
// UIWebViewdDemo
//
// Created by hellovoidworld on 15/1/30.
// Copyright (c) 2015年 hellovoidworld. All rights reserved.
// #import "ViewController.h" @interface ViewController () <UISearchBarDelegate, UIWebViewDelegate> @property (weak, nonatomic) IBOutlet UIWebView *webView; @property (weak, nonatomic) IBOutlet UIBarButtonItem *backButton;
@property (weak, nonatomic) IBOutlet UIBarButtonItem *forwardButton;
- (IBAction)back:(UIBarButtonItem *)sender;
- (IBAction)forward:(UIBarButtonItem *)sender; @end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib. // 创建一个搜索栏
UISearchBar *searchBar = [[UISearchBar alloc] init];
searchBar.frame = CGRectMake(, , , );
searchBar.delegate = self; self.navigationItem.titleView = searchBar; self.webView.delegate = self;
} - (IBAction)back:(UIBarButtonItem *)sender {
NSLog(@"后退");
[self.webView goBack];
} - (IBAction)forward:(UIBarButtonItem *)sender {
NSLog(@"前进");
[self.webView goForward];
} #pragma mark - UIWebViewDelegate
- (void)webViewDidStartLoad:(UIWebView *)webView {
NSLog(@"开始加载");
} - (void)webViewDidFinishLoad:(UIWebView *)webView {
NSLog(@"完成加载"); // 设置后退、前进按钮
if ([self.webView canGoBack]) {
self.backButton.enabled = YES;
} else {
self.backButton.enabled = NO;
} if ([self.webView canGoForward]) {
self.forwardButton.enabled = YES;
} else {
self.forwardButton.enabled = NO;
}
} #pragma mark - UISearchBarDelegate
/** 点击搜索按钮(虚拟键盘上) */
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
NSString *str = searchBar.text;
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://%@", str]];
NSURLRequest *request = [NSURLRequest requestWithURL:url]; [self.webView loadRequest:request];
} @end

/** 运行js代码 */
- (IBAction)runJs {
NSString *str = @"alert('hello, world!');";
[self.webView stringByEvaluatingJavaScriptFromString:str];
}

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>运行OC代码</title>
</head>
<body> <script type="text/javascript">
function runOC() {
window.location.href="ios://openAlbum";
}
</script> <input type="button" value="运行OC代码" onclick="runOC();" /> </body>
</html>
/** webView发送请求之前都会调用,可以拦截请求,选择加载(返回YES)还是不加载(返回NO)*/
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType { // 拦截请求
NSString *urlStr = request.URL.absoluteString;
NSRange range = [urlStr rangeOfString:@"ios://"]; // 如果是ios请求
if (range.length != ) {
// 方法名
NSString *method = [urlStr substringFromIndex:range.location + range.length]; // 包装SEL
SEL selector = NSSelectorFromString(method); // 执行方法
[self performSelector:selector withObject:nil];
} return YES;
} /** 打开相册 */
- (void) openAlbum {
// 创建一个相片选择控制器
UIImagePickerController *imagePC = [[UIImagePickerController alloc] init];
[imagePC setSourceType:UIImagePickerControllerSourceTypePhotoLibrary]; // 使用相册来源 [self presentViewController:imagePC animated:YES completion:^{
NSLog(@"呼出相册");
}];
}
[iOS基础控件 - 7.0] UIWebView的更多相关文章
- [iOS基础控件 - 6.0] UITableView
A.需要掌握的 1.基本属性和方法 设置UITableView的dataSource.delegate UITableView多组数据和单组数据的展示 UITableViewCell的常见属性 UIT ...
- [iOS基础控件 - 5.5] 代理设计模式 (基于”APP列表"练习)
A.概述 在"[iOS基础控件 - 4.4] APP列表 进一步封装,初见MVC模式”上进一步改进,给“下载”按钮加上效果.功能 1.按钮点击后,显示为“已下载”,并且不 ...
- iOS 基础控件(下)
上篇介绍了UIButton.UILabel.UIImageView和UITextField,这篇就简短一点介绍UIScrollView和UIAlertView. UIScrollView 顾名思义也知 ...
- [iOS基础控件 - 6.11.3] 私人通讯录Demo 控制器的数据传递、存储
A.需求 1.搭建一个"私人通讯录"Demo 2.模拟登陆界面 账号 密码 记住密码开关 自动登陆开关 登陆按钮 3.退出注销 4.增删改查 5.恢复数据(取消修改) 这个代码 ...
- iOS基础 - 控件属性
一.控件的属性 1.CGRect frame 1> 表示控件的位置和尺寸(以父控件的左上角为坐标原点(0, 0)) 2> 修改这个属性,可以调整控件的位置和尺寸 2.CGPoint cen ...
- [iOS基础控件 - 6.10.2] PickerView 自定义row内容 国家选择Demo
A.需求 1.自定义一个UIView和xib,包含国家名和国旗显示 2.学习row的重用 B.实现步骤 1.准备plist文件和国旗图片 2.创建模型 // // Flag.h // Co ...
- [iOS基础控件 - 6.9.1] 聊天界面Demo 代码
框架: 所有代码文件: Model: // // Message.h // QQChatDemo // // Created by hellovoidworld on 14/12/8. // ...
- [iOS基础控件 - 6.9] 聊天界面Demo
A.需求 做出一个类似于QQ.微信的聊天界面 1.每个cell包含发送时间.发送人(头像).发送信息 2.使用对方头像放在左边,我方头像在右边 3.对方信息使用白色背景对话框,我方信息使用蓝色背景对话 ...
- [iOS基础控件 - 6.7] 微博展示 使用代码自定义TableCell(动态尺寸)
A.需求 1.类似于微博内容的展示 2.头像 3.名字 4.会员标志 5.内容 6.分割线 7.配图(可选,可有可无) code source: https://github.com/hellov ...
随机推荐
- Android studio中Rendering Problems不能可视化操作的解决办法
出现:Rendering Problems the following classes could not be found:android.support.v7.internal.widget.Ac ...
- JAVA的核心概念:接口(interface)
JAVA的核心概念:接口(interface) 接口与类属于同一层次,实际上,接口是一种特殊的抽象类. 如: interface IA{ } public interface: 公开接口 与 ...
- ha_innobase::general_fetch
/***********************************************************************//** Reads the next or previ ...
- Java实现RC4加解密
package com.vrv.paw.utils; public class RC4Util { public static String decry_RC4(byte[] data, String ...
- sql Server 的基本函数
--聚合函数 use pubs go select avg(distinct搜索 price) --算平均数 from titles where type='business' go use pubs ...
- 【C#学习笔记】检测进程是否存在并关闭
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...
- RTP头结构解析
RTP包头前12个固定字节机构图: 0 1 2 3 0 1 2 3 4 5 6 7 8 9 ...
- ftp在shell脚本中的使用方法
1. ftp自动登录批量下载文件. #####从ftp服务器上的/home/data 到 本地的/home/databackup#####!/bin/bashftp -n<<!open 1 ...
- Android Message和obtainMessage的区别
类概述 定义一个包含任意类型的描述数据对象,此对象可以发送给Handler.对象包含两个额外的int字段和一个额外的对象字段,这样可以使得在很多情况下不用做分配工作. 尽管Message的构造器是公开 ...
- @section Right
布局页 _BaseLayout.cshtml @RenderSection("Right", true) 视图页 Index.cshtml @{ Layout = "~/ ...