plugins_for_ios.rst 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529
  1. :article_outdated: True
  2. .. _doc_plugins_for_ios:
  3. Plugins for iOS
  4. ===============
  5. Godot provides StoreKit, GameCenter, iCloud services and other plugins.
  6. They are using same model of asynchronous calls explained below.
  7. ARKit and Camera access are also provided as plugins.
  8. Latest updates, documentation and source code can be found at `Godot iOS plugins repository <https://github.com/godotengine/godot-ios-plugins>`_
  9. Accessing plugin singletons
  10. ---------------------------
  11. To access plugin functionality, you first need to check that the plugin is
  12. exported and available by calling the `Engine.has_singleton()` function, which
  13. returns a registered singleton.
  14. Here's an example of how to do this in GDScript:
  15. ::
  16. var in_app_store
  17. var game_center
  18. func _ready():
  19. if Engine.has_singleton("InAppStore"):
  20. in_app_store = Engine.get_singleton("InAppStore")
  21. else:
  22. print("iOS IAP plugin is not available on this platform.")
  23. if Engine.has_singleton("GameCenter"):
  24. game_center = Engine.get_singleton("GameCenter")
  25. else:
  26. print("iOS Game Center plugin is not available on this platform.")
  27. Asynchronous methods
  28. --------------------
  29. When requesting an asynchronous operation, the method will look like
  30. this:
  31. ::
  32. Error purchase(Variant params);
  33. The parameter will usually be a Dictionary, with the information
  34. necessary to make the request, and the call will have two phases. First,
  35. the method will immediately return an Error value. If the Error is not
  36. 'OK', the call operation is completed, with an error probably caused
  37. locally (no internet connection, API incorrectly configured, etc). If
  38. the error value is 'OK', a response event will be produced and added to
  39. the 'pending events' queue. Example:
  40. ::
  41. func on_purchase_pressed():
  42. var result = in_app_store.purchase({ "product_id": "my_product" })
  43. if result == OK:
  44. animation.play("busy") # show the "waiting for response" animation
  45. else:
  46. show_error()
  47. # put this on a 1 second timer or something
  48. func check_events():
  49. while in_app_store.get_pending_event_count() > 0:
  50. var event = in_app_store.pop_pending_event()
  51. if event.type == "purchase":
  52. if event.result == "ok":
  53. show_success(event.product_id)
  54. else:
  55. show_error()
  56. Remember that when a call returns OK, the API will *always* produce an
  57. event through the pending_event interface, even if it's an error, or a
  58. network timeout, etc. You should be able to, for example, safely block
  59. the interface waiting for a reply from the server. If any of the APIs
  60. don't behave this way it should be treated as a bug.
  61. The pending event interface consists of two methods:
  62. - ``get_pending_event_count()``
  63. Returns the number of pending events on the queue.
  64. - ``Variant pop_pending_event()``
  65. Pops the first event from the queue and returns it.
  66. Store Kit
  67. ---------
  68. Implemented in `Godot iOS InAppStore plugin <https://github.com/godotengine/godot-ios-plugins/blob/master/plugins/inappstore/in_app_store.mm>`_.
  69. The Store Kit API is accessible through the ``InAppStore`` singleton.
  70. It is initialized automatically.
  71. The following methods are available and documented below:
  72. ::
  73. Error purchase(Variant params)
  74. Error request_product_info(Variant params)
  75. Error restore_purchases()
  76. void set_auto_finish_transaction(bool enable)
  77. void finish_transaction(String product_id)
  78. and the pending events interface:
  79. ::
  80. int get_pending_event_count()
  81. Variant pop_pending_event()
  82. ``purchase``
  83. ~~~~~~~~~~~~
  84. Purchases a product ID through the Store Kit API. You have to call ``finish_transaction(product_id)`` once you
  85. receive a successful response or call ``set_auto_finish_transaction(true)`` prior to calling ``purchase()``.
  86. These two methods ensure the transaction is completed.
  87. Parameters
  88. ^^^^^^^^^^
  89. Takes a dictionary as a parameter, with one field, ``product_id``, a
  90. string with your product ID. Example:
  91. ::
  92. var result = in_app_store.purchase({ "product_id": "my_product" })
  93. Response event
  94. ^^^^^^^^^^^^^^
  95. The response event will be a dictionary with the following fields:
  96. On error:
  97. ::
  98. {
  99. "type": "purchase",
  100. "result": "error",
  101. "product_id": "the product ID requested",
  102. }
  103. On success:
  104. ::
  105. {
  106. "type": "purchase",
  107. "result": "ok",
  108. "product_id": "the product ID requested",
  109. }
  110. ``request_product_info``
  111. ~~~~~~~~~~~~~~~~~~~~~~~~
  112. Requests the product info on a list of product IDs.
  113. Parameters
  114. ^^^^^^^^^^
  115. Takes a dictionary as a parameter, with a single ``product_ids`` key to which a
  116. string array of product IDs is assigned. Example:
  117. ::
  118. var result = in_app_store.request_product_info({ "product_ids": ["my_product1", "my_product2"] })
  119. Response event
  120. ^^^^^^^^^^^^^^
  121. The response event will be a dictionary with the following fields:
  122. ::
  123. {
  124. "type": "product_info",
  125. "result": "ok",
  126. "invalid_ids": [ list of requested IDs that were invalid ],
  127. "ids": [ list of IDs that were valid ],
  128. "titles": [ list of valid product titles (corresponds with list of valid IDs) ],
  129. "descriptions": [ list of valid product descriptions ],
  130. "prices": [ list of valid product prices ],
  131. "localized_prices": [ list of valid product localized prices ],
  132. }
  133. ``restore_purchases``
  134. ~~~~~~~~~~~~~~~~~~~~~
  135. Restores previously made purchases on user's account. This will create
  136. response events for each previously purchased product ID.
  137. Response event
  138. ^^^^^^^^^^^^^^
  139. The response events will be dictionaries with the following fields:
  140. ::
  141. {
  142. "type": "restore",
  143. "result": "ok",
  144. "product_id": "product ID of restored purchase",
  145. }
  146. ``set_auto_finish_transaction``
  147. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  148. If set to ``true``, once a purchase is successful, your purchase will be
  149. finalized automatically. Call this method prior to calling ``purchase()``.
  150. Parameters
  151. ^^^^^^^^^^
  152. Takes a boolean as a parameter which specifies if purchases should be
  153. automatically finalized. Example:
  154. ::
  155. in_app_store.set_auto_finish_transaction(true)
  156. ``finish_transaction``
  157. ~~~~~~~~~~~~~~~~~~~~~~
  158. If you don't want transactions to be automatically finalized, call this
  159. method after you receive a successful purchase response.
  160. Parameters
  161. ^^^^^^^^^^
  162. Takes a string ``product_id`` as an argument. ``product_id`` specifies what product to
  163. finalize the purchase on. Example:
  164. ::
  165. in_app_store.finish_transaction("my_product1")
  166. Game Center
  167. -----------
  168. Implemented in `Godot iOS GameCenter plugin <https://github.com/godotengine/godot-ios-plugins/blob/master/plugins/gamecenter/game_center.mm>`_.
  169. The Game Center API is available through the ``GameCenter`` singleton. It
  170. has the following methods:
  171. ::
  172. Error authenticate()
  173. bool is_authenticated()
  174. Error post_score(Variant score)
  175. Error award_achievement(Variant params)
  176. void reset_achievements()
  177. void request_achievements()
  178. void request_achievement_descriptions()
  179. Error show_game_center(Variant params)
  180. Error request_identity_verification_signature()
  181. and the pending events interface:
  182. ::
  183. int get_pending_event_count()
  184. Variant pop_pending_event()
  185. ``authenticate``
  186. ~~~~~~~~~~~~~~~~
  187. Authenticates a user in Game Center.
  188. Response event
  189. ^^^^^^^^^^^^^^
  190. The response event will be a dictionary with the following fields:
  191. On error:
  192. ::
  193. {
  194. "type": "authentication",
  195. "result": "error",
  196. "error_code": the value from NSError::code,
  197. "error_description": the value from NSError::localizedDescription,
  198. }
  199. On success:
  200. ::
  201. {
  202. "type": "authentication",
  203. "result": "ok",
  204. "player_id": the value from GKLocalPlayer::playerID,
  205. }
  206. ``post_score``
  207. ~~~~~~~~~~~~~~
  208. Posts a score to a Game Center leaderboard.
  209. Parameters
  210. ^^^^^^^^^^
  211. Takes a dictionary as a parameter, with two fields:
  212. - ``score`` a float number
  213. - ``category`` a string with the category name
  214. Example:
  215. ::
  216. var result = game_center.post_score({ "score": 100, "category": "my_leaderboard", })
  217. Response event
  218. ^^^^^^^^^^^^^^
  219. The response event will be a dictionary with the following fields:
  220. On error:
  221. ::
  222. {
  223. "type": "post_score",
  224. "result": "error",
  225. "error_code": the value from NSError::code,
  226. "error_description": the value from NSError::localizedDescription,
  227. }
  228. On success:
  229. ::
  230. {
  231. "type": "post_score",
  232. "result": "ok",
  233. }
  234. ``award_achievement``
  235. ~~~~~~~~~~~~~~~~~~~~~
  236. Modifies the progress of a Game Center achievement.
  237. Parameters
  238. ^^^^^^^^^^
  239. Takes a Dictionary as a parameter, with 3 fields:
  240. - ``name`` (string) the achievement name
  241. - ``progress`` (float) the achievement progress from 0.0 to 100.0
  242. (passed to ``GKAchievement::percentComplete``)
  243. - ``show_completion_banner`` (bool) whether Game Center should display
  244. an achievement banner at the top of the screen
  245. Example:
  246. ::
  247. var result = award_achievement({ "name": "hard_mode_completed", "progress": 6.1 })
  248. Response event
  249. ^^^^^^^^^^^^^^
  250. The response event will be a dictionary with the following fields:
  251. On error:
  252. ::
  253. {
  254. "type": "award_achievement",
  255. "result": "error",
  256. "error_code": the error code taken from NSError::code,
  257. }
  258. On success:
  259. ::
  260. {
  261. "type": "award_achievement",
  262. "result": "ok",
  263. }
  264. ``reset_achievements``
  265. ~~~~~~~~~~~~~~~~~~~~~~
  266. Clears all Game Center achievements. The function takes no parameters.
  267. Response event
  268. ^^^^^^^^^^^^^^
  269. The response event will be a dictionary with the following fields:
  270. On error:
  271. ::
  272. {
  273. "type": "reset_achievements",
  274. "result": "error",
  275. "error_code": the value from NSError::code,
  276. }
  277. On success:
  278. ::
  279. {
  280. "type": "reset_achievements",
  281. "result": "ok",
  282. }
  283. ``request_achievements``
  284. ~~~~~~~~~~~~~~~~~~~~~~~~
  285. Request all the Game Center achievements the player has made progress
  286. on. The function takes no parameters.
  287. Response event
  288. ^^^^^^^^^^^^^^
  289. The response event will be a dictionary with the following fields:
  290. On error:
  291. ::
  292. {
  293. "type": "achievements",
  294. "result": "error",
  295. "error_code": the value from NSError::code,
  296. }
  297. On success:
  298. ::
  299. {
  300. "type": "achievements",
  301. "result": "ok",
  302. "names": [ list of the name of each achievement ],
  303. "progress": [ list of the progress made on each achievement ],
  304. }
  305. ``request_achievement_descriptions``
  306. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  307. Request the descriptions of all existing Game Center achievements
  308. regardless of progress. The function takes no parameters.
  309. Response event
  310. ^^^^^^^^^^^^^^
  311. The response event will be a dictionary with the following fields:
  312. On error:
  313. ::
  314. {
  315. "type": "achievement_descriptions",
  316. "result": "error",
  317. "error_code": the value from NSError::code,
  318. }
  319. On success:
  320. ::
  321. {
  322. "type": "achievement_descriptions",
  323. "result": "ok",
  324. "names": [ list of the name of each achievement ],
  325. "titles": [ list of the title of each achievement ],
  326. "unachieved_descriptions": [ list of the description of each achievement when it is unachieved ],
  327. "achieved_descriptions": [ list of the description of each achievement when it is achieved ],
  328. "maximum_points": [ list of the points earned by completing each achievement ],
  329. "hidden": [ list of booleans indicating whether each achievement is initially visible ],
  330. "replayable": [ list of booleans indicating whether each achievement can be earned more than once ],
  331. }
  332. ``show_game_center``
  333. ~~~~~~~~~~~~~~~~~~~~
  334. Displays the built in Game Center overlay showing leaderboards,
  335. achievements, and challenges.
  336. Parameters
  337. ^^^^^^^^^^
  338. Takes a Dictionary as a parameter, with two fields:
  339. - ``view`` (string) (optional) the name of the view to present. Accepts
  340. "default", "leaderboards", "achievements", or "challenges". Defaults
  341. to "default".
  342. - ``leaderboard_name`` (string) (optional) the name of the leaderboard
  343. to present. Only used when "view" is "leaderboards" (or "default" is
  344. configured to show leaderboards). If not specified, Game Center will
  345. display the aggregate leaderboard.
  346. Examples:
  347. ::
  348. var result = show_game_center({ "view": "leaderboards", "leaderboard_name": "best_time_leaderboard" })
  349. var result = show_game_center({ "view": "achievements" })
  350. Response event
  351. ^^^^^^^^^^^^^^
  352. The response event will be a dictionary with the following fields:
  353. On close:
  354. ::
  355. {
  356. "type": "show_game_center",
  357. "result": "ok",
  358. }