Skip to content

Commit ca61fad

Browse files
committed
oc 切换本地h5头像
oc 切换本地h5头像
1 parent 6eda063 commit ca61fad

File tree

6 files changed

+31
-13
lines changed

6 files changed

+31
-13
lines changed

ChinaDailyForiPad.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
310D1C302074A86A00FF3CBE /* [email protected] in Resources */ = {isa = PBXBuildFile; fileRef = 310D1C2E2074A86A00FF3CBE /* [email protected] */; };
2121
310D1C312074A86A00FF3CBE /* [email protected] in Resources */ = {isa = PBXBuildFile; fileRef = 310D1C2F2074A86A00FF3CBE /* [email protected] */; };
2222
3120AD6D20BBF90800CDEE5A /* JStoOC.html in Resources */ = {isa = PBXBuildFile; fileRef = 3120AD6C20BBF90800CDEE5A /* JStoOC.html */; };
23+
31C7E522223F7AC2005753A9 /* girl.png in Resources */ = {isa = PBXBuildFile; fileRef = 31C7E521223F7AC1005753A9 /* girl.png */; };
2324
/* End PBXBuildFile section */
2425

2526
/* Begin PBXContainerItemProxy section */
@@ -62,6 +63,7 @@
6263
310D1C2E2074A86A00FF3CBE /* [email protected] */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = "[email protected]"; sourceTree = "<group>"; };
6364
310D1C2F2074A86A00FF3CBE /* [email protected] */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = "[email protected]"; sourceTree = "<group>"; };
6465
3120AD6C20BBF90800CDEE5A /* JStoOC.html */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.html; path = JStoOC.html; sourceTree = "<group>"; };
66+
31C7E521223F7AC1005753A9 /* girl.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = girl.png; sourceTree = "<group>"; };
6567
/* End PBXFileReference section */
6668

