diff --git a/venmo-sdk/Venmo.h b/venmo-sdk/Venmo.h index d155cbb..5202401 100644 --- a/venmo-sdk/Venmo.h +++ b/venmo-sdk/Venmo.h @@ -72,7 +72,7 @@ typedef void (^VENOAuthCompletionHandler)(BOOL success, NSError *error); * @param permissions List of permissions. * @param completionHandler Completion handler to call upon returning from OAuth flow. */ -- (void)requestPermissions:(NSArray *)permissions withCompletionHandler:(VENOAuthCompletionHandler)handler; +- (void)requestPermissions:(NSArray *)permissions withCurrentViewController:(nullable UIViewController*)currentViewController withCompletionHandler:(nonnull VENOAuthCompletionHandler)handler; /** diff --git a/venmo-sdk/Venmo.m b/venmo-sdk/Venmo.m index 05290a2..c4b50f3 100644 --- a/venmo-sdk/Venmo.m +++ b/venmo-sdk/Venmo.m @@ -16,6 +16,8 @@ #import #import +@import SafariServices; + #if __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_6_0 #import "UIDevice+IdentifierAddition.h" #endif @@ -36,6 +38,7 @@ @interface Venmo () @property (copy, nonatomic, readwrite) VENTransactionCompletionHandler transactionCompletionHandler; @property (copy, nonatomic, readwrite) VENOAuthCompletionHandler OAuthCompletionHandler; +@property (nonatomic, readwrite) SFSafariViewController *safariViewController; @property (nonatomic) BOOL internalDevelopment; @@ -95,7 +98,8 @@ + (BOOL)isVenmoAppInstalled { #pragma mark - Sessions - (void)requestPermissions:(NSArray *)permissions - withCompletionHandler:(VENOAuthCompletionHandler)completionHandler { + withCurrentViewController:(nullable UIViewController*)currentViewController + withCompletionHandler:(nonnull VENOAuthCompletionHandler)completionHandler { NSString *scopeURLEncoded = [permissions componentsJoinedByString:@"%20"]; self.OAuthCompletionHandler = completionHandler; self.session.state = VENSessionStateOpening; @@ -103,12 +107,21 @@ - (void)requestPermissions:(NSArray *)permissions NSString *baseURL; if ([Venmo isVenmoAppInstalled]) { baseURL = @"venmo://"; + NSURL *authURL = [NSURL URLWithString:[NSString stringWithFormat:@"%@oauth/authorize?sdk=ios&client_id=%@&scope=%@&response_type=code", baseURL, self.appId, scopeURLEncoded]]; + + [[UIApplication sharedApplication] openURL:authURL]; } else { baseURL = [self baseURLPath]; - } - NSURL *authURL = [NSURL URLWithString:[NSString stringWithFormat:@"%@oauth/authorize?sdk=ios&client_id=%@&scope=%@&response_type=code", baseURL, self.appId, scopeURLEncoded]]; + NSURL *authURL = [NSURL URLWithString:[NSString stringWithFormat:@"%@oauth/authorize?sdk=ios&client_id=%@&scope=%@&response_type=code", baseURL, self.appId, scopeURLEncoded]]; + + if ([SFSafariViewController class] && currentViewController) { + self.safariViewController = [[SFSafariViewController alloc] initWithURL:authURL entersReaderIfAvailable:NO]; - [[UIApplication sharedApplication] openURL:authURL]; + [currentViewController presentViewController:self.safariViewController animated:NO completion:nil]; + } else { + [[UIApplication sharedApplication] openURL:authURL]; + } + } } @@ -302,6 +315,11 @@ - (void)validateAPIRequestWithCompletionHandler:(VENGenericRequestCompletionHand - (BOOL)handleOpenURL:(NSURL *)url { if ([VENURLProtocol canInitWithRequest:[NSURLRequest requestWithURL:url]]) { + if (self.safariViewController) { + [self.safariViewController dismissViewControllerAnimated:YES completion:nil]; + self.safariViewController = nil; + } + VENURLProtocol *urlProtocol = [[VENURLProtocol alloc] initWithRequest:[NSURLRequest requestWithURL:url] cachedResponse:nil client:nil]; [urlProtocol startLoading]; return YES;