Ios OAuth2:登录后返回应用程序

Ios OAuth2:登录后返回应用程序,ios,swift,oauth-2.0,Ios,Swift,Oauth 2.0,我正在使用一个名为()的cocoapod来登录一个带有GitHub的帐户。我只是在玩,因为我想知道OAuth2是如何工作的 以下是迄今为止我在视图控制器(UIViewController)中拥有的内容 我想知道的是,在我成功地使用GitHub登录我的帐户后,如何返回到视图控制器。到目前为止,我将被重定向到我在GitHub的开发者网站上注册的URL。我想我需要在Info下注册一个回调URL方案。这一切都是全新的。所以我不知道怎么做。谢谢 更新 我通过info.plist添加了一组URL类型。URL

我正在使用一个名为()的cocoapod来登录一个带有GitHub的帐户。我只是在玩,因为我想知道OAuth2是如何工作的

以下是迄今为止我在视图控制器(
UIViewController
)中拥有的内容

我想知道的是,在我成功地使用GitHub登录我的帐户后,如何返回到视图控制器。到目前为止,我将被重定向到我在GitHub的开发者网站上注册的URL。我想我需要在
Info
下注册一个回调URL方案。这一切都是全新的。所以我不知道怎么做。谢谢

更新

我通过info.plist添加了一组
URL类型。URL方案已设置为myapp。下面是我在
AppDelegate
中看到的内容

func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey: Any] = [:]) -> Bool {
    if url.scheme == "myapp" && url.host == "oauth" {
        //oauth2.handleRedirectURL(url)
        let storyboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
        let controller = storyboard.instantiateViewController(withIdentifier: "HomeView") as! UINavigationController
        let viewController = controller.topViewController as! ViewController
        window?.rootViewController = viewController
    }
    window?.makeKeyAndVisible()
    return true
}
func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey: Any] = [:]) -> Bool {
    if url.scheme == "myapp" && url.host == "oauth" {
        let storyboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
        let controller = storyboard.instantiateViewController(withIdentifier: "HomeView") as! UINavigationController
        let viewController = controller.topViewController as! ViewController
        window?.rootViewController = viewController
    }
    window?.makeKeyAndVisible()
    return true
}

但是这个应用程序从来没有调用过上述功能。

我今天学到了一些新东西

  • 在GitHub的开发者网站上,我将回调URL设置为myapp://oauth/callback. (见下文。)
  • 我通过Info.plist创建了一组URL方案。标识符由我的反向域+唯一名称组成。一个URL项目是myapp。(见下文。)
  • 最后,我在
    AppDelegate
    中添加了以下代码行

    func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey: Any] = [:]) -> Bool {
        if url.scheme == "myapp" && url.host == "oauth" {
            //oauth2.handleRedirectURL(url)
            let storyboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
            let controller = storyboard.instantiateViewController(withIdentifier: "HomeView") as! UINavigationController
            let viewController = controller.topViewController as! ViewController
            window?.rootViewController = viewController
        }
        window?.makeKeyAndVisible()
        return true
    }
    
    func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey: Any] = [:]) -> Bool {
        if url.scheme == "myapp" && url.host == "oauth" {
            let storyboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
            let controller = storyboard.instantiateViewController(withIdentifier: "HomeView") as! UINavigationController
            let viewController = controller.topViewController as! ViewController
            window?.rootViewController = viewController
        }
        window?.makeKeyAndVisible()
        return true
    }
    
  • 如果我登录,我会得到如下提示


    当我点击“打开”时,我将被重定向到我的视图控制器。

    我今天学到了一些新东西

  • 在GitHub的开发者网站上,我将回调URL设置为myapp://oauth/callback. (见下文。)
  • 我通过Info.plist创建了一组URL方案。标识符由我的反向域+唯一名称组成。一个URL项目是myapp。(见下文。)
  • 最后,我在
    AppDelegate
    中添加了以下代码行

    func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey: Any] = [:]) -> Bool {
        if url.scheme == "myapp" && url.host == "oauth" {
            //oauth2.handleRedirectURL(url)
            let storyboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
            let controller = storyboard.instantiateViewController(withIdentifier: "HomeView") as! UINavigationController
            let viewController = controller.topViewController as! ViewController
            window?.rootViewController = viewController
        }
        window?.makeKeyAndVisible()
        return true
    }
    
    func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey: Any] = [:]) -> Bool {
        if url.scheme == "myapp" && url.host == "oauth" {
            let storyboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
            let controller = storyboard.instantiateViewController(withIdentifier: "HomeView") as! UINavigationController
            let viewController = controller.topViewController as! ViewController
            window?.rootViewController = viewController
        }
        window?.makeKeyAndVisible()
        return true
    }
    
  • 如果我登录,我会得到如下提示

    当我点击“打开”时,我将重定向到我的视图控制器