0

I am working on IOS app in which I open login page in SFSafariViewController. And On that login page when the login is successful, a Json response is generated.

I want to get that response and close SFSafariViewController.

In android I did it through Javascript and Javascript interface..

My server side code for android:

<?php
//curl initialize
//Some code
//Curl Execution
$response = curl_exe($ch)
?>
<script>
showAndroidToast("<?php echo $response;?>");
function showAndroidToast(res){
Android.dataResponse(res);
}
</script>

In android.

Android.dataResponse(res) is my javascript interface which i added to my Chrome Custom Tabs (https://developer.chrome.com/multidevice/android/customtabs).

I want similar functionality for my IOS app.I don't know. Is this possible with SFSafariViewController or not?

Avinash kumawat
  • 475
  • 6
  • 18

1 Answers1

2

According to this StackOverflow answer you cannot download files with SFSafariViewController Furthermore there seems to be no delegate to initiate the download of the JSON according to Apple's documentation

Given this information I am pretty sure that your safest bet is to go the WKWebView route even though WebKit has all sorts of limitations on iOS. You could probably use this as a starting point.

I would personally just call my login API's and create a native login form thus circumventing Apple's browsing options altogether. I hope this helps!

EDIT

The OP is actually using FitBit's API and OAuth2 and according to FitBit's Documentation:

For native applications, this means the authorization page must open in the default browser. Native applications can use custom URL schemes as redirect URIs to redirect the user back from the browser to the application requesting permission. iOS applications may use the SFSafariViewController class instead of app switching to Safari. Use of the WKWebView or UIWebView class is prohibited.

Pointers for using FitBit's Authorization Code Grant Flow can be found in their documentation but there is no proper guide. But OAuthSwift supports FitBit so that is probably the easiest way to implement the login. A discussion about a sample integration can also be found here and here.

Community
  • 1
  • 1
tech4242
  • 2,348
  • 2
  • 23
  • 33
  • I am integrating fitbit API. So according to their documentation. "iOS applications may use the SFSafariViewController class instead of app switching to Safari. Use of the WKWebView or UIWebView class is prohibited." https://dev.fitbit.com/docs/oauth2/#scope – Avinash kumawat Jan 16 '17 at 10:34
  • @Avinashkumawat I browsed through the documentation and you are right - OAuth2 only works with a browser-based approach. Try using the Authorization Code Grant Flow from FitBit's documentation: https://dev.fitbit.com/docs/oauth2/#authorization-code-grant-flow together with this StackOverflow answer (it is Objective-C but it should definitely point you in the right direction): http://stackoverflow.com/questions/32601255/how-to-do-access-token-request-in-fitbit-api-integration-ios – tech4242 Jan 16 '17 at 11:08
  • I have already followed Authorization Code Grant Flow. I am able to successfully login to fitbit login process. But how I can get the access_token and close safari controller after immediately getting the access_token. – Avinash kumawat Jan 16 '17 at 11:43
  • @Avinashkumawat Ok, have you checked this out: http://stackoverflow.com/questions/39908599/how-to-redirect-back-to-my-ios-app-after-logging-in-on-fitbit-login-page/ There are 2 options for redirecting back to your app; the second answer is also using OAuthSwift – tech4242 Jan 16 '17 at 11:55