Apple services
Game Center, Sign in with Apple, iCloud, StoreKit purchases and notifications from Balaur Script, and how to set each up in project.toml and at Apple.
На тази страница
On iOS and macOS, Game Center and iCloud answer the portable platform.*
calls, and the apple module holds what only Apple has: Sign in with Apple,
StoreKit purchases, notifications, URLs and Game Center's own screens.
apple::available()says whether this build has Apple's frameworks.- A build with no bundle id (a run from the shell, a test) answers
unsupportedto every Game Center and iCloud call. - Calls answer
task::waitwith a map whosekindis anapple::EVENT_*constant. What arrives unasked is theapple_eventevent, on a node that calledevents::listen(node, "apple_event").
The bundle
Every Apple service is resolved against the bundle, so each starts in the
[apple] table. balaur export writes the Info.plist and one entitlement
per capability, and refuses a capability the minimum OS cannot run.
[apple]
bundle_id = "com.studio.game"
team_id = "AB12CD34EF"
version = "1.0"
build_number = "1"
capabilities = ["game-center", "sign-in-with-apple", "icloud-kv", "in-app-purchase"]
| Capability | Entitlement written | Lowest OS |
|---|---|---|
game-center |
com.apple.developer.game-center |
iOS 13.5, macOS 10.15 |
sign-in-with-apple |
com.apple.developer.applesignin |
iOS 13, macOS 10.15 |
icloud-kv |
com.apple.developer.ubiquity-kvstore-identifier, from team_id and bundle_id |
iOS 5, macOS 10.7 |
in-app-purchase |
none: it checks the OS has StoreKit 2 | iOS 15, macOS 12 |
At Apple, the same capabilities are switched on for the App ID. In Certificates, Identifiers and Profiles, open Identifiers, the App ID with this bundle id, and tick each one. An iOS build's provisioning profile must be made after that, since it carries the entitlements. Forge signs with it: the iOS signing guide says where it goes. On macOS Apple treats these entitlements as restricted, so a signed build needs a provisioning profile that grants them; the export has no macOS profile key yet.
Game Center
platform::sign_in signs the player in, showing Game Center's sheet when
nobody is. Achievements and leaderboards go through platform::unlock,
progress, submit_score and scores, by the ids made in App Store
Connect, under the app's Game Center section.
pub async fn init() {
let me = await task::wait(platform::sign_in());
if me["kind"].text() != platform::EVENT_SIGNED_IN {
return;
}
platform::submit_score("best_time", 4210);
let proof = await task::wait(apple::identity());
// proof["player"], ["url"], ["signature"], ["salt"], ["timestamp"]:
// what a server posts to Apple to check this player.
apple::access_point(true, apple::AccessPointOptions { location: "top_trailing" });
}
apple::identityanswers the four items a server needs to verify a Game Center player,signatureandsaltin base64.apple::show_dashboardopens Game Center's own screens, andapple::access_pointshows or hides its floating button.apple::listen_for_inviteshears an accepted invite asinvite_accepted, and a match the player asked for asmatch_requested.
Sign in with Apple
A second account, separate from Game Center's: the Apple ID itself, through the system's own sheet on iOS and macOS.
pub async fn sign_in_with_apple() -> str {
let answer = await task::wait(apple::sign_in());
if answer["kind"].text() != apple::EVENT_SIGNED_IN {
return "";
}
// The authorization code, base64 like the token: decoded before it is posted.
let code = encoding::from_utf8(encoding::from_base64(answer["code"].text()));
code.or("")
}
- The answer carries
user(Apple's stable id for this player),token(the identity token) andcode(the authorization code), both base64. nameandemailcome once, on the first authorization. Keep them then.apple::credential_state(user)says later whether that account is stillauthorized, orrevoked,transferredornot_found.
Setting it up:
capabilities = ["sign-in-with-apple"]in[apple].- At Apple, tick Sign In with Apple on the App ID, and make the provisioning profile again.
- Under Keys, make a key with Sign in with Apple enabled for that App
ID. Download its
.p8, once, and note its Key ID. - For sign-in on the web too, make a Services ID under Identifiers, enable Sign in with Apple on it, and list the server's domain and return URL.
The server checks the code with Apple. Gamend does, given these settings:
| Gamend setting | Value |
|---|---|
GAMEND_OAUTH_APPLE_IOS_CLIENT_ID |
the bundle id, the audience of a native sign-in |
GAMEND_OAUTH_APPLE_CLIENT_ID |
the Services ID, for the web page |
GAMEND_OAUTH_APPLE_TEAM_ID |
the team id |
GAMEND_OAUTH_APPLE_KEY_ID, GAMEND_OAUTH_APPLE_PRIVATE_KEY |
the key's id, and the .p8 file's contents |
The game posts the decoded code as { "code": … } to
/api/v1/auth/apple/ios/callback, and gets a session back.
iCloud
platform::cloud_read and cloud_write go to the iCloud key-value store,
which syncs small values between the player's devices.
- It needs
capabilities = ["icloud-kv"], and ateam_id: the entitlement holds<team>.<bundle id>, and nothing expands it outside Xcode. - Tick iCloud on the App ID.
apple::listen_for_cloud_changeshears another device's change ascloud_changed, with thereasonand thekeysit touched.
Purchases
StoreKit 2, behind apple::products, purchase, entitlements,
restore_purchases and finish_purchase. The products are made in App
Store Connect, under the app's In-App Purchases.
pub async fn buy_coins() {
let bought = await task::wait(apple::purchase("com.studio.game.coins"));
if bought["kind"].text() == apple::EVENT_PURCHASED {
// Send bought["jws"] to the server. Once it has granted the coins:
apple::finish_purchase(bought["id"].text());
}
}
capabilities = ["in-app-purchase"], which needs iOS 15 or macOS 12.- A purchase answers
purchased,cancelledorpending. A purchase carriesjws, the signed transaction a server verifies. - Finish a purchase only after the server granted it. StoreKit hands an unfinished one back on every launch.
restore_purchasesis the button App Review expects a game to have.- The engine carries a small Swift shim for StoreKit 2. A machine with no
Swift toolchain still builds the engine, and purchases there answer
unsupported.
Notifications
apple::request_notificationsasks the player, and answers whether they allowed it.apple::notify(body, apple::NotifyOptions { title: "…", after: 3600.0 })schedules a local one;cancel_notificationdrops it.- A notification tapped, or shown while the game is in front, arrives as
notification_openedornotification_received. apple::register_for_pushasks for a push token, which arrives as thepush_tokenevent. Push also needs the Push Notifications capability on the App ID and itsaps-environmententitlement, which[apple] capabilitiesdoes not write yet. Without it the OS answerspush_failed.apple::listen_for_urlshears a URL the game is asked to open, the one it was launched with included, asurl.
Not yet run against Apple
Game Center, iCloud, StoreKit, the sign-in page and the arrivals above are compiled and tested through a canned backend. They have not yet answered on a device signed with an Apple ID and a provisioning profile. The Apple module's reference lists every call and constant.