一、OC調用js
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
self.title = [webView stringByEvaluatingJavaScriptFromString:@"doucument.title;"];
}
二、js調用OC
- 無參
- 在js中定義一個自己的協(xié)議頭
<script>
function method()
{
//login為oc方法
location.href = 'keven://login';
}
</script>
- 方法調用
//攔截請求
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
NSString *url = request.URL.absoluteString;
NSString *scheme = @"keven://";
if ([url hasPrefix:scheme]) {
//截取方法名
NSString *methodName = [url substringFromIndex:scheme.length];
//調用方法
[self performSelector:NSSelectorFromString(methodName) withObject:nil];
return NO;
}
return YES;
}
- 有參
- 一個參數(shù)的時候
[self performSelector:NSSelectorFromString(methodName) withObject:param];
- 兩個參數(shù)的時候
[self performSelector:NSSelectorFromString(methodName) withObject:firstParam withObject:secondParam];
- 多個參數(shù)的時候(使用NSInvocation,同樣適用于一個或者兩個參數(shù))
a. 給nsobject寫分類
#import "NSObject+Extension.h"
@implementation NSObject (Extension)
- (id)performSelector:(SEL)selector withObjects:(NSArray *)objects
{
// 方法簽名(方法的描述)
NSMethodSignature *signature = [[self class] instanceMethodSignatureForSelector:selector];
if (signature == nil) {
// @throw [NSException exceptionWithName:@"牛逼的錯誤" reason:@"方法找不到" userInfo:nil];
[NSException raise:@"牛逼的錯誤" format:@"%@方法找不到", NSStringFromSelector(selector)];
}
// NSInvocation : 利用一個NSInvocation對象包裝一次方法調用(方法調用者、方法名、方法參數(shù)、方法返回值)
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:signature];
invocation.target = self;
invocation.selector = selector;
// 設置參數(shù)
NSInteger paramsCount = signature.numberOfArguments - 2; // 除self、_cmd以外的參數(shù)個數(shù)
paramsCount = MIN(paramsCount, objects.count);
for (NSInteger i = 0; i < paramsCount; i++) {
id object = objects[i];
//傳入空參數(shù)的時候的處理
if ([object isKindOfClass:[NSNull class]]) continue;
//從2開始,應為索引0和1被占用了
[invocation setArgument:&object atIndex:i + 2];
}
// 調用方法
[invocation invoke];
// 獲取返回值
id returnValue = nil;
if (signature.methodReturnLength) { // 有返回值類型,才去獲得返回值
[invocation getReturnValue:&returnValue];
}
return returnValue;
}
@end
b. 在控制器中調用
[self performSelector:@selector(sendMessage:number2:number3:) withObjects:@[
[NSNull null],
[NSNull null],
@"10010"
]];
//等價于==》
[self sendMessage:nil number2:nil number3:@"10010"];
c. For Example
/**
* 通過這個方法完成JS調用OC
* JS和OC交互的第三方框架:WebViewJavaScriptBridge
*/
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
// url == xmg://sendMessage_?200
NSString *url = request.URL.absoluteString;
NSString *scheme = @"xmg://";
if ([url hasPrefix:scheme]) {
// 獲得協(xié)議后面的路徑 path == sendMessage_number2_?200&300
NSString *path = [url substringFromIndex:scheme.length];
// 利用?切割路徑
NSArray *subpaths = [path componentsSeparatedByString:@"?"];
// 方法名 methodName == sendMessage:number2:
NSString *methodName = [[subpaths firstObject] stringByReplacingOccurrencesOfString:@"_" withString:@":"];
// 參數(shù) 200&300
NSArray *params = nil;
if (subpaths.count == 2) {
params = [[subpaths lastObject] componentsSeparatedByString:@"&"];
}
// 調用
[self performSelector:NSSelectorFromString(methodName) withObjects:params];
return NO;
}
NSLog(@"想加載其他請求,不是想調用OC的方法");
return YES;
}