前提・実現したいこと
ZaimAPIを使って音声で出費登録できるiOSアプリを作っています。
まずOAuth認証をしてアクセスキーを入手したく下記を参考にしているのですが、「ログインして許可する」を押して「認証が完了」と表示されるもののアプリに戻ってくれず、アクセスキーが入手できません。
http://qs.nndo.jp/oauth-swift-zaim
https://qiita.com/fromage-blanc/items/739622dee826806c12d5
発生している問題・エラーメッセージ
予定では認証完了後にAppDelegateのメソッドが呼ばれて、コールバック時のクロージャー内でアクセスキー(credential.oauthToken
とcredential.oauthTokenSecret
)がprint出力されるはずなのですが、まずAppDelegateのメソッドが呼ばれません。
Xcodeのコンソールにも特にエラーメッセージはでていないです。
該当のソースコード
※一度Zaim Developers Center側でアプリを削除して作り直そうとしたら、同じ名称だと重複と言われたので名称を変えています。
swift
1// ViewController.swift 2import UIKit 3import OAuthSwift 4 5class ViewController: UIViewController { 6 7 var oauthswift: OAuthSwift? = nil 8 9 @IBAction func onTappedSyncButton(_ sender: UIButton) { 10 11 let oauthswift = OAuth1Swift( 12 consumerKey: "**********", 13 consumerSecret: "**********", 14 requestTokenUrl: "https://api.zaim.net/v2/auth/request", 15 authorizeUrl: "https://auth.zaim.net/users/auth", 16 accessTokenUrl: "https://api.zaim.net/v2/auth/access" 17 ) 18 19 self.oauthswift = oauthswift 20 oauthswift.authorizeURLHandler = getURLHandler() 21 22 let _ = oauthswift.authorize( 23 withCallbackURL: URL(string: "KoeDeZaim://oauth-callback")!, 24 success: { credential, response, parameters in 25 self.showTokenAlert(name: "Service", credential: credential) 26 }, 27 failure: { error in 28 print(error.description) 29 } 30 ) 31 } 32 33 func getURLHandler() -> OAuthSwiftURLHandlerType { 34 if #available(iOS 9.0, *) { 35 let handler = SafariURLHandler(viewController: self, oauthSwift: self.oauthswift!) 36 handler.presentCompletion = { 37 print("Safari presented") 38 } 39 handler.dismissCompletion = { 40 print("Safari dismissed") 41 } 42 return handler 43 } 44 return OAuthSwiftOpenURLExternally.sharedInstance 45 } 46 47 func showTokenAlert(name: String?, credential: OAuthSwiftCredential) { 48 var message = "oauth_token:(credential.oauthToken)" 49 if !credential.oauthTokenSecret.isEmpty { 50 message += "\n\noauth_token_secret:(credential.oauthTokenSecret)" 51 } 52 self.showAlertView(title: name ?? "Service", message: message) 53 54 } 55 56 func showAlertView(title: String, message: String) { 57 let alert = UIAlertController(title: title, message: message, preferredStyle: UIAlertController.Style.alert) 58 alert.addAction(UIAlertAction(title: "Close", style: UIAlertAction.Style.default, handler: nil)) 59 self.present(alert, animated: true, completion: nil) 60 } 61 62}
swift
1// AppDelegate.swift 2import UIKit 3import OAuthSwift 4 5@UIApplicationMain 6class AppDelegate: UIResponder, UIApplicationDelegate { 7 8 func application(_ application: UIApplication, open url: URL, sourceApplication: String?, annotation: Any) -> Bool { 9 applicationHandle(url: url) 10 return true 11 } 12} 13 14extension AppDelegate { 15 16 func applicationHandle(url: URL) { 17 if (url.host == "oauth-callback") { 18 OAuthSwift.handle(url: url) 19 } else { 20 // Google provider is the only one wuth your.bundle.id url schema. 21 OAuthSwift.handle(url: url) 22 } 23 } 24}
ruby
1# platform :ios, '10.0' 2 3target 'KoeDeZaim' do 4 # Comment the next line if you're not using Swift and don't want to use dynamic frameworks 5 use_frameworks! 6 7 # Pods for KoeDeZaim 8 pod 'OAuthSwift' 9 10 target 'KoeDeZaimTests' do 11 inherit! :search_paths 12 # Pods for testing 13 end 14 15 target 'KoeDeZaimUITests' do 16 inherit! :search_paths 17 # Pods for testing 18 end 19 20end
試したこと
- デリゲートメソッドを以下に変えても呼ばれませんでした。
swift
1// AppDelegate.swift 2 func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool { 3 if (url.host == "oauth-callback") { 4 OAuthSwift.handle(url: url) 5 } 6 return true 7 }
- 認証完了画面で左上の「完了」ボタンを押してアプリに戻っても特に変わりません。
getURLHandler()
内で必ずOAuthSwiftOpenURLExternally.sharedInstance
を返すようにし、Safariで認証を行ってもうまくいきませんでした。- OAuthSwiftのDemoで以下すべてのパターンでうまくいきませんでした。
・external web browser
・internal web browser
・SFSafariViewController
補足情報(FW/ツールのバージョンなど)
- Xcode10.1
- Swift4.2
- Depleyment Target: iOS 12.1
- OAuthSwift: 1.2.2
完成イメージ
https://soh335.hatenablog.com/entry/2013/03/03/004306
回答2件
あなたの回答
tips
プレビュー