UIWebView中Html中用JS調用OC方法及OC執行JS代碼
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
|
HTML代碼: <html> <head> <title>HTML中用JS調用OC方法</title> <meta
http-equiv="Content-Type" content="text/html; charset=UTF- 8 "> [removed] function
openAlbum() { //
打開相冊(openMyAlbum爲OC中的方法) [removed] .href
= 'ios: //openMyAlbum'; } function
openCamera() { //
打開相機(openMyCamera爲OC中的方法) [removed] .href
= 'ios: //openMyCamera'; } [removed] </head> <body> <br/> <br/> <br/> <input
type="button" value="打開相機"> <br><br/> <input
type="button" value="打開相冊"> </body> </html> OC代碼: #import
"ViewController.h" @interface
ViewController
()<UIWebViewDelegate> @property
(weak, nonatomic )
IBOutlet
UIWebView
*webview; -
( IBAction )getTitle; @end @implementation
ViewController -
( void )viewDidLoad { [ super
viewDidLoad ]; NSURL
*url = [[ NSBundle
mainBundle ]
URLForResource : @"test.html"
withExtension :nil ]; [ self .webview
loadRequest :[ NSURLRequest
requestWithURL :url]]; self .webview .delegate
= self ; self .webview .dataDetectorTypes
= UIDataDetectorTypeAll ; } #pragma
mark UIWebViewDelegate方法 /** *開始加重請求攔截 */ -( BOOL )webView:( UIWebView
*)webView
shouldStartLoadWithRequest :( NSURLRequest
*)request
navigationType :(UIWebViewNavigationType)navigationType { NSString
*urlstr = request .URL .absoluteString ; if
(range .length != 0 )
{ NSString
*method = [urlstr
substringFromIndex :(range .location +range .length )]; SEL
selctor = NSSelectorFromString(method); [ self
performSelector :selctor
withObject :nil ]; } return
YES ; } /** *
OC執行JS代碼 */ -
( IBAction )getTitle
{ [ self .webview
stringByEvaluatingJavaScriptFromString : @"alert(document.title);" ]; } -( void )openMyAlbum { UIImagePickerController
*vc = [[ UIImagePickerController
alloc ]init]; vc .sourceType
= UIImagePickerControllerSourceTypePhotoLibrary ; [ self
presentViewController :vc
animated : YES
completion :nil ]; } -( void )openMyCamera { UIImagePickerController
*vc = [[ UIImagePickerController
alloc ]init]; vc .sourceType
= UIImagePickerControllerSourceTypeCamera ; [ self
presentViewController :vc
animated : YES
completion :nil ]; } @end Html段的代碼
少了一些 我補充下 可以直接用這個替代 原作者的 就可以了 |
-
12345678910111213141516171819202122232425262728
<html>
<head>
<meta http-equiv="Content-Type" content="text/html"; charset="UTF-
8
"/>
<title>HTML中用JS調用OC方法</title>
[removed]
function openAlbum()
{
// 打開相冊(openMyAlbum爲OC中的方法)
[removed]
.href
= 'ios:
//openMyAlbum';
}
function openCamera()
{
// 打開相機(openMyCamera爲OC中的方法)
[removed]
.href
= 'ios:
//openMyCamera';
}
[removed]
</head>
<body>
<br/>
<br/>
<br/>
<input type="button" value="打開相機" > <br><br/>
<input type="button" value="打開相冊"><br/>
<input type=button name=smt value='確定'>
</body>
</html>
Tag : UIWebView , JavaSriptConquer-2014 創建 • 2015-02-11 15:24:09 -
<html>
<head>
<meta http-equiv="Content-Type" content="text/html"; charset="UTF-8"/>
<title>HTML中用JS調用OC方法</title>
[removed]
function openAlbum()
{
// 打開相冊(openMyAlbum爲OC中的方法)
[removed].href = 'ios://openMyAlbum';
}
function openCamera()
{
// 打開相機(openMyCamera爲OC中的方法)
[removed].href = 'ios://openMyCamera';
}
[removed]
</head>
<body>
<input type="button" value="打開相機" >
<input type="button" value="打開相冊"><br/>
<input type=button name=smt value='確定'>
</body>
</html>1code
4
會自動過濾一些腳步也是醉了
Conquer-2014 創建 • 2015-02-11 15:25:50 -
這纔是對的,12345678910111213141516171819202122232425262728293031
<html>
<head>
<meta http-equiv="Content-Type" content="text/html"; charset="UTF-
8
"/>
<title>HTML中用JS調用OC方法</title>
[removed]
function openAlbum()
{
// 打開相冊(openMyAlbum爲OC中的方法)
[removed]
.href
= 'ios:
//openMyAlbum';
}
function openCamera()
{
// 打開相機(openMyCamera爲OC中的方法)
[removed]
.href
= 'ios:
//openMyCamera';
}
[removed]
</head>
<body>
<br/>
<br/>
<br/>
<input type="button" value="打開相機" >
<br>
<br/>
<input type="button" value="打開相冊">
<br/>
<input type=button name=smt value='確定'>
</body>
</html>
-
<html>
<head>
<meta http-equiv="Content-Type" content="text/html"; charset="UTF-8"/>
<title>HTML中用JS調用OC方法</title>
[removed]
function openAlbum()
{
// 打開相冊(openMyAlbum爲OC中的方法)
[removed].href = 'ios://openMyAlbum';
}
function openCamera()
{
// 打開相機(openMyCamera爲OC中的方法)
[removed].href = 'ios://openMyCamera';
}
[removed]
</head>
<body>
<input type="button" value="打開相機" >
<input type="button" value="打開相冊">
<input type=button name=smt value='確定'>
</body>
</html>
代碼寫在這裏,有些東西被自動改掉了。12345678910111213141516171819202122232425262728293031<html>
<head>
<meta http-equiv="Content-Type" content="text/html"; charset="UTF-
8
"/>
<title>HTML中用JS調用OC方法</title>
[removed]
function openAlbum()
{
// 打開相冊(openMyAlbum爲OC中的方法)
[removed]
.href
= 'ios:
//openMyAlbum';
}
function openCamera()
{
// 打開相機(openMyCamera爲OC中的方法)
[removed]
.href
= 'ios:
//openMyCamera';
}
[removed]
</head>
<body>
<br/>
<br/>
<br/>
<input type="button" value="打開相機" >
<br>
<br/>
<input type="button" value="打開相冊">
<br/>
<input type=button name=smt value='確定'>
</body>
</html>