How to redirect back to my iOS app after logging i

2019-07-26 04:47发布

I am developing a very basic iOS app with Swift. Just to read the heart rate data. I am using SFSafariViewController. As known, I first need to register my app on dev.fitbit.com. The registration form requires a callback URL to be entered.

After logging in successfully, FitBit always redirects me back to that entered callback URL. What should I do/code/configure to be able to redirect user back to my iOS app after logging in successfully?

2条回答
We Are One
2楼-- · 2019-07-26 05:27

Try using below 3 steps in your application.

You are using "fitbit", so I am considering your are using OAuth2.0 for login and get access-token.

Step 1 : Set up you URL Schemes.

enter image description here

Step 2 : In your AppDelegate class

func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool {
  if (url.host == "oauth-swift") {
    OAuthSwift.handle(url: url)
  }
  return true
}

In Above function what we did is, we check the URL comes to handleOpenUrl method, and check weather it is the right url call back is coming or not.

Step 3 : Set the proper call back URL in you OAuth Handler.

oauthswift = OAuth2Swift(
    consumerKey:    "********",
    consumerSecret: "********",
    authorizeUrl:   "your authorisation url",
    responseType:   "token"
)
let handle = oauthswift.authorize(
    withCallbackURL: URL(string: "oauth-swift://oauth-callback/fitbit")!,
    scope: "your application scope", state:"state",
    success: { credential, response, parameters in
      print(credential.oauth_token)
    },
    failure: { error in
      print(error.localizedDescription)
    }
)

In above step, we set the call back url starting with "oauth-swift:", so it will be work as a host of your call back url.

Image and Code Courtesy : I have tried to explain a solution of your problem in easy words. And all information of this answers are originally documented and explained on this URL : https://github.com/OAuthSwift/OAuthSwift

查看更多
仙女界的扛把子
3楼-- · 2019-07-26 05:39

What you need to do is to add the application(_ application: UIApplication, open url: URL, sourceApplication: String?, annotation: Any) -> Bool function to your AppDelegate and then create an url scheme for your application as an identifier. To create an url scheme go to your application target > Info > URL Types (at the bottom). Then just add the following in your AppDelegate:

func application(_ application: UIApplication, open url: URL, sourceApplication: String?, annotation: Any) -> Bool {
    DispatchQueue.main.async {
        // Conctrol so that we´re coming from the right application
        if (url.scheme == "The url scheme that you created"){
            // Navigate to the viewController you want
            let storyboard = UIStoryboard(name: "Main", bundle: nil)
            let controller = storyboard.instantiateViewController(withIdentifier: "WebView") as! WebViewController
            self.window?.rootViewController!.present(controller, animated: true, completion: { () -> Void in
            })
        }
    }
    return true
}
查看更多
登录 后发表回答