BTAppContextSwitcher.m 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. #if __has_include(<Braintree/BraintreeCore.h>)
  2. #import <Braintree/BTAppContextSwitcher.h>
  3. #else
  4. #import <BraintreeCore/BTAppContextSwitcher.h>
  5. #endif
  6. #import <UIKit/UIKit.h>
  7. @interface BTAppContextSwitcher ()
  8. @property (nonatomic, strong) NSMutableSet *appContextSwitchDrivers;
  9. @end
  10. @implementation BTAppContextSwitcher
  11. + (instancetype)sharedInstance {
  12. static BTAppContextSwitcher *instance;
  13. static dispatch_once_t onceToken;
  14. dispatch_once(&onceToken, ^{
  15. instance = [[BTAppContextSwitcher alloc] init];
  16. });
  17. return instance;
  18. }
  19. - (instancetype)init {
  20. self = [super init];
  21. if (self) {
  22. _appContextSwitchDrivers = [NSMutableSet set];
  23. }
  24. return self;
  25. }
  26. + (void)setReturnURLScheme:(NSString *)returnURLScheme {
  27. [BTAppContextSwitcher sharedInstance].returnURLScheme = returnURLScheme;
  28. }
  29. + (BOOL)handleOpenURL:(NSURL *)url {
  30. return [[BTAppContextSwitcher sharedInstance] handleOpenURL:url];
  31. }
  32. + (BOOL)handleOpenURLContext:(UIOpenURLContext *)URLContext API_AVAILABLE(ios(13.0)) {
  33. return [[BTAppContextSwitcher sharedInstance] handleOpenURL:URLContext.URL];
  34. }
  35. - (BOOL)handleOpenURL:(NSURL *)url {
  36. for (Class<BTAppContextSwitchDriver> driverClass in self.appContextSwitchDrivers) {
  37. if ([driverClass canHandleReturnURL:url]) {
  38. [driverClass handleReturnURL:url];
  39. return YES;
  40. }
  41. }
  42. return NO;
  43. }
  44. - (void)registerAppContextSwitchDriver:(Class<BTAppContextSwitchDriver>)driver {
  45. if (!driver) return;
  46. [self.appContextSwitchDrivers addObject:driver];
  47. }
  48. @end