티스토리 뷰
iOS의 Web <-> App통신
1. App -> Web
1) 사용법
[webView stringByEvaluatingJavaScriptFromString:(Javascript명)];
2) 예제
[self.webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"callJavascriptFromObjectiveC('%@')", @"Example"]];
3) Javascript
function callJavascriptFromObjectiveC(text) {
alert('text : ' + text);
}
2. Web -> App
1) 사용법
.h file
<UIWebViewDelegate> 선언
.m file
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
2) 예제
.h file
@interface ViewController : UIViewController <UIWebViewDelegate>
.m file
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
if([requestUrl hasPrefix:@"jscall://"]) {
NSArray *components = [requestUrl componentsSeparatedByString:@"://"];
NSString *functionName = [components objectAtIndex:1];
if([self respondsToSelector:NSSelectorFromString(functionName)]) {
SEL selector = NSSelectorFromString(functionName);
IMP imp = [self methodForSelector:selector];
void (*func)(id, SEL) = (void*)imp;
func(self, selector);
return NO;
}
}
return YES;
}
3) Javascript
function callObjectiveCFromJavascript(){
window.location="jscall://callObjectiveCFromJavascript";
}
callObjectiveCFromJavascript();
오늘도 즐거운 하루~
'Mobile > iOS' 카테고리의 다른 글
[iOS]performSelector의 메모리 leak warning (0) | 2016.04.04 |
---|---|
[iOS]다른 앱 호출 (0) | 2016.03.30 |
[iOS]Async 처리 (0) | 2016.03.25 |
[iOS]NSString 처리 (0) | 2016.03.25 |
[iOS]xcode 단축키 (0) | 2016.03.25 |
- Total
- Today
- Yesterday
- windows10
- SWIFT
- php
- docker
- rxswift
- go
- war
- MySQL
- CentOS
- Kotlin
- android
- Xcode
- Windows
- intellij
- golang
- github
- enum
- cocoapods
- nodejs
- tomcat
- git
- ios
- Python
- Java
- Spring
- Codable
- centos8
- Linux
- ubuntu
- Gradle
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |