-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathDisqusAuthenticateViewController.swift
111 lines (89 loc) · 4.19 KB
/
DisqusAuthenticateViewController.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
//
// DisqusAuthenticateViewController.swift
// SwiftDisqusManager
//
// Created by TheFlow_ on 10/03/2015.
// Copyright (c) 2015 TheFlow_. All rights reserved.
//
import UIKit
class DisqusAuthenticateViewController: UIViewController, UIWebViewDelegate, UIAlertViewDelegate {
var webView: UIWebView!
let disqusManager = DisqusManager()
let authorizationURL = NSURL(string: "https://disqus.com/api/oauth/2.0/authorize/?scope=read,write&response_type=api_key&redirect_uri=\(DisqusManager().authRedirectURL)")!
var onSuccess: (() -> ())!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
self.navigationItem.title = "Connexion à Disqus"
webView = UIWebView(frame: self.view.bounds)
webView.delegate = self
self.view.addSubview(webView)
let cancelButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Cancel, target: self, action: "cancel")
self.navigationItem.leftBarButtonItem = cancelButton
webView.loadRequest(NSURLRequest(URL: authorizationURL))
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func paramsFromQueryString(queryString: String) -> [String: String] {
var returnDict = [String: String]()
for pair in queryString.componentsSeparatedByString("&") {
let keyAndValue = pair.componentsSeparatedByString("=")
if keyAndValue.count == 2 {
let key = keyAndValue[0] as String
let value = keyAndValue[1] as String
returnDict[key] = value
}
}
return returnDict
}
func cancel() {
self.dismissViewControllerAnimated(true, completion: nil)
}
func complete() {
self.dismissViewControllerAnimated(true, completion: onSuccess)
}
// MARK: - Web view delegate
func webViewDidStartLoad(webView: UIWebView) {
// Afficher l'icône de chargement dans la barre de status
UIApplication.sharedApplication().networkActivityIndicatorVisible = true
}
func webViewDidFinishLoad(webView: UIWebView) {
// Masquer l'icône de chargement dans la barre de status
UIApplication.sharedApplication().networkActivityIndicatorVisible = false
}
func webView(webView: UIWebView, shouldStartLoadWithRequest request: NSURLRequest, navigationType: UIWebViewNavigationType) -> Bool {
if request.URL.host == NSURL(string: disqusManager.authRedirectURL)!.host {
let params = self.paramsFromQueryString(request.URL.query!)
if let code = params["code"] {
disqusManager.authenticate(code, onSuccess: {
self.complete()
}, onFailure: {
let errorAlert = UIAlertView(title: "Erreur", message: "Une erreur s'est produite lors de la connexion à Disqus. Merci de réessayer plus tard.", delegate: self, cancelButtonTitle: "OK")
errorAlert.delegate = self
dispatch_async(dispatch_get_main_queue(), {
errorAlert.show()
})
})
UIApplication.sharedApplication().networkActivityIndicatorVisible = false // Masquer l'icône de chargement dans la barre de status
return false
}
}
return true
}
// MARK: - Alert view delegate
func alertView(alertView: UIAlertView, clickedButtonAtIndex buttonIndex: Int) {
if buttonIndex == alertView.cancelButtonIndex {
self.dismissViewControllerAnimated(true, completion: nil)
}
}
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
}
*/
}