Callback on some URL
Sometimes we need to use the browser for open authentication process like payment or sign in or other things for this we provide the response URL on success or failure through which identify when we have to close the safari.
Here I have taken a basic example where we are are creating the “test.html” file and we are returning the custom URL look like:
1 2 3 4 5 6 7 8 |
<!DOCTYPE html> <html> <head> <meta http-equiv="Refresh" content="3; URL='freedomofkeima://?ThisIsResponseSample'"> </head> <body> </body> </html> |
Here “freedomofkeima” is a URL Schemes.
After that you can set the response data to depend upon your need:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
<key>LSApplicationQueriesSchemes</key> <array> <string>freedomofkeima</string> </array> <key>CFBundleURLTypes</key> <array> <dict> <key>CFBundleURLSchemes</key> <array> <string>freedomofkeima</string> </array> <key>CFBundleURLName</key> <string>com.freedomofkeima.ios-safariview-example</string> </dict> </array> <key>UIRequiresPersistentWiFi</key> <true/> |
2: Now test.html file put into your MAMP’s htdocs folder.
3 :Now your local URL is:
“http://localhost:8888/test.html
4: Now start the Objective C code:
5: Here first create NSNOtification when it will throw the response:
1 2 3 4 5 |
- (void)viewDidLoad { [super viewDidLoad]; // Set notification handler for SFSafariViewController callback [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(safariCallback:) name:@"SafariCallback" object:nil]; } |
6: Now launch the Safari:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
- (void)viewDidAppear:(BOOL)animated { if (is_startup) { BOOL isSafariViewSupported = [self showSafariPage]; NSLog(@"Support? %d", isSafariViewSupported); if (!isSafariViewSupported) { UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Warning" message:@"iOS version is less than 9" preferredStyle:UIAlertControllerStyleAlert]; UIAlertAction *okButton = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) {}]; [alert addAction:okButton]; [self presentViewController:alert animated:YES completion:nil]; } is_startup = NO; } } |
1 2 3 4 5 6 7 8 9 10 |
- (BOOL)showSafariPage { if ([SFSafariViewController class]) { NSURL *url = [NSURL URLWithString:url_endpoint]; SFSafariViewController *safariVC = [[SFSafariViewController alloc] initWithURL:url]; safariVC.delegate = self; [self presentViewController:safariVC animated:YES completion:nil]; return YES; } return NO; } |
Now Define the callback method:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
- (void)safariCallback:(NSNotification *)notification { // Dismiss View [self dismissViewControllerAnimated:NO completion:nil]; // Extract information from notification NSDictionary *dict = [notification userInfo]; NSString *value = [dict objectForKey:@"key"]; NSLog(@"Value is %@", value); // Show result as alert dialog UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Key content" message:value preferredStyle:UIAlertControllerStyleAlert]; UIAlertAction *okButton = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) {}]; [alert addAction:okButton]; [self presentViewController:alert animated:YES completion:nil]; } # pragma mark - SFSafariViewController delegate methods - (void)safariViewController:(SFSafariViewController *)controller didCompleteInitialLoad:(BOOL)didLoadSuccessfully { // Load finished NSLog(@"Load finished"); } - (void)safariViewControllerDidFinish:(SFSafariViewController *)controller { // Done button pressed NSLog(@"Done button pressed"); } |
7: Now go to Appdelegate.m file and write this one:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<NSString *, id> *)options { NSLog(@"application openURL options is called"); NSString *sourceApplication = [options objectForKey:@"UIApplicationOpenURLOptionsSourceApplicationKey"]; NSString *urlScheme = [url scheme]; NSString *urlQuery = [url query]; NSLog(@"valueaaa %@",urlQuery); // Check URL scheme and caller if ([urlScheme isEqualToString:@"freedomofkeima"] && [sourceApplication isEqualToString:@"com.apple.SafariViewService"]) { // Pass it via notification NSLog(@"Value: %@", urlQuery); NSDictionary *data = [NSDictionary dictionaryWithObject:urlQuery forKey:@"key"]; [[NSNotificationCenter defaultCenter] postNotificationName:@"SafariCallback" object:self userInfo:data]; return YES; } return NO; } |
8: Now when you run this app this track your custom URL.