Latest 1.02
Homepage https://github.com/7heaven/SHJavascriptInterface
License MIT
Platforms ios
Frameworks Foundation, UIKit
Authors

[license]()

JavascriptInterface


使用


1.

使用pod添加到项目中:

pod 'SHJavascriptInterface', '~>1.02'

2.

引入头文件:

import "UIWebView+JavascriptInterface.h"

简化JS调用iOS原生方法时的麻烦过程,在初始化时添加javascriptInterface:

[webView addJavascriptInterface:interfaceProvider forName:@"nativeCommon"];

并通过interfaceprovider的javascriptInterfaces返回需要js调用的原生方法和js调用名称:

- (NSDictionary<NSString *, NSValue *> *) javascriptInterfaces{
    return @{
             @"callNative" : [NSValue valueWithPointer:@selector(callNative:)]
             };
}

对应的原生方法:

- (NSString *) callNative:(NSString *) input{
    return [input stringByAppendingString:"-modified-by-native"];
}

然后js调用原生方法可以直接写成:

var result = nativeCommon.callNative(xxx);

Latest podspec

{
    "name": "SHJavascriptInterface",
    "version": "1.02",
    "license": "MIT",
    "homepage": "https://github.com/7heaven/SHJavascriptInterface",
    "authors": {
        "7heaven": "[email protected]"
    },
    "summary": "Easy to use javascript interface for iOS",
    "source_files": "*.{h,m,mm}",
    "source": {
        "git": "./"
    },
    "platforms": {
        "ios": null
    },
    "public_header_files": "*.h",
    "frameworks": [
        "Foundation",
        "UIKit"
    ]
}

Pin It on Pinterest

Share This