Skip to content

Commit e6380ef

Browse files
author
Jakey
committed
init
1 parent ee33bab commit e6380ef

17 files changed

+1101
-5
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
![image](https://raw.github.com/shaojiankui/WebViewJS/master/thumb.png)

WebViewJS.xcodeproj/project.pbxproj

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,14 @@
1919
A2E71E26197F911B0055B687 /* UIKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = A2E71E0E197F911B0055B687 /* UIKit.framework */; };
2020
A2E71E2E197F911B0055B687 /* InfoPlist.strings in Resources */ = {isa = PBXBuildFile; fileRef = A2E71E2C197F911B0055B687 /* InfoPlist.strings */; };
2121
A2E71E30197F911B0055B687 /* WebViewJSTests.m in Sources */ = {isa = PBXBuildFile; fileRef = A2E71E2F197F911B0055B687 /* WebViewJSTests.m */; };
22+
A2E71E40197F91360055B687 /* UIWebView+Blocks.m in Sources */ = {isa = PBXBuildFile; fileRef = A2E71E3B197F91360055B687 /* UIWebView+Blocks.m */; };
23+
A2E71E41197F91360055B687 /* UIWebView+Style.m in Sources */ = {isa = PBXBuildFile; fileRef = A2E71E3D197F91360055B687 /* UIWebView+Style.m */; };
24+
A2E71E42197F91360055B687 /* WebView+Alert.m in Sources */ = {isa = PBXBuildFile; fileRef = A2E71E3F197F91360055B687 /* WebView+Alert.m */; };
25+
A2E71E45197F913D0055B687 /* light_advice.png in Resources */ = {isa = PBXBuildFile; fileRef = A2E71E43197F913D0055B687 /* light_advice.png */; };
26+
A2E71E46197F913D0055B687 /* light_illstration.png in Resources */ = {isa = PBXBuildFile; fileRef = A2E71E44197F913D0055B687 /* light_illstration.png */; };
27+
A2E71E49197F91420055B687 /* ViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = A2E71E48197F91420055B687 /* ViewController.m */; };
28+
A2E71E4B197F91E90055B687 /* ViewController.xib in Resources */ = {isa = PBXBuildFile; fileRef = A2E71E4A197F91E90055B687 /* ViewController.xib */; };
29+
A2E71E4D197F92260055B687 /* 1.html in Resources */ = {isa = PBXBuildFile; fileRef = A2E71E4C197F92260055B687 /* 1.html */; };
2230
/* End PBXBuildFile section */
2331

2432
/* Begin PBXContainerItemProxy section */
@@ -48,6 +56,18 @@
4856
A2E71E2B197F911B0055B687 /* WebViewJSTests-Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = "WebViewJSTests-Info.plist"; sourceTree = "<group>"; };
4957
A2E71E2D197F911B0055B687 /* en */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = en; path = en.lproj/InfoPlist.strings; sourceTree = "<group>"; };
5058
A2E71E2F197F911B0055B687 /* WebViewJSTests.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = WebViewJSTests.m; sourceTree = "<group>"; };
59+
A2E71E3A197F91360055B687 /* UIWebView+Blocks.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "UIWebView+Blocks.h"; sourceTree = "<group>"; };
60+
A2E71E3B197F91360055B687 /* UIWebView+Blocks.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "UIWebView+Blocks.m"; sourceTree = "<group>"; };
61+
A2E71E3C197F91360055B687 /* UIWebView+style.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "UIWebView+style.h"; sourceTree = "<group>"; };
62+
A2E71E3D197F91360055B687 /* UIWebView+Style.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "UIWebView+Style.m"; sourceTree = "<group>"; };
63+
A2E71E3E197F91360055B687 /* WebView+Alert.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "WebView+Alert.h"; sourceTree = "<group>"; };
64+
A2E71E3F197F91360055B687 /* WebView+Alert.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "WebView+Alert.m"; sourceTree = "<group>"; };
65+
A2E71E43197F913D0055B687 /* light_advice.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = light_advice.png; sourceTree = "<group>"; };
66+
A2E71E44197F913D0055B687 /* light_illstration.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = light_illstration.png; sourceTree = "<group>"; };
67+
A2E71E47197F91420055B687 /* ViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ViewController.h; sourceTree = "<group>"; };
68+
A2E71E48197F91420055B687 /* ViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ViewController.m; sourceTree = "<group>"; };
69+
A2E71E4A197F91E90055B687 /* ViewController.xib */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.xib; path = ViewController.xib; sourceTree = "<group>"; };
70+
A2E71E4C197F92260055B687 /* 1.html */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.html; path = 1.html; sourceTree = "<group>"; };
5171
/* End PBXFileReference section */
5272

