apache / cordova-ios

Apache Cordova iOS
https://cordova.apache.org/
Apache License 2.0
2.15k stars 987 forks source link

When I inject js in iOS, there will be some errors, how should I inject custom js? #1263

Closed stupidchou closed 1 year ago

stupidchou commented 1 year ago

Bug Report

When I inject js in iOS, there will be some errors, how should I inject custom js?

Problem

When I inject js in iOS, there will be some errors, how should I inject custom js?

What is expected to happen?

When I inject js in iOS, there will be some errors, how should I inject custom js?

What does actually happen?

When I inject js in iOS, there will be some errors, how should I inject custom js?

Information

When I inject js in iOS, there will be some errors, how should I inject custom js?

Command or Code

When I inject js in iOS, there will be some errors, how should I inject custom js?

Environment, Platform, Device

When I inject js in iOS, there will be some errors, how should I inject custom js?

Version information

Checklist

breautek commented 1 year ago

When I inject js in iOS, there will be some errors, how should I inject custom js?

You should load up JS using traditional means, such as a <script> tag.

Alternatively, you can use the standard Function constructor, which will create a function with the scope of the global object, but this isn't the recommended approach due to similar issues (although less severe) to the standard eval function. MDN writes about the bad practices behind both eval and the Function constructor.

If you're authoring a Cordova plugin, then refer to the iOS plugin documentation. It contains examples of how to do cross communication between the native and javascript environments. Generally speaking, the application should respond to data with an appropriate, predefined function instead relying on the eval solution, which will likely introduce a security risk into your app.