Given that some of our data providers have started banning the use of WebViews (on iOS and Android), we are encouraging our customers to update their SDK implementations to use an instance of their native browsers instead of creating browser view objects. In the example below we show how to integrate Connect by using a SFSafariViewController instead of a UIWebView.
Given that SFSafariViewController instances cannot be manipulated like UIWebViews, we need to make use of URL redirection when integrating Connect. This means that we have to opt for a simpler integration on the mobile side but customers need to update their server side callback URLs to support an HTTP GET method instead. In this case, Connect will be automatically redirecting the user to a customer hosted URL that should process the newly created token and redirect the user back to the customer app.
This is the list of steps customers should follow in order to update their SDK implementations to support SFSafariViewController and URL redirection:
This is a new class that handles generating a Connect URL for any user given their clientUserId and their publicToken. It doens't provide any other functionality at the moment (see attached files).
Require HumanConnect and SafariServices:
#import "HumanConnect.h"
@import SafariServices;
Ensure your view controller implements the SFSafariViewControllerDelegate protocol:
@interface ViewController : UIViewController <SFSafariViewControllerDelegate>
In your view controller's viewDidLoad method, register a new observer that will listen to external notifcations when a user has successfully finished a Connect interaction (i.e. after the token exchange is completed on your server):
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(completeConnection:) name:@"HumanAPIConnection" object:nil];
Inside the method where you launch Connect on your view controller instance:
// Register your server-side callback URL to finalize authentication on your server
NSString *callbackURL = @"http://127.0.0.1:8080/sdk-test";
// Generate a Connect URL with HumanConnect
HumanConnect *connect = [[HumanConnect alloc] initWithClientID:myClientID andCallbackURL:callbackURL];
connect.options = [NSDictionary dictionaryWithObjectsAndKeys:
@"en",@"language",
nil];
NSURL *connectUrl = [connect generateURLForNewUser:@"andres-test"];
// And present Connect to the user
self.connectWindow = [[SFSafariViewController alloc] initWithURL:connectUrl];
self.connectWindow.delegate = self;
[self presentViewController:self.connectWindow animated:YES completion:nil];
Now, when the user finalizes their Connect session, they will be redirected to the callback URL you registered above. This is where you will now have to make a change to your implementation. The next step in this process is to redirect your user back to your own app and allow Connect to be closed gracefully. This can be achieved by doing the following:
Redirect user to your app by issuing a redirect with the following URL format from your server:
// Redirect to a URL with the 'HumanAPIAuthDemo' protocol
"HumanAPIAuthDemo://co.my.company?" + queryString;
// Where queryString are any values you want to pass back to your app.
Ensure your app handles the application:openURL
method on the delegate:
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
if ([sourceApplication isEqualToString:@"com.apple.SafariViewService"]) {
[[NSNotificationCenter defaultCenter] postNotificationName:@"HumanAPIConnection" object:url];
}
return YES;
}
Given that you already setup an observer on your view controller class, completeNotification:
handler should take care of gracefully closing the SFSafariViewController instance:
- (void)completeConnection:(NSNotification *)notification
{
NSLog(@"User has completed authorization process! Popup is going to be automatically closed.");
[self.connectWindow dismissViewControllerAnimated:YES completion:nil];
}
Attached you can find some sample code that implements the integration steps defined above.