A library for Haxe/Lime that focuses on iOS development, giving access to native iOS features through Objective-C. Think of it as extension-androidtools, but for iOS.
haxelib git extension-iostools https://github.com/MultiSliceCrew/extension-iostools.gitAdd it to your Project.xml:
<haxelib name="extension-iostools" if="ios" />Classes are grouped in packages under ios (ios.app, ios.device, ios.ui, ios.hardware, ios.media, ios.net, ios.security, ios.games and ios.events) and only exist when compiling for iOS, so wrap your calls with #if ios.
| Class | What it does |
|---|---|
ios.app.App |
Bundle id, version, build, display name, app state, open URLs, open Settings and the App Store, review prompt, badge number, alternate app icons, TestFlight detection, background tasks |
ios.app.Paths |
Documents, Library, Caches, Application Support, tmp and bundle paths, free/total disk space, exclude files from iCloud backup |
ios.app.Share |
Native share sheet for text, links and files |
ios.app.Notifications |
Local notifications, cancel, clear and foreground presentation |
ios.device.Device |
Name, model, machine identifier, iOS version, battery, thermal state, low power mode, memory, CPU count, locale, language, region, timezone, orientation |
ios.device.Accessibility |
VoiceOver, Switch Control, reduce motion and transparency, bold text, darker colors, invert colors, grayscale, mono audio, captions, font scale, dark mode, announcements and change events |
ios.ui.Screen |
Brightness, keep screen awake, scale, size, safe area insets, max FPS, screen recording detection, screenshot, recording and brightness events |
ios.ui.Alert |
Native alerts, action sheets, confirm dialogs and text prompts |
ios.ui.Keyboard |
Keyboard visible state and height, change events, dismiss |
ios.ui.Toast |
Android-style toast messages |
ios.ui.Clipboard |
Read, write and clear the clipboard |
ios.hardware.Haptic |
Impact, notification and selection feedback, classic vibration, Core Haptics taps, rumbles, custom patterns and heartbeat |
ios.hardware.Gamepad |
Connected controllers, connect and disconnect events, battery, player index, light color and controller rumble |
ios.hardware.Motion |
Accelerometer, gyroscope, magnetometer, device attitude (roll, pitch, yaw, heading), gravity, user acceleration, barometer and shake detection |
ios.hardware.Flashlight |
Turn the torch on and off with a brightness level |
ios.media.Audio |
Audio session category, activation, other audio playing, output volume, headphones |
ios.media.Speech |
Text to speech with language, rate, pitch and volume, pause and resume |
ios.net.Network |
Connection state and type (Wi-Fi, cellular, wired), expensive/constrained paths, onChange signal |
ios.security.Permissions |
Status and requests for camera, microphone, photos and notifications |
ios.security.Biometrics |
Face ID, Touch ID and Optic ID detection and authentication, with passcode fallback |
ios.security.Keychain |
Strings and objects stored in the Keychain, kept after reinstalls, with optional iCloud sync |
ios.games.GameCenter |
Sign in, player info, scores, achievements, dashboard, leaderboards and the access point |
ios.events.Callback |
Global listener for every native event |
#if ios
import ios.app.*;
import ios.events.*;
import ios.games.*;
import ios.device.*;
import ios.hardware.*;
import ios.media.*;
import ios.security.*;
import ios.ui.*;
class Main
{
static function main():Void
{
Audio.setCategory(AMBIENT);
Screen.keepAwake = true;
var info:String = Device.machine + " / iOS " + Device.systemVersion;
Alert.show("Hello", info, ["Cancel", "Play"], function(index:Int):Void
{
if (index == 1)
Haptic.impact(HEAVY);
}, ALERT, 0);
Alert.prompt("Name", "Type your name", function(result:PromptResult):Void
{
if (result.confirmed)
Toast.show("Hi " + result.text);
});
Permissions.request(NOTIFICATIONS, function(granted:Bool, status:PermissionStatus):Void
{
if (granted)
Notifications.schedule("daily", "Come back!", "Your songs are waiting.", 60 * 60 * 24);
});
Share.file(Paths.documents + "/replay.json");
}
}
#endMotion.start(DEVICE_MOTION, 60);
Motion.enableShakeDetection();
var tilt:Float = Motion.roll;
var gravity:MotionVector = Motion.getGravity();
Flashlight.turnOn(0.5);
Speech.language = "pt-BR";
Speech.speak("Boa sorte!");
if (Biometrics.available)
{
Biometrics.authenticate("Unlock your save", function(result:BiometricResult):Void
{
if (result.success)
Toast.show("Unlocked");
});
}Sensors are read when you ask for a value, so call Motion.start once and read the values every frame. Stop them with Motion.stop or Motion.stopAll when you don't need them, because they use battery.
if (Haptic.advancedSupported)
{
Haptic.tap(0.8, 1.0);
Haptic.rumble(0.4, 1.0, 0.2);
Haptic.pattern([
{time: 0, intensity: 1, sharpness: 0.8},
{time: 0.1, continuous: true, duration: 0.3, intensity: 0.5}
]);
}
Gamepad.startMonitoring();
Gamepad.onConnect.add(function(pad:GamepadInfo):Void
{
Gamepad.setPlayerIndex(pad.index, 0);
Gamepad.setLightColor(pad.index, 0x31A2FD);
Gamepad.vibrate(pad.index, 0.2);
});
Keychain.set("token", token);
var saved:Null<String> = Keychain.get("token");
GameCenter.authenticate(function(result:RequestResult):Void
{
if (result.success)
GameCenter.submitScore("weekly", 120000);
});
GameCenter.unlockAchievement("first_clear");
GameCenter.showLeaderboard("weekly");
Screen.startMonitoring();
Screen.onScreenshot.add(() -> Toast.show("Nice shot!"));
if (Accessibility.reduceMotion)
camera.shakeEnabled = false;Keyboard.startMonitoring();
Keyboard.onChange.add(visible -> moveTextField(visible ? Keyboard.height : 0));
App.runInBackground("save", () -> save.flush());
var task = App.beginBackgroundTask("upload");
upload(() -> App.endBackgroundTask(task));Keyboard.height is in pixels. runInBackground keeps the app alive while the work runs after it goes to the background, and backgroundTimeRemaining gives the seconds left (-1 while in the foreground).
Game Center needs the Game Center capability on your App ID. Alternate icons have to be listed under CFBundleAlternateIcons in the Info.plist.
Some features keep sending events after they start. Modules with their own signals (Screen, Keyboard, Network, Gamepad, Accessibility, GameCenter) also dispatch typed events. Listen to everything through ios.events.Callback:
Device.startMonitoring();
Network.start();
Audio.startMonitoring();
Notifications.setForegroundPresentation(true);
Callback.addListener(function(event:String, data:Dynamic):Void
{
switch (event)
{
case "network":
Toast.show(data.connected ? "Online" : "Offline");
case "audio_interruption":
FlxG.sound.pause();
default:
}
});| Event | Data |
|---|---|
battery_level |
level |
battery_state |
state |
orientation |
orientation |
thermal_state |
state |
low_power_mode |
enabled |
memory_warning |
none |
network |
connected, type, expensive, constrained |
keyboard |
visible, height (pixels) |
background_task_expired |
task |
audio_interruption |
began, shouldResume |
audio_route |
reason, headphones |
notification_scheduled |
identifier, success, error |
notification_received |
identifier |
notification_opened |
identifier, action |
shake |
force |
speech_start |
text |
speech_finish |
text, cancelled |
screenshot |
none |
screen_captured |
captured |
screen_brightness |
brightness |
gamepad_connected |
Gamepad info |
gamepad_disconnected |
name, playerIndex |
accessibility |
name of the setting that changed |
game_center_auth_changed |
success, error |
game_center_closed |
none |
alert, prompt, share, permission, biometrics, alternate_icon, game_center_auth, game_center_score, game_center_achievement, game_center_reset |
Results of the matching calls, also delivered to their own callbacks |
All events are delivered on the main thread.
iOS needs a usage description before asking for some permissions, otherwise the app will crash. Add the keys you need to your Info.plist template:
| Permission | Key |
|---|---|
CAMERA |
NSCameraUsageDescription |
MICROPHONE |
NSMicrophoneUsageDescription |
PHOTOS |
NSPhotoLibraryUsageDescription |
PHOTOS_ADD_ONLY |
NSPhotoLibraryAddUsageDescription |
Biometrics.authenticate with Face ID |
NSFaceIDUsageDescription |
Motion.start(ALTIMETER) |
NSMotionUsageDescription |
To open other apps with App.canOpenURL, add their schemes to LSApplicationQueriesSchemes.
The native side is written in Objective-C (.mm files compiled with ARC) under project/src, split in folders that match the Haxe packages, and exposed through a plain C API (project/include/iostools.h). The Haxe externs in ios.internal.Native call that API directly, and project/Build.xml is pulled into the hxcpp build automatically. Frameworks are linked through include.xml.
MIT