5373
/* Begin PBXFrameworksBuildPhase section */
@@ -107,8 +127,15 @@
107127
A2E71E10197F911B0055B687 /* WebViewJS */ = {
108128
isa = PBXGroup;
109129
children = (
130+
A2E71E39197F91360055B687 /* category */,
110131
A2E71E19197F911B0055B687 /* AppDelegate.h */,
111132
A2E71E1A197F911B0055B687 /* AppDelegate.m */,
133+
A2E71E47197F91420055B687 /* ViewController.h */,
134+
A2E71E48197F91420055B687 /* ViewController.m */,
135+
A2E71E4A197F91E90055B687 /* ViewController.xib */,
136+
A2E71E43197F913D0055B687 /* light_advice.png */,
137+
A2E71E44197F913D0055B687 /* light_illstration.png */,
138+
A2E71E4C197F92260055B687 /* 1.html */,
112139
A2E71E1C197F911B0055B687 /* Images.xcassets */,
113140
A2E71E11197F911B0055B687 /* Supporting Files */,
114141
);
@@ -144,6 +171,19 @@
144171
name = "Supporting Files";
145172
sourceTree = "<group>";
146173
};
174+
A2E71E39197F91360055B687 /* category */ = {
175+
isa = PBXGroup;
176+
children = (
177+
A2E71E3A197F91360055B687 /* UIWebView+Blocks.h */,
178+
A2E71E3B197F91360055B687 /* UIWebView+Blocks.m */,
179+
A2E71E3C197F91360055B687 /* UIWebView+style.h */,
180+
A2E71E3D197F91360055B687 /* UIWebView+Style.m */,
181+
A2E71E3E197F91360055B687 /* WebView+Alert.h */,
182+
A2E71E3F197F91360055B687 /* WebView+Alert.m */,
183+
);
184+
path = category;
185+
sourceTree = "<group>";
186+
};
147187
/* End PBXGroup section */
148188

149189
/* Begin PBXNativeTarget section */
@@ -219,8 +259,12 @@
219259
isa = PBXResourcesBuildPhase;
220260
buildActionMask = 2147483647;
221261
files = (
262+
A2E71E4B197F91E90055B687 /* ViewController.xib in Resources */,
263+
A2E71E46197F913D0055B687 /* light_illstration.png in Resources */,
222264
A2E71E15197F911B0055B687 /* InfoPlist.strings in Resources */,
223265
A2E71E1D197F911B0055B687 /* Images.xcassets in Resources */,
266+
A2E71E4D197F92260055B687 /* 1.html in Resources */,
267+
A2E71E45197F913D0055B687 /* light_advice.png in Resources */,
224268
);
225269
runOnlyForDeploymentPostprocessing = 0;
226270
};
@@ -239,8 +283,12 @@
239283
isa = PBXSourcesBuildPhase;
240284
buildActionMask = 2147483647;
241285
files = (
286+
A2E71E49197F91420055B687 /* ViewController.m in Sources */,
287+
A2E71E40197F91360055B687 /* UIWebView+Blocks.m in Sources */,
242288
A2E71E1B197F911B0055B687 /* AppDelegate.m in Sources */,
243289
A2E71E17197F911B0055B687 /* main.m in Sources */,
290+
A2E71E41197F91360055B687 /* UIWebView+Style.m in Sources */,
291+
A2E71E42197F91360055B687 /* WebView+Alert.m in Sources */,
244292
);
245293
runOnlyForDeploymentPostprocessing = 0;
246294
};

