iOS

Swift Package#

The Replay Swift package is hosted on GitHub. Once added as a package dependency to your Xcode project, you can replace the rootViewController:

import Replay
class SceneDelegate: UIResponder, UIWindowSceneDelegate {
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
if let windowScene = scene as? UIWindowScene {
let window = UIWindow(windowScene: windowScene)
window.rootViewController = ReplayViewController()
self.window = window
window.makeKeyAndVisible()
}
}
}

Your image and audio assets also need to be added to your Xcode project. See Replay Starter for an example setup.

ReplayViewController(hideStatusBar)#

Parameters#

  • hideStatusBar: (Optional) A boolean to set if the status bar is hidden or not. Default true.
  • onJsCallback: (Optional) A callback for messages sent from your game. See Bridge.

Methods#

  • jsBridge(messageId: String, jsArg: String): Send a value from Swift to your JS game code. See Bridge.

Inputs#

Since Replay Swift embeds your game as a web view, the device.inputs parameter of Sprite methods is an alias of the Web package's inputs:

type iOSInputs = WebInputs;

The @replay/swift package exports this type for TypeScript projects.

Bridge#

You can send asynchronous messages from your game's JS code to your Swift code, and then respond back using a Promise. This allows you to use native features like in-app purchases.

JS side#

import { makeSprite } from "@replay/core";
import { swiftBridge } from "@replay/swift";
export const BridgeSprite = makeSprite({
init({ device }) {
swiftBridge({
// This should be unique between parallel messages
id: "TestBridge",
// A string to send
message: "Hello!",
}).then((message) => {
// message is a value sent back from Swift code.
// This will log "Bridge response: Hi!"
device.log(`Bridge response: ${message.response}`);
});
},
render() {
return [];
},
});

Swift side#

var vc: ReplayViewController!
vc = ReplayViewController(onJsCallback: { (message) in
if (message == "Hello!") {
// Here you can call native APIs
let myApiVal = "Hi!"
vc.jsBridge(
// This should match the id above
messageId: "TestBridge",
// The return value of the Promise in JS code.
// Use a String which will be evaluated as JS code (like eval)
jsArg: "{ response: `\(myApiVal)` }"
)
}
})