plugins_for_ios.rst 12 KB

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