FlutterPlugin.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  1. // Copyright 2013 The Flutter Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef FLUTTER_FLUTTERPLUGIN_H_
  5. #define FLUTTER_FLUTTERPLUGIN_H_
  6. #import <UIKit/UIKit.h>
  7. #import <UserNotifications/UNUserNotificationCenter.h>
  8. #include "FlutterBinaryMessenger.h"
  9. #include "FlutterChannels.h"
  10. #include "FlutterCodecs.h"
  11. #include "FlutterPlatformViews.h"
  12. #include "FlutterTexture.h"
  13. NS_ASSUME_NONNULL_BEGIN
  14. @protocol FlutterPluginRegistrar;
  15. @protocol FlutterPluginRegistry;
  16. #pragma mark -
  17. /**
  18. * Protocol for listener of events from the UIApplication, typically a FlutterPlugin.
  19. */
  20. @protocol FlutterApplicationLifeCycleDelegate
  21. #if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_10_0
  22. <UNUserNotificationCenterDelegate>
  23. #endif
  24. @optional
  25. /**
  26. * Called if this has been registered for `UIApplicationDelegate` callbacks.
  27. *
  28. * @return `NO` if this vetoes application launch.
  29. */
  30. - (BOOL)application:(UIApplication*)application
  31. didFinishLaunchingWithOptions:(NSDictionary*)launchOptions;
  32. /**
  33. * Called if this has been registered for `UIApplicationDelegate` callbacks.
  34. *
  35. * @return `NO` if this vetoes application launch.
  36. */
  37. - (BOOL)application:(UIApplication*)application
  38. willFinishLaunchingWithOptions:(NSDictionary*)launchOptions;
  39. /**
  40. * Called if this has been registered for `UIApplicationDelegate` callbacks.
  41. */
  42. - (void)applicationDidBecomeActive:(UIApplication*)application;
  43. /**
  44. * Called if this has been registered for `UIApplicationDelegate` callbacks.
  45. */
  46. - (void)applicationWillResignActive:(UIApplication*)application;
  47. /**
  48. * Called if this has been registered for `UIApplicationDelegate` callbacks.
  49. */
  50. - (void)applicationDidEnterBackground:(UIApplication*)application;
  51. /**
  52. * Called if this has been registered for `UIApplicationDelegate` callbacks.
  53. */
  54. - (void)applicationWillEnterForeground:(UIApplication*)application;
  55. /**
  56. Called if this has been registered for `UIApplicationDelegate` callbacks.
  57. */
  58. - (void)applicationWillTerminate:(UIApplication*)application;
  59. /**
  60. * Called if this has been registered for `UIApplicationDelegate` callbacks.
  61. */
  62. - (void)application:(UIApplication*)application
  63. didRegisterUserNotificationSettings:(UIUserNotificationSettings*)notificationSettings
  64. API_DEPRECATED(
  65. "See -[UIApplicationDelegate application:didRegisterUserNotificationSettings:] deprecation",
  66. ios(8.0, 10.0));
  67. /**
  68. * Called if this has been registered for `UIApplicationDelegate` callbacks.
  69. */
  70. - (void)application:(UIApplication*)application
  71. didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken;
  72. /**
  73. * Called if this has been registered for `UIApplicationDelegate` callbacks.
  74. *
  75. * @return `YES` if this handles the request.
  76. */
  77. - (BOOL)application:(UIApplication*)application
  78. didReceiveRemoteNotification:(NSDictionary*)userInfo
  79. fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))completionHandler;
  80. /**
  81. * Calls all plugins registered for `UIApplicationDelegate` callbacks.
  82. */
  83. - (void)application:(UIApplication*)application
  84. didReceiveLocalNotification:(UILocalNotification*)notification
  85. API_DEPRECATED(
  86. "See -[UIApplicationDelegate application:didReceiveLocalNotification:] deprecation",
  87. ios(4.0, 10.0));
  88. /**
  89. * Called if this has been registered for `UIApplicationDelegate` callbacks.
  90. *
  91. * @return `YES` if this handles the request.
  92. */
  93. - (BOOL)application:(UIApplication*)application
  94. openURL:(NSURL*)url
  95. options:(NSDictionary<UIApplicationOpenURLOptionsKey, id>*)options;
  96. /**
  97. * Called if this has been registered for `UIApplicationDelegate` callbacks.
  98. *
  99. * @return `YES` if this handles the request.
  100. */
  101. - (BOOL)application:(UIApplication*)application handleOpenURL:(NSURL*)url;
  102. /**
  103. * Called if this has been registered for `UIApplicationDelegate` callbacks.
  104. *
  105. * @return `YES` if this handles the request.
  106. */
  107. - (BOOL)application:(UIApplication*)application
  108. openURL:(NSURL*)url
  109. sourceApplication:(NSString*)sourceApplication
  110. annotation:(id)annotation;
  111. /**
  112. * Called if this has been registered for `UIApplicationDelegate` callbacks.
  113. *
  114. * @return `YES` if this handles the request.
  115. */
  116. - (BOOL)application:(UIApplication*)application
  117. performActionForShortcutItem:(UIApplicationShortcutItem*)shortcutItem
  118. completionHandler:(void (^)(BOOL succeeded))completionHandler
  119. API_AVAILABLE(ios(9.0));
  120. /**
  121. * Called if this has been registered for `UIApplicationDelegate` callbacks.
  122. *
  123. * @return `YES` if this handles the request.
  124. */
  125. - (BOOL)application:(UIApplication*)application
  126. handleEventsForBackgroundURLSession:(nonnull NSString*)identifier
  127. completionHandler:(nonnull void (^)(void))completionHandler;
  128. /**
  129. * Called if this has been registered for `UIApplicationDelegate` callbacks.
  130. *
  131. * @return `YES` if this handles the request.
  132. */
  133. - (BOOL)application:(UIApplication*)application
  134. performFetchWithCompletionHandler:(void (^)(UIBackgroundFetchResult result))completionHandler;
  135. /**
  136. * Called if this has been registered for `UIApplicationDelegate` callbacks.
  137. *
  138. * @return `YES` if this handles the request.
  139. */
  140. - (BOOL)application:(UIApplication*)application
  141. continueUserActivity:(NSUserActivity*)userActivity
  142. restorationHandler:(void (^)(NSArray*))restorationHandler;
  143. @end
  144. #pragma mark -
  145. /**
  146. * A plugin registration callback.
  147. *
  148. * Used for registering plugins with additional instances of
  149. * `FlutterPluginRegistry`.
  150. *
  151. * @param registry The registry to register plugins with.
  152. */
  153. typedef void (*FlutterPluginRegistrantCallback)(NSObject<FlutterPluginRegistry>* registry);
  154. #pragma mark -
  155. /**
  156. * Implemented by the iOS part of a Flutter plugin.
  157. *
  158. * Defines a set of optional callback methods and a method to set up the plugin
  159. * and register it to be called by other application components.
  160. */
  161. @protocol FlutterPlugin <NSObject, FlutterApplicationLifeCycleDelegate>
  162. @required
  163. /**
  164. * Registers this plugin using the context information and callback registration
  165. * methods exposed by the given registrar.
  166. *
  167. * The registrar is obtained from a `FlutterPluginRegistry` which keeps track of
  168. * the identity of registered plugins and provides basic support for cross-plugin
  169. * coordination.
  170. *
  171. * The caller of this method, a plugin registrant, is usually autogenerated by
  172. * Flutter tooling based on declared plugin dependencies. The generated registrant
  173. * asks the registry for a registrar for each plugin, and calls this method to
  174. * allow the plugin to initialize itself and register callbacks with application
  175. * objects available through the registrar protocol.
  176. *
  177. * @param registrar A helper providing application context and methods for
  178. * registering callbacks.
  179. */
  180. + (void)registerWithRegistrar:(NSObject<FlutterPluginRegistrar>*)registrar;
  181. @optional
  182. /**
  183. * Set a callback for registering plugins to an additional `FlutterPluginRegistry`,
  184. * including headless `FlutterEngine` instances.
  185. *
  186. * This method is typically called from within an application's `AppDelegate` at
  187. * startup to allow for plugins which create additional `FlutterEngine` instances
  188. * to register the application's plugins.
  189. *
  190. * @param callback A callback for registering some set of plugins with a
  191. * `FlutterPluginRegistry`.
  192. */
  193. + (void)setPluginRegistrantCallback:(FlutterPluginRegistrantCallback)callback;
  194. @optional
  195. /**
  196. * Called if this plugin has been registered to receive `FlutterMethodCall`s.
  197. *
  198. * @param call The method call command object.
  199. * @param result A callback for submitting the result of the call.
  200. */
  201. - (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result;
  202. @optional
  203. /**
  204. * Called when a plugin is being removed from a `FlutterEngine`, which is
  205. * usually the result of the `FlutterEngine` being deallocated. This method
  206. * provides the opportunity to do necessary cleanup.
  207. *
  208. * You will only receive this method if you registered your plugin instance with
  209. * the `FlutterEngine` via `-[FlutterPluginRegistry publish:]`.
  210. *
  211. * @param registrar The registrar that was used to publish the plugin.
  212. *
  213. */
  214. - (void)detachFromEngineForRegistrar:(NSObject<FlutterPluginRegistrar>*)registrar;
  215. @end
  216. #pragma mark -
  217. /**
  218. * How the UIGestureRecognizers of a platform view are blocked.
  219. *
  220. * UIGestureRecognizers of platform views can be blocked based on decisions made by the
  221. * Flutter Framework (e.g. When an interact-able widget is covering the platform view).
  222. */
  223. typedef enum {
  224. /**
  225. * Flutter blocks all the UIGestureRecognizers on the platform view as soon as it
  226. * decides they should be blocked.
  227. *
  228. * With this policy, only the `touchesBegan` method for all the UIGestureRecognizers is guaranteed
  229. * to be called.
  230. */
  231. FlutterPlatformViewGestureRecognizersBlockingPolicyEager,
  232. /**
  233. * Flutter blocks the platform view's UIGestureRecognizers from recognizing only after
  234. * touchesEnded was invoked.
  235. *
  236. * This results in the platform view's UIGestureRecognizers seeing the entire touch sequence,
  237. * but never recognizing the gesture (and never invoking actions).
  238. */
  239. FlutterPlatformViewGestureRecognizersBlockingPolicyWaitUntilTouchesEnded,
  240. } FlutterPlatformViewGestureRecognizersBlockingPolicy;
  241. #pragma mark -
  242. /**
  243. * Registration context for a single `FlutterPlugin`, providing a one stop shop
  244. * for the plugin to access contextual information and register callbacks for
  245. * various application events.
  246. *
  247. * Registrars are obtained from a `FlutterPluginRegistry` which keeps track of
  248. * the identity of registered plugins and provides basic support for cross-plugin
  249. * coordination.
  250. */
  251. @protocol FlutterPluginRegistrar <NSObject>
  252. /**
  253. * Returns a `FlutterBinaryMessenger` for creating Dart/iOS communication
  254. * channels to be used by the plugin.
  255. *
  256. * @return The messenger.
  257. */
  258. - (NSObject<FlutterBinaryMessenger>*)messenger;
  259. /**
  260. * Returns a `FlutterTextureRegistry` for registering textures
  261. * provided by the plugin.
  262. *
  263. * @return The texture registry.
  264. */
  265. - (NSObject<FlutterTextureRegistry>*)textures;
  266. /**
  267. * Registers a `FlutterPlatformViewFactory` for creation of platform views.
  268. *
  269. * Plugins expose `UIView` for embedding in Flutter apps by registering a view factory.
  270. *
  271. * @param factory The view factory that will be registered.
  272. * @param factoryId A unique identifier for the factory, the Dart code of the Flutter app can use
  273. * this identifier to request creation of a `UIView` by the registered factory.
  274. */
  275. - (void)registerViewFactory:(NSObject<FlutterPlatformViewFactory>*)factory
  276. withId:(NSString*)factoryId;
  277. /**
  278. * Registers a `FlutterPlatformViewFactory` for creation of platform views.
  279. *
  280. * Plugins can expose a `UIView` for embedding in Flutter apps by registering a view factory.
  281. *
  282. * @param factory The view factory that will be registered.
  283. * @param factoryId A unique identifier for the factory, the Dart code of the Flutter app can use
  284. * this identifier to request creation of a `UIView` by the registered factory.
  285. * @param gestureRecognizersBlockingPolicy How UIGestureRecognizers on the platform views are
  286. * blocked.
  287. *
  288. */
  289. - (void)registerViewFactory:(NSObject<FlutterPlatformViewFactory>*)factory
  290. withId:(NSString*)factoryId
  291. gestureRecognizersBlockingPolicy:
  292. (FlutterPlatformViewGestureRecognizersBlockingPolicy)gestureRecognizersBlockingPolicy;
  293. /**
  294. * Publishes a value for external use of the plugin.
  295. *
  296. * Plugins may publish a single value, such as an instance of the
  297. * plugin's main class, for situations where external control or
  298. * interaction is needed.
  299. *
  300. * The published value will be available from the `FlutterPluginRegistry`.
  301. * Repeated calls overwrite any previous publication.
  302. *
  303. * @param value The value to be published.
  304. */
  305. - (void)publish:(NSObject*)value;
  306. /**
  307. * Registers the plugin as a receiver of incoming method calls from the Dart side
  308. * on the specified `FlutterMethodChannel`.
  309. *
  310. * @param delegate The receiving object, such as the plugin's main class.
  311. * @param channel The channel
  312. */
  313. - (void)addMethodCallDelegate:(NSObject<FlutterPlugin>*)delegate
  314. channel:(FlutterMethodChannel*)channel;
  315. /**
  316. * Registers the plugin as a receiver of `UIApplicationDelegate` calls.
  317. *
  318. * @param delegate The receiving object, such as the plugin's main class.
  319. */
  320. - (void)addApplicationDelegate:(NSObject<FlutterPlugin>*)delegate;
  321. /**
  322. * Returns the file name for the given asset.
  323. * The returned file name can be used to access the asset in the application's main bundle.
  324. *
  325. * @param asset The name of the asset. The name can be hierarchical.
  326. * @return the file name to be used for lookup in the main bundle.
  327. */
  328. - (NSString*)lookupKeyForAsset:(NSString*)asset;
  329. /**
  330. * Returns the file name for the given asset which originates from the specified package.
  331. * The returned file name can be used to access the asset in the application's main bundle.
  332. *
  333. *
  334. * @param asset The name of the asset. The name can be hierarchical.
  335. * @param package The name of the package from which the asset originates.
  336. * @return the file name to be used for lookup in the main bundle.
  337. */
  338. - (NSString*)lookupKeyForAsset:(NSString*)asset fromPackage:(NSString*)package;
  339. @end
  340. #pragma mark -
  341. /**
  342. * A registry of Flutter iOS plugins.
  343. *
  344. * Plugins are identified by unique string keys, typically the name of the
  345. * plugin's main class. The registry tracks plugins by this key, mapping it to
  346. * a value published by the plugin during registration, if any. This provides a
  347. * very basic means of cross-plugin coordination with loose coupling between
  348. * unrelated plugins.
  349. *
  350. * Plugins typically need contextual information and the ability to register
  351. * callbacks for various application events. To keep the API of the registry
  352. * focused, these facilities are not provided directly by the registry, but by
  353. * a `FlutterPluginRegistrar`, created by the registry in exchange for the unique
  354. * key of the plugin.
  355. *
  356. * There is no implied connection between the registry and the registrar.
  357. * Specifically, callbacks registered by the plugin via the registrar may be
  358. * relayed directly to the underlying iOS application objects.
  359. */
  360. @protocol FlutterPluginRegistry <NSObject>
  361. /**
  362. * Returns a registrar for registering a plugin.
  363. *
  364. * @param pluginKey The unique key identifying the plugin.
  365. */
  366. - (nullable NSObject<FlutterPluginRegistrar>*)registrarForPlugin:(NSString*)pluginKey;
  367. /**
  368. * Returns whether the specified plugin has been registered.
  369. *
  370. * @param pluginKey The unique key identifying the plugin.
  371. * @return `YES` if `registrarForPlugin` has been called with `pluginKey`.
  372. */
  373. - (BOOL)hasPlugin:(NSString*)pluginKey;
  374. /**
  375. * Returns a value published by the specified plugin.
  376. *
  377. * @param pluginKey The unique key identifying the plugin.
  378. * @return An object published by the plugin, if any. Will be `NSNull` if
  379. * nothing has been published. Will be `nil` if the plugin has not been
  380. * registered.
  381. */
  382. - (NSObject*)valuePublishedByPlugin:(NSString*)pluginKey;
  383. @end
  384. #pragma mark -
  385. /**
  386. * Implement this in the `UIAppDelegate` of your app to enable Flutter plugins to register
  387. * themselves to the application life cycle events.
  388. *
  389. * For plugins to receive events from `UNUserNotificationCenter`, register this as the
  390. * `UNUserNotificationCenterDelegate`.
  391. */
  392. @protocol FlutterAppLifeCycleProvider
  393. #if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_10_0
  394. <UNUserNotificationCenterDelegate>
  395. #endif
  396. /**
  397. * Called when registering a new `FlutterApplicaitonLifeCycleDelegate`.
  398. *
  399. * See also: `-[FlutterAppDelegate addApplicationLifeCycleDelegate:]`
  400. */
  401. - (void)addApplicationLifeCycleDelegate:(NSObject<FlutterApplicationLifeCycleDelegate>*)delegate;
  402. @end
  403. NS_ASSUME_NONNULL_END;
  404. #endif // FLUTTER_FLUTTERPLUGIN_H_