ios_plugin.rst 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. :article_outdated: True
  2. .. _doc_ios_plugin:
  3. Creating iOS plugins
  4. ====================
  5. This page explains what iOS plugins can do for you, how to use an existing plugin, and the steps to code a new one.
  6. iOS plugins allow you to use third-party libraries and support iOS-specific features like In-App Purchases, GameCenter integration, ARKit support, and more.
  7. Loading and using an existing plugin
  8. ------------------------------------
  9. An iOS plugin requires a ``.gdip`` configuration file, a binary file which can be either ``.a`` static library or ``.xcframework`` containing ``.a`` static libraries, and possibly other dependencies. To use it, you need to:
  10. 1. Copy the plugin's files to your Godot project's ``res://ios/plugins`` directory. You can also group files in a sub-directory, like ``res://ios/plugins/my_plugin``.
  11. 2. The Godot editor automatically detects and imports ``.gdip`` files inside ``res://ios/plugins`` and its subdirectories.
  12. 3. You can find and activate detected plugins by going to Project -> Export... -> iOS and in the Options tab, scrolling to the Plugins section.
  13. .. image:: img/ios_export_preset_plugins_section.png
  14. When a plugin is active, you can access it in your using ``Engine.get_singleton()``::
  15. if Engine.has_singleton("MyPlugin"):
  16. var singleton = Engine.get_singleton("MyPlugin")
  17. print(singleton.foo())
  18. .. note::
  19. The plugin's files have to be in the ``res://ios/plugins/`` directory or a subdirectory, otherwise the Godot editor will not automatically detect them.
  20. Creating an iOS plugin
  21. ----------------------
  22. At its core, a Godot iOS plugin is an iOS library (*.a* archive file or *.xcframework* containing static libraries) with the following requirements:
  23. - The library must have a dependency on the Godot engine headers.
  24. - The library must come with a ``.gdip`` configuration file.
  25. An iOS plugin can have the same functionality as a Godot module but provides more flexibility and doesn't require to rebuild the engine.
  26. Here are the steps to get a plugin's development started. We recommend using `Xcode <https://developer.apple.com/develop/>`_ as your development environment.
  27. .. seealso:: The `Godot iOS Plugins <https://github.com/godotengine/godot-ios-plugins>`_.
  28. The `Godot iOS plugin template <https://github.com/naithar/godot_ios_plugin>`_ gives you all the boilerplate you need to get your iOS plugin started.
  29. To build an iOS plugin:
  30. 1. Create an Objective-C static library for your plugin inside Xcode.
  31. 2. Add the Godot engine header files as a dependency for your plugin library in ``HEADER_SEARCH_PATHS``. You can find the setting inside the ``Build Settings`` tab:
  32. - Download the Godot engine source from the `Godot GitHub page <https://github.com/godotengine/godot>`_.
  33. - Run SCons to generate headers. You can learn the process by reading :ref:`doc_compiling_for_ios`. You don't have to wait for compilation to complete to move forward as headers are generated before the engine starts to compile.
  34. - You should use the same header files for iOS plugins and for the iOS export template.
  35. 3. In the ``Build Settings`` tab, specify the compilation flags for your static library in ``OTHER_CFLAGS``. The most important ones are ``-fcxx-modules``, ``-fmodules``, and ``-DDEBUG`` if you need debug support. Other flags should be the same you use to compile Godot. For instance:
  36. ::
  37. -DPTRCALL_ENABLED -DDEBUG_ENABLED -DDEBUG_MEMORY_ALLOC -DDISABLE_FORCED_INLINE -DTYPED_METHOD_BIND
  38. 4. Add the required logic for your plugin and build your library to generate a ``.a`` file. You will probably need to build both ``debug`` and ``release`` target ``.a`` files. Depending on your needs, pick either or both. If you need both debug and release ``.a`` files, their name should match following pattern: ``[PluginName].[TargetType].a``. You can also build the static library with your SCons configuration.
  39. 5. The iOS plugin system also supports ``.xcframework`` files. To generate one, you can use a command such as:
  40. ::
  41. xcodebuild -create-xcframework -library [DeviceLibrary].a -library [SimulatorLibrary].a -output [PluginName].xcframework
  42. 6. Create a Godot iOS Plugin configuration file to help the system detect and load your plugin:
  43. - The configuration file extension must be ``gdip`` (e.g.: ``MyPlugin.gdip``).
  44. - The configuration file format is as follow::
  45. [config]
  46. name="MyPlugin"
  47. binary="MyPlugin.a"
  48. initialization="init_my_plugin"
  49. deinitialization="deinit_my_plugin"
  50. [dependencies]
  51. linked=[]
  52. embedded=[]
  53. system=["Foundation.framework"]
  54. capabilities=["arkit", "metal"]
  55. files=["data.json"]
  56. linker_flags=["-ObjC"]
  57. [plist]
  58. PlistKeyWithDefaultType="Some Info.plist key you might need"
  59. StringPlistKey:string="String value"
  60. IntegerPlistKey:integer=42
  61. BooleanPlistKey:boolean=true
  62. RawPlistKey:raw="
  63. <array>
  64. <string>UIInterfaceOrientationPortrait</string>
  65. </array>
  66. "
  67. StringPlistKeyToInput:string_input="Type something"
  68. The ``config`` section and fields are required and defined as follow:
  69. - **name**: name of the plugin
  70. - **binary**: this should be the filepath of the plugin library (``a`` or ``xcframework``) file.
  71. - The filepath can be relative (e.g.: ``MyPlugin.a``, ``MyPlugin.xcframework``) in which case it's relative to the directory where the ``gdip`` file is located.
  72. - The filepath can be absolute: ``res://some_path/MyPlugin.a`` or ``res://some_path/MyPlugin.xcframework``.
  73. - In case you need multitarget library usage, the filename should be ``MyPlugin.a`` and ``.a`` files should be named as ``MyPlugin.release.a`` and ``MyPlugin.debug.a``.
  74. - In case you use multitarget ``xcframework`` libraries, their filename in the configuration should be ``MyPlugin.xcframework``. The ``.xcframework`` files should be named as ``MyPlugin.release.xcframework`` and ``MyPlugin.debug.xcframework``.
  75. The ``dependencies`` and ``plist`` sections are optional and defined as follow:
  76. - **dependencies**:
  77. - **linked**: contains a list of iOS frameworks that the iOS application should be linked with.
  78. - **embedded**: contains a list of iOS frameworks or libraries that should be both linked and embedded into the resulting iOS application.
  79. - **system**: contains a list of iOS system frameworks that are required for plugin.
  80. - **capabilities**: contains a list of iOS capabilities that is required for plugin. A list of available capabilities can be found at `Apple UIRequiredDeviceCapabilities documentation page <https://developer.apple.com/documentation/bundleresources/information_property_list/uirequireddevicecapabilities>`_.
  81. - **files**: contains a list of files that should be copied on export. This is useful for data files or images.
  82. - **linker_flags**: contains a list of linker flags to add to the Xcode project when exporting the plugin.
  83. - **plist**: should have keys and values that should be present in ``Info.plist`` file.
  84. - Each line should follow pattern: ``KeyName:KeyType=KeyValue``
  85. - Supported values for ``KeyType`` are ``string``, ``integer``, ``boolean``, ``raw``, ``string_input``
  86. - If no type is used (e.g.: ``KeyName="KeyValue"``) ``string`` type will be used.
  87. - If ``raw`` type is used value for corresponding key will be stored in ``Info.plist`` as is.
  88. - If ``string_input`` type is used you will be able to modify value in Export window.