JavaScript library, designed to facilitate communication with iOS devices. The library’s interface is Promise-based.
In order to use ios-device-lib, just add a reference to it in your package.json by executing
$ npm install --save --save-exact ios-device-lib
In your project. Now you are ready to use ios-device-lib in your project:
const DeviceLib = require("ios-device-lib");
const dl = new DeviceLib.IOSDeviceLib(device => {
console.log("Device FOUND!", device);
dl.install("./app.ipa", [device.deviceId])
.forEach(promise => {
promise.then(response => {
console.log("INSTALL PASSED", response);
}).catch(err => {
console.log("An error occurred ;(", err);
});
});
}, device => {
console.log("Device UPDATED!", device);
}, device => {
console.log("Device LOST!", device);
});The ios-device-lib package can be built on either Windows (requires Visual Studio) or macOS (requires Xcode). In order to build the application one should simply open the .sln or .xcodeproj file with the respective tool and click build. Whenever building in release configuration, the result binary will end up in bin/<platform-name>/<architecture>, relative to the root of this repository. For example bin/win32/x64/ or bin/darwin/x64/. The JavaScript counterpart of the C++ code expects the binaries to be present in those exact locations, so one would have to build at least once prior to using the application.
Releases are built and published by the ios-device-lib -> npm GitHub Actions workflow (.github/workflows/npm_release.yml), which compiles the universal macOS binary on a hosted macOS runner, packs it together with the JavaScript layer, and publishes to npm with provenance. The manual Xcode build is only needed for local development. Windows binaries are not currently built: the Windows sources stopped compiling with the iOS 17 changes and need to be repaired before a windows-latest job can be added to build-native.yml.
- Every push to
masterpublishes a rolling prerelease under thenextdist-tag. - To cut a stable release, run the workflow manually (Actions ->
ios-device-lib -> npm-> Run workflow) and enter a semver keyword (patch,minor,major) or an explicit version (e.g.0.9.6). The workflow bumpspackage.json, commits, tagsv<version>, publishes underlatest(or the prerelease id's dist-tag for0.10.0-alpha.1style versions) and creates a GitHub release with the tarball attached. - Pushing a
v*tag by hand also publishes that version. - Every push to
masterand every pull request also publishes a preview build to pkg.pr.new; the bot comments the install URL on the pull request (npm i https://pkg.pr.new/ios-device-lib@<sha>).
Publishing uses npm trusted publishing (OIDC) from the npm-publish environment, so no npm token is stored in the repository. To publish with a granular token instead, set the repository variable USE_NPM_TOKEN to true and store the token in the NPM_PUBLISH_TOKEN secret.
This application consists of two separate layers - one C++ and one Node.js. The C++ layer implements the application's business logic, whereas the Node.js layer exists simply for alleviation. Whenever the Node.js part is used, it launches the C++ binary and initiates communication with it. This communication consists of requests to the binary via the stdin pipe and responses from the binary via the stdout pipe. Currently the stderr pipe is only used for debugging purposes.
The nature of this way of communication imposes some quirks:
- All request messages are marked with an unique identifier which is also present in the response messages. This way the
Node.jslayer can distinguish between different messages and match a request with it's response counterpart - All messages are printed in binary on the
stdout(and thestderr) pipe. Each message is prefixed with a 4 byte header, containing the length of the succeeding message. These 4-byte prefixes appear as peculiar symbols in the console whenever one decides to launch the binary directly. This is necessary as the messages have variable length and in addition it alleviates printing from theC++code as the strings we want to print might containNULLcharacters which would otherwise terminate printing.
In order to use the application directly one can either launch the binary, either directly or from within an IDE, or use the JavaScript API. Whenever using this library make sure that there is at least one actual iOS device attached to the system. This library has no functionality whatsoever without actual devices.
Upon launching the binary it will report all devices currently attached in the following form:
{
"deviceColor": "1",
"deviceId": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
"deviceName": "iPhone",
"event": "deviceFound",
"productType": "iPhone9,4",
"productVersion": "11.0.3",
"status": "Connected"
}
After that the binary is ready to accept requests via its standart input. Currently each passed message must be on one line, ending with a new line (Enter key) in order for it to be parsed correctly. Each message contains the name of the method, which you'd like to invoke, an identification string and the method's arguments. Messages are processed asynchronously, hence multiple messages can be passed at once. Example messages for the different opperations can be found below:
{
"methods": [
{
"id": "1",
"name": "apps",
"args": [
"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
"yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy"
]
}
]
}
{
"methods": [
{
"id": "2",
"name": "install",
"args": [
"C:\\\apps\\\app.ipa",
[
"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
"yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy"
]
]
}
]
}
{
"methods": [
{
"id": "3",
"name": "uninstall",
"args": [
"com.sample.MyApp",
[
"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
"yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy"
]
]
}
]
}
{
"methods": [
{
"id": "4",
"name": "list",
"args": [
{
"appId": "com.sample.MyApp",
"deviceId": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
"path": "Library\\\Application Support
}
]
}
]
}
{
"methods": [
{
"id": "5",
"name": "upload",
"args": [
{
"appId": "com.sample.MyApp",
"deviceId": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
"source": "D:\\\Project\\\app.js",
"destination": "Library\\\Application Support\\\LiveSync\\\app\\\app.js"
}
]
}
]
}
{
"methods": [
{
"id": "6",
"name": "delete",
"args": [
{
"appId": "com.sample.MyApp",
"deviceId": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
"destination": "Library\\\Application Support\\\LiveSync\\\app\\\app.js"
}
]
}
]
}
{
"methods": [
{
"id": "7",
"name": "read",
"args": [
{
"appId": "com.sample.MyApp",
"deviceId": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
"path": "Library\\\Application Support\\\LiveSync\\\app\\\app.js"
}
]
}
]
}
{
"methods": [
{
"id": "8",
"name": "download",
"args": [
{
"appId": "com.sample.MyApp",
"deviceId": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
"source": "Library\\\Application Support\\\LiveSync\\\app\\\app.js",
"destination": "D:\\\Downloads\\\app.js"
}
]
}
]
}
{
"methods": [
{
"id": "9",
"name": "log",
"args": [
"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
]
}
]
}
Post a notification to a device. (As if it was dispatched via NSNotificationCenter). This call is non-blocking.
{
"methods": [
{
"id": "10",
"name": "postNotification",
"args": [
{
"deviceId": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
"commandType": "PostNotification",
"notificationName": "com.sample.MyApp:NativeScript.Debug.AttachAvailabilityQuery"
}
]
}
]
}
{
"methods": [
{
"id": "11",
"name": "postNotification",
"args": [
{
"deviceId": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
"commandType": "ObserveNotification",
"notificationName": "com.sample.MyApp:NativeScript.Debug.AttachAvailable"
}
]
}
]
}
{
"methods": [
{
"id": "12",
"name": "awaitNotificationResponse",
"args": [
{
"deviceId": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
"socket": 10,
"timeout": 9,
"responseCommandType": "RelayNotification",
"responsePropertyName": "Name"
}
]
}
]
}
{
"methods": [
{
"id": "13",
"name": "start",
"args": [
{
"appId": "com.sample.MyApp",
"deviceId": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
}
]
}
]
}
{
"methods": [
{
"id": "14",
"name": "stop",
"args": [
{
"appId": "com.sample.MyApp",
"deviceId": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
}
]
}
]
}
A detailed definition of all the methods can be found here
- Upon launching the
ios-device-libbinary it will immediately start a detached thread with a run loop so that it can proactively detect attaching and detaching of devices. - All requests to the binary are executed in separate threads
- Error understanding: Whenever an error is raised, for example:
{
"deviceId": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
"error": {
"code": -402653081,
"deviceId": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
"message": "Could not install application"
},
"id": "1"
}
How does one go about deciphering the error code?
First off you'd need to convert the code to hex (this can easily be done with a calculator application for example). So -402653081 becomes FFFFFFFFE8000067 or E8000067 if we disregard the sign. Using this hex code you can look the error up in this header file. In this example we can see that this is a kAMDAPIInternalError, hence an internal error.