WebViewJS/1.html

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<html>
2+
<head>
3+
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
4+
5+
<style type="text/css">
6+
7+
.detail{
8+
color:#787878;
9+
font-size: 14px;
10+
}
11+
12+
.detail p{
13+
14+
text-indent:2em;
15+
}
16+
17+
.pic img{
18+
width:113px;
19+
height:20px;
20+
}
21+
22+
</style>
23+
<script type="text/javascript">
24+
function showAlert(hahahha) {
25+
alert(hahahha);
26+
}
27+
</script>
28+
</head>
29+
<body>
30+
31+
<div id="idTest">
32+
33+
</div>
34+
35+
<form method="get" action="http://www.baidu.com/s">
36+
<input type="text" id = "wd" name ="wd" value="123"/>
37+
<input type="submit" id ="submitButton" name ="submitButton" value="提交"/>
38+
</form>
39+
40+
41+
<div class="detail">
42+
<div class = "pic"><img src="light_illstration.png"></div>
43+
<p>
44+
颗粒滤清器故障,柴油颗粒过滤器需要进行还原。
45+
</p>
46+
</div>
47+
48+
<div class="detail">
49+
<div class = "pic"><img src="light_advice.png"></div>
50+
<p>
51+
请以 4 档或 5 档(或 S档)、最低 60 km/h 的速度行驶大约 15 分钟,使发动机转速保持在大约 2000 转/ 分钟,直至指示灯熄灭。如果指示灯没有熄灭,请立即驶往维修站排除故障。确保您的车速与天气、道路、地形和交通状况相适应。不得由于行车建议而不遵守道路交通的法规。
52+
</p>
53+
</div>
54+
55+
56+
</body>
57+
</html>

WebViewJS/AppDelegate.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77
//
88

99
#import <UIKit/UIKit.h>
10-
10+
#import "ViewController.h"
1111
@interface AppDelegate : UIResponder <UIApplicationDelegate>
1212

1313
@property (strong, nonatomic) UIWindow *window;
14-
14+
@property (strong, nonatomic) ViewController *viewController;
1515
@end

WebViewJS/AppDelegate.m

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,22 @@
22
// AppDelegate.m
33
// WebViewJS
44
//
5-
// Created by Jakey on 14-7-23.
6-
// Copyright (c) 2014年 Jakey. All rights reserved.
5+
// Created by jakey on 14-5-27.
6+
// Copyright (c) 2014年 jakey. All rights reserved.
77
//
88

99
#import "AppDelegate.h"
1010

11+
#import "ViewController.h"
12+
1113
@implementation AppDelegate
1214

1315
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
1416
{
1517
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
1618
// Override point for customization after application launch.
17-
self.window.backgroundColor = [UIColor whiteColor];
19+
self.viewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];
20+
self.window.rootViewController = self.viewController;
1821
[self.window makeKeyAndVisible];
1922
return YES;
2023
}

WebViewJS/ViewController.h

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
//
2+
// ViewController.h
3+
// WebViewJS
4+
//
5+
// Created by jakey on 14-5-27.
6+
// Copyright (c) 2014年 jakey. All rights reserved.
7+
//
8+
9+
#import <UIKit/UIKit.h>
10+
11+
@interface ViewController : UIViewController
12+
@property (weak, nonatomic) IBOutlet UIWebView *myWeb;
13+
14+
@property (strong, nonatomic) NSString *someString;
15+
16+
- (IBAction)loadTouched:(id)sender;
17+
- (IBAction)exeFuncTouched:(id)sender;
18+
- (IBAction)insertHtmTouched:(id)sender;
19+
- (IBAction)inputButtonTouched:(id)sender;
20+
- (IBAction)insertJSTouched:(id)sender;
21+
- (IBAction)insertDivHtml:(id)sender;
22+
- (IBAction)submitTouched:(id)sender;
23+
- (IBAction)replaceImgSrc:(id)sender;
24+
@end

