BTAppSwitch.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. #import <UIKit/UIKit.h>
  2. @protocol BTAppSwitchHandler;
  3. NS_ASSUME_NONNULL_BEGIN
  4. #pragma mark - BTAppSwitch
  5. /**
  6. Handles return URLs when returning from app switch and routes the return URL to the correct app switch handler class.
  7. @note `returnURLScheme` must contain your app's registered URL Type that starts with the app's bundleID. When your app returns from app switch, the app delegate should call `handleOpenURL:sourceApplication:`
  8. */
  9. @interface BTAppSwitch : NSObject
  10. /**
  11. The URL scheme to return to this app after switching to another app.
  12. This URL scheme must be registered as a URL Type in the app's info.plist, and it must start with the app's bundle ID.
  13. */
  14. @property (nonatomic, copy) NSString *returnURLScheme;
  15. /**
  16. The singleton instance
  17. */
  18. + (instancetype)sharedInstance;
  19. /**
  20. Sets the return URL scheme for your app.
  21. This must be configured if your app integrates a payment option that may switch to either
  22. Mobile Safari or to another app to finish the payment authorization workflow.
  23. @param returnURLScheme The return URL scheme
  24. */
  25. + (void)setReturnURLScheme:(NSString *)returnURLScheme;
  26. /**
  27. Handles a return from app switch
  28. @param url The URL that was opened to return to your app
  29. @param sourceApplication The source app that requested the launch of your app
  30. @return `YES` if the app switch successfully handled the URL, or `NO` if the attempt to handle the URL failed.
  31. */
  32. + (BOOL)handleOpenURL:(NSURL *)url sourceApplication:(nullable NSString *)sourceApplication;
  33. /**
  34. Handles a return from app switch
  35. @param url The URL that was opened to return to your app
  36. @param options The options dictionary provided by `application:openURL:options:`
  37. @return `YES` if the app switch successfully handled the URL, or `NO` if the attempt to handle the URL failed.
  38. */
  39. + (BOOL)handleOpenURL:(NSURL *)url options:(NSDictionary *)options;
  40. /**
  41. Handles a return from app switch
  42. @param URLContext The URLContext provided by `scene:openURLContexts:`
  43. @return `YES` if the app switch successfully handled the URLContext, or `NO` if the attempt to handle the URLContext failed.
  44. */
  45. + (BOOL)API_AVAILABLE(ios(13.0))handleOpenURLContext:(UIOpenURLContext *)URLContext NS_SWIFT_NAME(handleOpenURLContext(_:));
  46. /**
  47. Registers a class that knows how to handle a return from app switch
  48. */
  49. - (void)registerAppSwitchHandler:(Class<BTAppSwitchHandler>)handler;
  50. /**
  51. Unregisters a class that knows how to handle a return from app switch
  52. */
  53. - (void)unregisterAppSwitchHandler:(Class<BTAppSwitchHandler>)handler;
  54. /**
  55. Handles a return from app switch
  56. @param url The URL that was opened to return to your app
  57. @param sourceApplication The string representing the source application
  58. @return `YES` if the app switch successfully handled the URL, or `NO` if the attempt to handle the URL failed.
  59. */
  60. - (BOOL)handleOpenURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication DEPRECATED_MSG_ATTRIBUTE("Use handleOpenURL:options: or handleOpenURLContext: instead.");
  61. @end
  62. #pragma mark - BTAppSwitchDelegate
  63. /**
  64. Notification that an app switch will occur
  65. */
  66. extern NSString * const BTAppSwitchWillSwitchNotification;
  67. /**
  68. Notification that an app switch did occur
  69. */
  70. extern NSString * const BTAppSwitchDidSwitchNotification;
  71. /**
  72. Notification that an app switch will process payment information
  73. */
  74. extern NSString * const BTAppSwitchWillProcessPaymentInfoNotification;
  75. /**
  76. Key for the target of an app switch notification
  77. @see BTAppSwitchTarget
  78. */
  79. extern NSString * const BTAppSwitchNotificationTargetKey;
  80. /**
  81. Notification that context will switch away from from the origin app
  82. */
  83. extern NSString * const BTAppContextWillSwitchNotification;
  84. /**
  85. Notification that the context switch has returned
  86. */
  87. extern NSString * const BTAppContextDidReturnNotification;
  88. /**
  89. Specifies the destination of an app switch
  90. */
  91. typedef NS_ENUM(NSInteger, BTAppSwitchTarget) {
  92. /// Unknown error
  93. BTAppSwitchTargetUnknown = 0,
  94. /// Native app
  95. BTAppSwitchTargetNativeApp,
  96. /// Browser (i.e. Mobile Safari)
  97. BTAppSwitchTargetWebBrowser,
  98. };
  99. /**
  100. Protocol for receiving payment lifecycle messages from a payment option that may initiate an app or browser switch event to authorize payments.
  101. */
  102. @protocol BTAppSwitchDelegate <NSObject>
  103. /**
  104. The app switcher will perform an app switch in order to obtain user payment authorization.
  105. Your implementation of this method may set your app to the state
  106. it should be in if the user manually app-switches back to your app.
  107. For example, re-enable any controls that are disabled.
  108. @note Use `appContextWillSwitch:` to receive events for all types of context switching from the origin app to apps/browsers.
  109. @param appSwitcher The app switcher
  110. */
  111. - (void)appSwitcherWillPerformAppSwitch:(id)appSwitcher;
  112. /**
  113. Delegates receive this message when the app switcher has successfully performed an app switch.
  114. You may use this hook to prepare your UI for app switch return. Keep in mind that
  115. users may manually switch back to your app via the iOS task manager.
  116. @note You may also hook into the app switch lifecycle via UIApplicationWillResignActiveNotification.
  117. @param appSwitcher The app switcher instance performing user authentication
  118. @param target The destination that was actually used for this app switch
  119. */
  120. - (void)appSwitcher:(id)appSwitcher didPerformSwitchToTarget:(BTAppSwitchTarget)target;
  121. /**
  122. The app switcher has obtained user payment details and/or user authorization and will process the results.
  123. This typically indicates asynchronous network activity.
  124. When you receive this message, your UI should indicate activity.
  125. In the case of an app switch, this message indicates that the user has returned to this app;
  126. this is usually after handleAppSwitchReturnURL: is called in your UIApplicationDelegate.
  127. You may also hook into the app switch lifecycle via UIApplicationWillResignActiveNotification.
  128. @note Use `appContextDidReturn:` to receive events for all types of context switching from apps/browsers back to the origin app.
  129. @param appSwitcher The app switcher
  130. */
  131. - (void)appSwitcherWillProcessPaymentInfo:(id)appSwitcher;
  132. @optional
  133. /**
  134. Regardless of the method (e.g. app, Safari, SFSafariViewController, SFAuthenticationSession) events will be sent when the context will switch away from from the origin app.
  135. @note Use this method to update UI (e.g. displaying a loading indicator) before the switch takes place.
  136. @param appSwitcher The app switcher
  137. */
  138. - (void)appContextWillSwitch:(id)appSwitcher;
  139. /**
  140. The context switch has returned.
  141. @note This is not guaranteed to be called. Example: A user leaves an app or Safari switch and manually returns to the origin app.
  142. @param appSwitcher The app switcher
  143. */
  144. - (void)appContextDidReturn:(id)appSwitcher;
  145. @end
  146. #pragma mark - BTAppSwitchHandler protocol
  147. /**
  148. A protocol for handling the return from switching out of an app to gather payment information.
  149. @note The app may switch out to Mobile Safari or to a native app.
  150. */
  151. @protocol BTAppSwitchHandler
  152. @required
  153. /**
  154. Determine whether the app switch return URL can be handled.
  155. @param url the URL you receive in `application:openURL:sourceApplication:annotation` when returning to your app
  156. @param sourceApplication The source application you receive in `application:openURL:sourceApplication:annotation`
  157. @return `YES` when the object can handle returning from the application with a URL
  158. */
  159. + (BOOL)canHandleAppSwitchReturnURL:(NSURL *)url sourceApplication:(NSString * _Nullable)sourceApplication;
  160. /**
  161. Pass control back to `BTPayPalDriver` after returning from app or browser switch.
  162. @param url The URL you receive in `application:openURL:sourceApplication:annotation`
  163. */
  164. + (void)handleAppSwitchReturnURL:(NSURL *)url;
  165. @optional
  166. /**
  167. Indicates whether an iOS app is installed and available for app switch.
  168. */
  169. - (BOOL)isiOSAppAvailableForAppSwitch;
  170. @end
  171. NS_ASSUME_NONNULL_END