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

推荐订阅源

酷 壳 – CoolShell
酷 壳 – CoolShell
T
Threatpost
Latest news
Latest news
N
News | PayPal Newsroom
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Help Net Security
Help Net Security
D
Darknet – Hacking Tools, Hacker News & Cyber Security
AI
AI
Simon Willison's Weblog
Simon Willison's Weblog
TaoSecurity Blog
TaoSecurity Blog
The Last Watchdog
The Last Watchdog
L
LINUX DO - 热门话题
Google DeepMind News
Google DeepMind News
T
Threat Research - Cisco Blogs
O
OpenAI News
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
T
The Exploit Database - CXSecurity.com
NISL@THU
NISL@THU
Application and Cybersecurity Blog
Application and Cybersecurity Blog
S
Securelist
小众软件
小众软件
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Martin Fowler
Martin Fowler
S
SegmentFault 最新的问题
Cisco Talos Blog
Cisco Talos Blog
云风的 BLOG
云风的 BLOG
AWS News Blog
AWS News Blog
GbyAI
GbyAI
N
News and Events Feed by Topic
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
美团技术团队
Engineering at Meta
Engineering at Meta
A
About on SuperTechFans
博客园 - 三生石上(FineUI控件)
S
Schneier on Security
博客园 - 聂微东
V2EX - 技术
V2EX - 技术
T
Troy Hunt's Blog
SecWiki News
SecWiki News
S
Secure Thoughts
B
Blog RSS Feed
Hugging Face - Blog
Hugging Face - Blog
WordPress大学
WordPress大学
腾讯CDC
H
Heimdal Security Blog
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
Apple Machine Learning Research
Apple Machine Learning Research
月光博客
月光博客
www.infosecurity-magazine.com
www.infosecurity-magazine.com
P
Privacy International News Feed

博客园 - 瑞雪年

开始使用 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. 是不是很简单?!