WebViewJS/ViewController.m

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
//
2+
// ViewController.m
3+
// WebViewJS
4+
//
5+
// Created by jakey on 14-5-27.
6+
// Copyright (c) 2014年 jakey. All rights reserved.
7+
//
8+
9+
#import "ViewController.h"
10+
@interface ViewController ()
11+
12+
@end
13+
14+
@implementation ViewController
15+
16+
- (void)viewDidLoad
17+
{
18+
[super viewDidLoad];
19+
// Do any additional setup after loading the view, typically from a nib.
20+
self.someString = @"UIWebView是iOS最常用的SDK之一,它有一个stringByEvaluatingJavaScriptFromString方法可以将javascript嵌入页面中,通过这个方法我们可以在iOS中与UIWebView中的网页元素交互";
21+
22+
23+
[self loadTouched:nil];
24+
}
25+
26+
- (void)didReceiveMemoryWarning
27+
{
28+
[super didReceiveMemoryWarning];
29+
// Dispose of any resources that can be recreated.
30+
}
31+
32+
- (IBAction)loadTouched:(id)sender {
33+
NSString *filePath = [[NSBundle mainBundle]pathForResource:@"1" ofType:@"html"];
34+
35+
NSURL *url = [NSURL fileURLWithPath:filePath];
36+
37+
NSURLRequest *request = [NSURLRequest requestWithURL:url];
38+
39+
[self.myWeb loadRequest:request];
40+
}
41+
42+
43+
44+
//执行已经存在的js方法
45+
- (IBAction)exeFuncTouched:(id)sender {
46+
[self.myWeb stringByEvaluatingJavaScriptFromString:@"showAlert(\"hahahha\");"];
47+
}
48+
49+
//插入html getElementsByTagName 根据html自带标签定位元素
50+
- (IBAction)insertHtmTouched:(id)sender {
51+
//插入整个页面内容
52+
// document.getElementsByTagName('body')[0];"
53+
54+
//替换第一个P元素内容
55+
NSString *tempString = [NSString stringWithFormat:@"document.getElementsByTagName('p')[0].innerHTML ='%@';",self.someString];
56+
[self.myWeb stringByEvaluatingJavaScriptFromString:tempString];
57+
58+
}
59+
60+
61+
// 插入html 根据getElementById 定位元素
62+
- (IBAction)insertDivHtml:(id)sender {
63+
64+
//替换第id为idtest的DIV元素内容
65+
NSString *tempString2 = [NSString stringWithFormat:@"document.getElementById('idTest').innerHTML ='%@';",self.someString];
66+
[self.myWeb stringByEvaluatingJavaScriptFromString:tempString2];
67+
68+
}
69+
70+
71+
//修改input值 getElementsByName根据标签名称获取定位元素
72+
- (IBAction)inputButtonTouched:(id)sender {
73+
NSString *tempString = [NSString stringWithFormat:@"document.getElementsByName('wd')[0].value='%@';",self.someString];
74+
[self.myWeb stringByEvaluatingJavaScriptFromString:tempString];
75+
}
76+
77+
//插入js 并且执行传值
78+
- (IBAction)insertJSTouched:(id)sender {
79+
NSString *insertString = [NSString stringWithFormat:
80+
@"var script = document.createElement('script');"
81+
"script.type = 'text/javascript';"
82+
"script.text = \"function jsFunc() { "
83+
"var a=document.getElementsByTagName('body')[0];"
84+
"alert('%@');"
85+
"}\";"
86+
"document.getElementsByTagName('head')[0].appendChild(script);", self.someString];
87+
88+
NSLog(@"insert string %@",insertString);
89+
[self.myWeb stringByEvaluatingJavaScriptFromString:insertString];
90+
[self.myWeb stringByEvaluatingJavaScriptFromString:@"jsFunc();"];
91+
}
92+
93+
- (IBAction)submitTouched:(id)sender {
94+
95+
[self.myWeb stringByEvaluatingJavaScriptFromString:@"document.forms[0].submit(); "];
96+
}
97+
//替换图片地址
98+
- (IBAction)replaceImgSrc:(id)sender {
99+
NSString *tempString2 = [NSString stringWithFormat:@"document.getElementsByTagName('img')[0].src ='%@';",@"light_advice.png"];
100+
[self.myWeb stringByEvaluatingJavaScriptFromString:tempString2];
101+
}
102+
@end

0 commit comments

Comments
 (0)