plugins_for_ios.rst 12 KB

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