Access iOS Directory from Javascript

I have an application that has a local Javascript file as a resource. I am also running a node.js server on the device. I need the Javascript to be able to read a file that is in the Documents directory of the app. I know that the file url is dynamic and I can provide that to JS at runtime. My question is whether it's possible for JS to read a file directly from the iOS directory or is this walled off for security reasons? When trying I have gotten errors such as

Code Block
[connection] nw_socket_handle_socket_event [C2.1:2] Socket SO_ERROR [61: Connection refused]

I know libraries like Cordova can access files but I wonder if that is through native libraries that provide the file to JS in other ways.

The short of it is to understand if providing a local url to Javascript will allow it to access the file system or again if this is forbidden?

Are you running the JavaScript in a web view? If so, what type of web view? Or are you running it via JavaScriptCore?

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"
I am running a node.js server on device called Nodejs-mobile. When that is spun up it runs a JavaScript file that is also located in the app's bundle. I can interact with the JS through any number of means such as a WKWebView or a URLSession. In either case I have, at the iOS app level, the path to an on-device resource that I provide to JS. The desire here is for the local JS to then use that path to access the file. I am flexible to use differing mechanisms here if it enables JS to directly read a file. The JS uses fs.opensync in it's current attempt.

Thanks.

I am running a node.js server on device called Nodejs-mobile.

I’m not familiar with that third-party tool. What Apple API does it use to execute the JavaScript?

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"
I don't know specifically what they are using (it's a large library), however, I can also execute a script using WKWebView or URLSession. If it's possible to set aside the node.js library, is it possible for Javascript run locally to access the iOS file system given a path?

I don't know specifically what they are using (it's a large library)

Then I recommend that you raise this with the support channel for that library. I can answer questions about Apple APIs but it’s hard to say whether the approach I’m going to outline is correct for you because the library you’re using may already have a design in place for file system access.

Apropos Apple APIs, we have two recommended JavaScript runtimes:
  • JavaScriptCore

  • WKWebView [1]

Neither has a file system access mechanism built in. To access the file system you must bridge over to native code using the APIs bridging technology. That’s not super hard, but it’d be a pity to do all that work if the third-party library you’re using has already done it.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

[1] This actually uses JavaScriptCore internally but the bridging story is very different.
I've looked at the documentation for both, but don't see anything specific to the file system. Is there some Apple documentation or suite of methods you can point to? For the moment the only solution I can see here is to read the file natively and then socket the contents to the JS/Node side. Another method could be a multipart transfer via URLSession. Is this the kind of thing you are referring to or are you aware of other methods in JSCore or WKWeb?

Thanks.

I've looked at the documentation for both, but don't see anything
specific to the file system.

Indeed. Neither has file system specific support. Rather, both have generic JavaScript-to-native (and back) messaging, on which you can build your own file system integration. Specifically:
  • JaveScriptCore has comprehensive support for adding a native object to the JS context so that JavaScript can call methods on that object. A good place to start here is the <JavaScriptCore/JSObjectRef.h> header.

  • WKWebView has WKUserScript for loading integration scripts into the web view, WKScriptMessage for JavaScript-to-native messaging, and various methods on WKWebView itself for native-to-JavaScript messaging

Building a file system API on top of this would be fun, but also a lot of work, which is why I recommend that you discuss this with your third-party library vendor because they’ve probably already done all this work.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"
Thank you for the help and confirmation. This open source library does not have a file system from any of its documentation. They support things like Cordova which has other plug-ins but that doesn't help with a native approach. At this point I am using socket.io to provide binary to the JS locally.

Thanks again for your time and expertise.
Access iOS Directory from Javascript
 
 
Q