Skip to content

Instantly share code, notes, and snippets.

@29satnam
Created February 7, 2016 05:29
Show Gist options
  • Save 29satnam/bf5174baa9047a9f6dd8 to your computer and use it in GitHub Desktop.
Save 29satnam/bf5174baa9047a9f6dd8 to your computer and use it in GitHub Desktop.
import UIKit
class ViewController: UIViewController, UIWebViewDelegate {
var webView: UIWebView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
@IBAction func twitterLogin(sender: UIButton) {
webView = UIWebView(frame: CGRectMake(0, 0, UIScreen.mainScreen().bounds.width, UIScreen.mainScreen().bounds.height))
webView.loadRequest(NSURLRequest(URL: NSURL(string: "http://xxxxxxxxx.com:2403/auth/twitter")!))
webView.delegate = self;
self.view.addSubview(webView)
}
func webView(webView: UIWebView, shouldStartLoadWithRequest request: NSURLRequest, navigationType: UIWebViewNavigationType) -> Bool {
let splitURL = request.URL!.absoluteString.characters.split{$0 == "?"}.map(String.init)
print(splitURL)
if splitURL[0] == "http://xxxxxxxxx.com:2403/auth/twitter/callback" {
webView.stopLoading();
let url = NSURL(string: request.URL!.absoluteString)
let task = NSURLSession.sharedSession().dataTaskWithURL(url!) {(data, response, error) in
let datastring = NSString(data: data!, encoding: NSUTF8StringEncoding)
let responsedata = self.convertStringToDictionary(datastring as! String);
print("\(responsedata!["id"])");
print(responsedata!)
dispatch_async(dispatch_get_main_queue(), {
webView.removeFromSuperview()
})
}
task.resume()
}
return true
}
func convertStringToDictionary(text: String) -> [String:AnyObject]? {
if let data = text.dataUsingEncoding(NSUTF8StringEncoding) {
do {
return try NSJSONSerialization.JSONObjectWithData(data, options: []) as? [String:AnyObject]
} catch let error as NSError {
print(error)
}
}
return nil
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment