BTAppSwitch.m 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. #import "BTAppSwitch.h"
  2. #import <UIKit/UIKit.h>
  3. NSString * const BTAppSwitchWillSwitchNotification = @"com.braintreepayments.BTAppSwitchWillSwitchNotification";
  4. NSString * const BTAppSwitchDidSwitchNotification = @"com.braintreepayments.BTAppSwitchDidSwitchNotification";
  5. NSString * const BTAppSwitchWillProcessPaymentInfoNotification = @"com.braintreepayments.BTAppSwitchWillProcessPaymentInfoNotification";
  6. NSString * const BTAppSwitchNotificationTargetKey = @"BTAppSwitchNotificationTargetKey";
  7. NSString * const BTAppContextWillSwitchNotification = @"com.braintreepayments.BTAppContextWillSwitchNotification";
  8. NSString * const BTAppContextDidReturnNotification = @"com.braintreepayments.BTAppContextDidReturnNotification";
  9. @interface BTAppSwitch ()
  10. @property (nonatomic, strong) NSMutableSet *appSwitchHandlers;
  11. @end
  12. @implementation BTAppSwitch
  13. + (instancetype)sharedInstance {
  14. static BTAppSwitch *instance;
  15. static dispatch_once_t onceToken;
  16. dispatch_once(&onceToken, ^{
  17. instance = [[BTAppSwitch alloc] init];
  18. });
  19. return instance;
  20. }
  21. - (instancetype)init {
  22. self = [super init];
  23. if (self) {
  24. _appSwitchHandlers = [NSMutableSet set];
  25. }
  26. return self;
  27. }
  28. + (void)setReturnURLScheme:(NSString *)returnURLScheme {
  29. [BTAppSwitch sharedInstance].returnURLScheme = returnURLScheme;
  30. }
  31. + (BOOL)handleOpenURL:(NSURL *)url options:(NSDictionary *)options {
  32. #pragma clang diagnostic push
  33. #pragma clang diagnostic ignored "-Wdeprecated-declarations"
  34. if (@available(iOS 9.0, *)) {
  35. return [[BTAppSwitch sharedInstance] handleOpenURL:url sourceApplication:options[UIApplicationOpenURLOptionsSourceApplicationKey]];
  36. } else {
  37. return [[BTAppSwitch sharedInstance] handleOpenURL:url sourceApplication:@""];
  38. }
  39. #pragma clang diagnostic pop
  40. }
  41. + (BOOL)handleOpenURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication {
  42. #pragma clang diagnostic push
  43. #pragma clang diagnostic ignored "-Wdeprecated-declarations"
  44. return [[BTAppSwitch sharedInstance] handleOpenURL:url sourceApplication:sourceApplication];
  45. #pragma clang diagnostic pop
  46. }
  47. + (BOOL)handleOpenURLContext:(UIOpenURLContext *)URLContext API_AVAILABLE(ios(13.0)) {
  48. #pragma clang diagnostic push
  49. #pragma clang diagnostic ignored "-Wdeprecated-declarations"
  50. return [[BTAppSwitch sharedInstance] handleOpenURL:URLContext.URL sourceApplication:URLContext.options.sourceApplication];
  51. #pragma clang diagnostic pop
  52. }
  53. // NEXT_MAJOR_VERSION Remove this method from public header, but continue using it internally.
  54. // Once removed, delete the code to ignore deprecation warnings (above).
  55. - (BOOL)handleOpenURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication {
  56. for (Class<BTAppSwitchHandler> handlerClass in self.appSwitchHandlers) {
  57. if ([handlerClass canHandleAppSwitchReturnURL:url sourceApplication:sourceApplication]) {
  58. [handlerClass handleAppSwitchReturnURL:url];
  59. return YES;
  60. }
  61. }
  62. return NO;
  63. }
  64. -(void)registerAppSwitchHandler:(Class<BTAppSwitchHandler>)handler {
  65. if (!handler) return;
  66. [self.appSwitchHandlers addObject:handler];
  67. }
  68. - (void)unregisterAppSwitchHandler:(Class<BTAppSwitchHandler>)handler {
  69. [self.appSwitchHandlers removeObject:handler];
  70. }
  71. @end