plugins_for_ios.rst 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  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 p_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. - ``Error purchase(Variant p_params);``
  66. - ``Error request_product_info(Variant p_params);``
  67. - ``Error restore_purchases();``
  68. and the pending_event interface
  69. ::
  70. int get_pending_event_count();
  71. Variant pop_pending_event();
  72. purchase
  73. ~~~~~~~~
  74. Purchases a product id through the Store Kit API.
  75. Parameters
  76. ^^^^^^^^^^
  77. Takes a Dictionary as a parameter, with one field, ``product_id``, a
  78. string with your product id. Example:
  79. ::
  80. var result = InAppStore.purchase( { "product_id": "my_product" } )
  81. Response event
  82. ^^^^^^^^^^^^^^
  83. The response event will be a dictionary with the following fields:
  84. On error:
  85. ::
  86. {
  87. "type": "purchase",
  88. "result": "error",
  89. "product_id": "the product id requested"
  90. }
  91. On success:
  92. ::
  93. {
  94. "type": "purchase",
  95. "result": "ok",
  96. "product_id": "the product id requested"
  97. }
  98. request_product_info
  99. ~~~~~~~~~~~~~~~~~~~~
  100. Requests the product info on a list of product IDs.
  101. Parameters
  102. ^^^^^^^^^^
  103. Takes a Dictionary as a parameter, with one field, ``product_ids``, a
  104. string array with a list of product ids. Example:
  105. ::
  106. var result = InAppStore.request_product_info( { "product_ids": ["my_product1", "my_product2"] } )
  107. Response event
  108. ^^^^^^^^^^^^^^
  109. The response event will be a dictionary with the following fields:
  110. ::
  111. {
  112. "type": "product_info",
  113. "result": "ok",
  114. "invalid_ids": [ list of requested ids that were invalid ],
  115. "ids": [ list of ids that were valid ],
  116. "titles": [ list of valid product titles (corresponds with list of valid ids) ],
  117. "descriptions": [ list of valid product descriptions ] ,
  118. "prices": [ list of valid product prices ],
  119. "localized_prices": [ list of valid product localized prices ],
  120. }
  121. restore_purchases
  122. ~~~~~~~~~~~~~~~~~
  123. Restores previously made purchases on user's account. This will create
  124. response events for each previously purchased product id.
  125. Response event
  126. ^^^^^^^^^^^^^^
  127. The response events will be dictionaries with the following fields:
  128. ::
  129. {
  130. "type": "restore",
  131. "result": "ok",
  132. "product id": "product id of restored purchase"
  133. }
  134. Game Center
  135. -----------
  136. Implemented in `Godot iOS GameCenter plugin <https://github.com/godotengine/godot-ios-plugins/blob/master/plugins/gamecenter/game_center.mm>`_.
  137. The Game Center API is available through the ``GameCenter`` singleton. It
  138. has the following methods:
  139. - ``Error authenticate();``
  140. - ``bool is_authenticated();``
  141. - ``Error post_score(Variant p_score);``
  142. - ``Error award_achievement(Variant p_params);``
  143. - ``void reset_achievements();``
  144. - ``void request_achievements();``
  145. - ``void request_achievement_descriptions();``
  146. - ``Error show_game_center(Variant p_params);``
  147. - ``Error request_identity_verification_signature();``
  148. plus the standard pending event interface.
  149. authenticate
  150. ~~~~~~~~~~~~
  151. Authenticates a user in Game Center.
  152. Response event
  153. ^^^^^^^^^^^^^^
  154. The response event will be a dictionary with the following fields:
  155. On error:
  156. ::
  157. {
  158. "type": "authentication",
  159. "result": "error",
  160. "error_code": the value from NSError::code,
  161. "error_description": the value from NSError::localizedDescription,
  162. }
  163. On success:
  164. ::
  165. {
  166. "type": "authentication",
  167. "result": "ok",
  168. "player_id": the value from GKLocalPlayer::playerID,
  169. }
  170. post_score
  171. ~~~~~~~~~~
  172. Posts a score to a Game Center leaderboard.
  173. Parameters
  174. ^^^^^^^^^^
  175. Takes a Dictionary as a parameter, with two fields:
  176. - ``score`` a float number
  177. - ``category`` a string with the category name
  178. Example:
  179. ::
  180. var result = GameCenter.post_score( { "score": 100, "category": "my_leaderboard", } )
  181. Response event
  182. ^^^^^^^^^^^^^^
  183. The response event will be a dictionary with the following fields:
  184. On error:
  185. ::
  186. {
  187. "type": "post_score",
  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": "post_score",
  196. "result": "ok",
  197. }
  198. award_achievement
  199. ~~~~~~~~~~~~~~~~~
  200. Modifies the progress of a Game Center achievement.
  201. Parameters
  202. ^^^^^^^^^^
  203. Takes a Dictionary as a parameter, with 3 fields:
  204. - ``name`` (string) the achievement name
  205. - ``progress`` (float) the achievement progress from 0.0 to 100.0
  206. (passed to ``GKAchievement::percentComplete``)
  207. - ``show_completion_banner`` (bool) whether Game Center should display
  208. an achievement banner at the top of the screen
  209. Example:
  210. ::
  211. var result = award_achievement( { "name": "hard_mode_completed", "progress": 6.1 } )
  212. Response event
  213. ^^^^^^^^^^^^^^
  214. The response event will be a dictionary with the following fields:
  215. On error:
  216. ::
  217. {
  218. "type": "award_achievement",
  219. "result": "error",
  220. "error_code": the error code taken from NSError::code,
  221. }
  222. On success:
  223. ::
  224. {
  225. "type": "award_achievement",
  226. "result": "ok",
  227. }
  228. reset_achievements
  229. ~~~~~~~~~~~~~~~~~~
  230. Clears all Game Center achievements. The function takes no parameters.
  231. Response event
  232. ^^^^^^^^^^^^^^
  233. The response event will be a dictionary with the following fields:
  234. On error:
  235. ::
  236. {
  237. "type": "reset_achievements",
  238. "result": "error",
  239. "error_code": the value from NSError::code
  240. }
  241. On success:
  242. ::
  243. {
  244. "type": "reset_achievements",
  245. "result": "ok",
  246. }
  247. request_achievements
  248. ~~~~~~~~~~~~~~~~~~~~
  249. Request all the Game Center achievements the player has made progress
  250. on. The function takes no parameters.
  251. Response event
  252. ^^^^^^^^^^^^^^
  253. The response event will be a dictionary with the following fields:
  254. On error:
  255. ::
  256. {
  257. "type": "achievements",
  258. "result": "error",
  259. "error_code": the value from NSError::code
  260. }
  261. On success:
  262. ::
  263. {
  264. "type": "achievements",
  265. "result": "ok",
  266. "names": [ list of the name of each achievement ],
  267. "progress": [ list of the progress made on each achievement ]
  268. }
  269. request_achievement_descriptions
  270. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  271. Request the descriptions of all existing Game Center achievements
  272. regardless of progress. The function takes no parameters.
  273. Response event
  274. ^^^^^^^^^^^^^^
  275. The response event will be a dictionary with the following fields:
  276. On error:
  277. ::
  278. {
  279. "type": "achievement_descriptions",
  280. "result": "error",
  281. "error_code": the value from NSError::code
  282. }
  283. On success:
  284. ::
  285. {
  286. "type": "achievement_descriptions",
  287. "result": "ok",
  288. "names": [ list of the name of each achievement ],
  289. "titles": [ list of the title of each achievement ]
  290. "unachieved_descriptions": [ list of the description of each achievement when it is unachieved ]
  291. "achieved_descriptions": [ list of the description of each achievement when it is achieved ]
  292. "maximum_points": [ list of the points earned by completing each achievement ]
  293. "hidden": [ list of booleans indicating whether each achievement is initially visible ]
  294. "replayable": [ list of booleans indicating whether each achievement can be earned more than once ]
  295. }
  296. show_game_center
  297. ~~~~~~~~~~~~~~~~
  298. Displays the built in Game Center overlay showing leaderboards,
  299. achievements, and challenges.
  300. Parameters
  301. ^^^^^^^^^^
  302. Takes a Dictionary as a parameter, with two fields:
  303. - ``view`` (string) (optional) the name of the view to present. Accepts
  304. "default", "leaderboards", "achievements", or "challenges". Defaults
  305. to "default".
  306. - ``leaderboard_name`` (string) (optional) the name of the leaderboard
  307. to present. Only used when "view" is "leaderboards" (or "default" is
  308. configured to show leaderboards). If not specified, Game Center will
  309. display the aggregate leaderboard.
  310. Examples:
  311. ::
  312. var result = show_game_center( { "view": "leaderboards", "leaderboard_name": "best_time_leaderboard" } )
  313. var result = show_game_center( { "view": "achievements" } )
  314. Response event
  315. ^^^^^^^^^^^^^^
  316. The response event will be a dictionary with the following fields:
  317. On close:
  318. ::
  319. {
  320. "type": "show_game_center",
  321. "result": "ok",
  322. }