惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

WordPress大学
WordPress大学
T
Threatpost
阮一峰的网络日志
阮一峰的网络日志
美团技术团队
F
Fortinet All Blogs
The GitHub Blog
The GitHub Blog
月光博客
月光博客
V
Visual Studio Blog
T
Tailwind CSS Blog
Stack Overflow Blog
Stack Overflow Blog
博客园 - 聂微东
Jina AI
Jina AI
J
Java Code Geeks
Martin Fowler
Martin Fowler
大猫的无限游戏
大猫的无限游戏
Recorded Future
Recorded Future
C
Check Point Blog
腾讯CDC
N
Netflix TechBlog - Medium
aimingoo的专栏
aimingoo的专栏
罗磊的独立博客
Hacker News: Ask HN
Hacker News: Ask HN
SecWiki News
SecWiki News
博客园 - Franky
Hacker News - Newest:
Hacker News - Newest: "LLM"
N
News | PayPal Newsroom
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
S
Security @ Cisco Blogs
W
WeLiveSecurity
The Last Watchdog
The Last Watchdog
Cloudbric
Cloudbric
F
Full Disclosure
The Cloudflare Blog
Y
Y Combinator Blog
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
Recent Commits to openclaw:main
Recent Commits to openclaw:main
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Google DeepMind News
Google DeepMind News
MongoDB | Blog
MongoDB | Blog
S
Schneier on Security
Schneier on Security
Schneier on Security
Spread Privacy
Spread Privacy
L
LINUX DO - 热门话题
AI
AI
N
News and Events Feed by Topic
T
Tor Project blog
P
Palo Alto Networks Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
H
Hackread – Cybersecurity News, Data Breaches, AI and More
G
Google Developers Blog

博客园 - 瑞雪年

开始使用 Xamarin.Forms.Platform.Avalonia Git仓库迁移命令 .NET Core也可以使用MongoDB了 ng2-timesheet, 一个timesheet.js的angular2复制版 Angular 2 + Electron 开发web和桌面应用 Xamarin.Forms.Platform.Perspex, Xamarin Forms 的 Perspex(号称下一代WPF) 实现 Visual Studio Xamarin编译Android项目出错的解决办法 Salesforce + AngularJS + Bootstrap NativeScript 也能开发桌面应用 (nativescript-dotnet-runtime) WinObjC?这是什么鬼? 如何禁用Marlin温度保护 React Native也正式发布了 微信企业号 用javascript写Android和iOS naitve应用,实在炫酷。 CoreCLR 在 Linux 下编译成功 Swift 对比学习 (二) Swift 对比学习 (一) 重量级Orchard模块发布 - 模块生成工具RaisingStudio.ModuleGenerator coding.net
iOS集成丁香园DXY OAuth 登陆 swift代码示例
瑞雪年 · 2015-12-21 · via 博客园 - 瑞雪年

问:iOS集成OAuth登陆分几步?

答:和把大象放冰箱里一样。

第一步:打开webview,跳转到登陆页面:

        let url = "https://auth.dxy.cn/conn/oauth2/authorize?clientId=xxx&state=xxx&responseType=code&redirectUri=xxx”
        webView.loadRequest(NSURLRequest(URL: NSURL(string:url)!))

第二步:在AppDelegate中,使用

func application(application: UIApplication, handleOpenURL url: NSURL) -> Bool {}方法获取返回的code, 再通过code获取accessToken:

        let requestUrl = NSURL(string: "https://auth.dxy.cn/conn/oauth2/accessToken")
        let postData =  "clientId=xxx&clientSecret=xxx&grantType=authorizationCode&redirectUri=xxx&code=\(code)"
         let request = NSMutableURLRequest(URL: requestUrl!)
        request.HTTPMethod = "POST"
        request.HTTPBody = postData.dataUsingEncoding(NSUTF8StringEncoding)

第三步:通过accessToken获取用户信息,再捎带把webview关了:

let getUserInfoRequestUrl = NSURL(string: "https://auth.dxy.cn/conn/oauth2/profile")
let getUserInfoPostData = "accessToken=\(access_token)"
                            
let getUserInfoRequest = NSMutableURLRequest(URL: getUserInfoRequestUrl!)
getUserInfoRequest.HTTPMethod = "POST"
getUserInfoRequest.HTTPBody = getUserInfoPostData.dataUsingEncoding(NSUTF8StringEncoding)
                        
let getUserInfoDataTask = session.dataTaskWithRequest(getUserInfoRequest,
            completionHandler: {(data:NSData?, response:NSURLResponse?, error:NSError?) -> Void in
                  if error != nil {
                      print(error?.code)
                      print(error?.description)
                  }
                  else    {
                     let str = NSString(data: data!, encoding: NSUTF8StringEncoding)
                     do{
                        let userInfoResult = try NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions())
                        let id = userInfoResult["id"] as? String

                        let viewController = self.window?.rootViewController as! ViewController
                        viewController.id = id
                        viewController.closeWebView()
                     }
                     catch {}
                 }
       })
                            
getUserInfoDataTask.resume()

Ok, That's it. 是不是很简单?!