現在アプリ開発の学習の一環として、QiitaAPIを用いてQIItaのクライアントアプリを作成しています。
アラートでログイン画面を作り、認証をしたいのですが、よくわからなくなってしまいました。
参考:qiita api
ライブラリとして「OAuthSwift」を用いようと思ったですが、下記コードの一番下の
failure: { error in print(error.localizedDescription)}
のところで、
// エラー Unable to infer closure type in the current context
このようなエラーが出てしまいます。
SWift
1import UIKit 2import OAuthSwift 3 4class LoginViewController: UIViewController, URLSessionDelegate, URLSessionDataDelegate { 5 6 // URLの材料 7 let consumerData:[String:String] = 8 ["consumerKey":"aaaaaaaaaaaaaaaaaaaaaaa", // コンシューマキー 9 "consumerSecret":"bbbbbbbbbbbbbbbbbbbbbbbb"] // コンシューマシークレット 10 11 override func viewDidLoad() { 12 super.viewDidLoad() 13 } 14 15 override func viewDidAppear(_ animated: Bool) { 16 17 let loginAlert = UIAlertController(title: "ログイン", message: "", preferredStyle: .alert) 18 loginAlert.addTextField(configurationHandler: {(textField: UITextField!) -> Void in 19 textField.placeholder = "username" 20 }) 21 loginAlert.addTextField(configurationHandler: {(textField: UITextField!) -> Void in 22 textField.placeholder = "passwd" 23 textField.isSecureTextEntry = true 24 }) 25 // Loginボタン生成 26 let login = UIAlertAction(title: "Login", style: .default, handler: { action -> Void in 27 // 押された時の処理 28 // ログイン処理 29 self.tryLogin(textFeild: loginAlert.textFields!) 30 // 判定 31 if self.lookQuery() == false { 32 // 失敗したら、もう1度Alertを表示 33 self.present(loginAlert, animated: true, completion: nil) 34 } else { 35 // 成功したら、ArticleViewControllerに遷移 36 self.tabBarController?.selectedIndex = 0 37 } 38 }) 39 // Acitonを追加 40 loginAlert.addAction(login) 41 // Alert表示 42 present(loginAlert, animated: true, completion: nil) 43 } 44 45 override func didReceiveMemoryWarning() { 46 super.didReceiveMemoryWarning() 47 // Dispose of any resources that can be recreated. 48 } 49 50 51 // ログイン判定 52 func lookQuery() -> Bool { 53 54 return true 55 } 56 57 58 // ログイン処理 59 func tryLogin(textFeild: [UITextField]) { 60 61 // AlertのTextFieldから値を取得 62 let username = textFeild[0].text 63 let passwd = textFeild[1].text 64 65 print("\(username)") 66 print("\(passwd)") 67 68 let oauthswift = OAuth2Swift( 69 consumerKey: "<consumerKey>", 70 consumerSecret: "<consumerSecret>", 71 authorizeUrl: "https://qiita.com/api/v2/oauth/authorize", 72 accessTokenUrl: "https://qiita.com/api/v2/access_tokens ", 73 responseType: "code" 74 ) 75 oauthswift.authorize(withCallbackURL: NSURL(string: "smartqiita://oauth-callback"), 76 scope: "", 77 state: "", 78 success: { credential, response, parameters in 79 print(credential.oauth_token) 80 // トークンを保存 81 let defaults = NSUserDefaults.standardUserDefaults() 82 defaults.setValue(credential.oauth_token, forKey: "oauth_token") 83 // 画面遷移 84 self.tabBarController?.selectedIndex = 0}, 85 failure: { error in 86 print(error.localizedDescription)} 87 ) 88 } 89}
まだまだ手探りの状態ですが、是非教えていただけたらと思います。
またこのようなログイン機構の手順ようなものも教えていただけたら嬉しいです。
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。