我正在尝试从android和iOS Swift(WKWebview)中调用函数javascript。
对于Android:
public class WebAppInterface {
WebAppInterface(Context c) {
mContext = c;
}
@JavascriptInterface
public void postMessage(String message) {
Log.v(TAG, "message ----"+message);
Toast.makeText(mContext, message, Toast.LENGTH_SHORT).show();
}
}
webView.addJavascriptInterface(new WebAppInterface(mContext), "appInterface");
Run Code Online (Sandbox Code Playgroud)
Android的JavaScript调用:
window.appInterface.postMessage("Hello);
Run Code Online (Sandbox Code Playgroud)
iOS的Java语言调用:
window.webkit.messageHandlers.appInterafce.postMeesage("Hello");
Run Code Online (Sandbox Code Playgroud)
因此,我们在这里从javascriptfor android和发出不同的调用iOS。是否有可能调用函数在这两个android和iOS一个单一的方式?
谢谢。
我有一个使用 JSONObject 的函数,我需要测试它。这是我的代码:
这是我想测试的代码:
public String getJsonData() {
try {
InputStream is = mContext.getAssets().open("chartInfo.json");
int size = is.available();
byte[] buffer = new byte[size];
if (is.read(buffer) > 0)
jsonString = new String(buffer, "UTF-8");
is.close();
} catch (IOException ex) {
ex.printStackTrace();
return null;
}
return jsonString;
}
public String getChartTypeJS() {
jsonString = getJsonData();
try {
JSONObject jsonObject = new JSONObject(jsonString);
JSONObject javascriptEvent_JsonObject = jsonObject.getJSONObject("javascript_events");
return javascriptEvent_JsonObject.getString("chartType");
} catch (JSONException e) {
e.printStackTrace();
}
return "";
}
Run Code Online (Sandbox Code Playgroud)
我的测试代码:
@RunWith(MockitoJUnitRunner.class)
public class LoadJsonData_Test …Run Code Online (Sandbox Code Playgroud) android ×2
ios ×1
java ×1
javascript ×1
junit ×1
mockito ×1
unit-testing ×1
webview ×1
wkwebview ×1