6769
/* Begin PBXFrameworksBuildPhase section */
@@ -112,6 +114,7 @@
112114
310D1BEE2074674600FF3CBE /* ChinaDailyForiPad */ = {
113115
isa = PBXGroup;
114116
children = (
117+
31C7E521223F7AC1005753A9 /* girl.png */,
115118
310D1C2E2074A86A00FF3CBE /* [email protected] */,
116119
310D1C2F2074A86A00FF3CBE /* [email protected] */,
117120
310D1C2A2074A83D00FF3CBE /* [email protected] */,
@@ -257,6 +260,7 @@
257260
files = (
258261
3120AD6D20BBF90800CDEE5A /* JStoOC.html in Resources */,
259262
310D1BFC2074674600FF3CBE /* LaunchScreen.storyboard in Resources */,
263+
31C7E522223F7AC2005753A9 /* girl.png in Resources */,
260264
310D1C2D2074A83E00FF3CBE /* [email protected] in Resources */,
261265
310D1BF92074674600FF3CBE /* Assets.xcassets in Resources */,
262266
310D1C312074A86A00FF3CBE /* [email protected] in Resources */,

ChinaDailyForiPad.xcodeproj/xcuserdata/wangshuanglong.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,11 @@
2020
ignoreCount = "0"
2121
continueAfterRunningActions = "No"
2222
filePath = "ChinaDailyForiPad/ViewController.m"
23-
timestampString = "550659703.837243"
23+
timestampString = "574586182.598644"
2424
startingColumnNumber = "9223372036854775807"
2525
endingColumnNumber = "9223372036854775807"
26-
startingLineNumber = "430"
27-
endingLineNumber = "430"
26+
startingLineNumber = "438"
27+
endingLineNumber = "438"
2828
landmarkName = "-webView:runJavaScriptTextInputPanelWithPrompt:defaultText:initiatedByFrame:completionHandler:"
2929
landmarkType = "7">
3030
</BreakpointContent>
@@ -36,11 +36,11 @@
3636
ignoreCount = "0"
3737
continueAfterRunningActions = "No"
3838
filePath = "ChinaDailyForiPad/ViewController.m"
39-
timestampString = "550659703.837772"
39+
timestampString = "574586182.59914"
4040
startingColumnNumber = "9223372036854775807"
4141
endingColumnNumber = "9223372036854775807"
42-
startingLineNumber = "441"
43-
endingLineNumber = "441"
42+
startingLineNumber = "449"
43+
endingLineNumber = "449"
4444
landmarkName = "-webView:createWebViewWithConfiguration:forNavigationAction:windowFeatures:"
4545
landmarkType = "7">
4646
</BreakpointContent>

ChinaDailyForiPad/JStoOC.html

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
<h2 style="text-align:center">Welcome To You</h2>
1919

2020
<!-- 图片-->
21-
<p style="text-align:center"> <img src="https://upload.jianshu.io/users/upload_avatars/1708447/fe2c67e7-25f7-4e2e-a321-a5d517c124aa.jpeg?imageMogr2/auto-orient/strip|imageView2/1/w/240/h/240" width="128" height="128" align="middle" onclick="clickImage();" /> </p>
21+
<p style="text-align:center"> <img src="https://upload.jianshu.io/users/upload_avatars/1708447/fe2c67e7-25f7-4e2e-a321-a5d517c124aa.jpeg?imageMogr2/auto-orient/strip|imageView2/1/w/240/h/240" id= "pictureId" width="128" height="128" align="middle" onclick="clickImage();" /> </p>
2222

2323
<p style="text-align: center ;background-color: #e6b500"> 且行且珍惜_iOS </p>
2424

@@ -54,10 +54,10 @@ <h2 style="text-align:center">Welcome To You</h2>
5454
<!-- JS语法-->
5555
<script type = "text/javascript">
5656

57-
function jsToOcFunction1()
58-
{
59-
window.webkit.messageHandlers.jsToOcNoPrams.postMessage({});
60-
}
57+
function jsToOcFunction1()
58+
{
59+
window.webkit.messageHandlers.jsToOcNoPrams.postMessage({});
60+
}
6161

6262
function jsToOcFunction2()
6363
{
@@ -72,7 +72,7 @@ <h2 style="text-align:center">Welcome To You</h2>
7272
//图片点击事件
7373
function clickImage()
7474
{
75-
alert("欢迎你关注我!");
75+
alert("欢迎你关注我!点击了图片");
7676
}
7777

7878
//OC调用JS改变背景色
@@ -90,6 +90,12 @@ <h2 style="text-align:center">Welcome To You</h2>
9090
return "rgb("+r+','+g+','+b+")";//所有方法的拼接都可以用ES6新特性`其他字符串{$变量名}`替换
9191
}
9292

93+
// 切换图片
94+
function changePicture(id, path) {
95+
var image = document.getElementById(id);
96+
image.src = path;
97+
}
98+
9399
</script>
94100

95101
</body>

ChinaDailyForiPad/ViewController.m

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,9 @@ - (void)refreshAction:(id)sender{
140140

141141
- (void)ocToJs{
142142

143-
//OC调用JS changeColor()是JS方法名,completionHandler是异步回调block
143+
//OC调用JS
144+
145+
//changeColor()是JS方法名,completionHandler是异步回调block
144146
NSString *jsString = [NSString stringWithFormat:@"changeColor('%@')", @"Js参数"];
145147
[_webView evaluateJavaScript:jsString completionHandler:^(id _Nullable data, NSError * _Nullable error) {
146148
NSLog(@"改变HTML的背景色");
@@ -150,6 +152,12 @@ - (void)ocToJs{
150152
NSString *jsFont = [NSString stringWithFormat:@"document.getElementsByTagName('body')[0].style.webkitTextSizeAdjust= '%d%%'", arc4random()%99 + 100];
151153
[_webView evaluateJavaScript:jsFont completionHandler:nil];
152154

155+
NSString * path = [[NSBundle mainBundle] pathForResource:@"girl" ofType:@"png"];
156+
NSString *jsPicture = [NSString stringWithFormat:@"changePicture('%@','%@')", @"pictureId",path];
157+
[_webView evaluateJavaScript:jsPicture completionHandler:^(id _Nullable data, NSError * _Nullable error) {
158+
NSLog(@"切换本地头像");
159+
}];
160+
153161
}
154162

155163
#pragma mark -- Getter

ChinaDailyForiPad/girl.png

194 KB
Loading

0 commit comments

Comments
 (0)