LoginSignup
4
1

More than 3 years have passed since last update.

Value of optional type 'AuthCredential?' must be unwrapped to a value of type 'AuthCredential'

Last updated at Posted at 2019-08-10

Firebase | iOSでTwitterを使用して認証するをサクッとやってみようと思い久々にXcodeを立ち上げたりpodコマンドを叩いたりしてみると結構苦戦したので3〜4時間格闘した記録です。

目的

元々は、UnityアプリでSNS連携を実装したかったんですが、前回の記事の通りネイティブプラグインを実装する必要があるため、各プラットフォーム側の対応がどんなもんなのかを知るためにまずはiOSでSNS連携を実装してみようと思い事が始まりました。

事象

今回起きた事象は2点、1つは公式で書かれている$ pod 'Firebase/Auth'Unknown commandになってしまう。
もう1つがタイトルにもある通りValue of optional type 'AuthCredential?' must be unwrapped to a value of type 'AuthCredential'になります。

[!] Unknown command: 'Firebase/Auth'

スクリーンショット 2019-08-10 19.15.47.png

解決方法

準備が足りていないのか何が原因なのか分からずじまいだったので強引に$ vi Podfileで解決させましたw

# platform :ios, '9.0'
use_frameworks!

target 'unityfirebaseapp' do

  pod 'Firebase/Auth'

end

$pod installして正常に入ったことを確認
スクリーンショット 2019-08-10 19.20.12.png

Value of optional type 'AuthCredential?' must be unwrapped to a value of type 'AuthCredential'

さて、これでimport Firebaseがエラーにならないことを確認して、公式のサンプルコードをAppDelegate.swiftViewController.swiftに書き込んでいくとSNS連携の処理でタイトルのエラーが出ました。

スクリーンショット 2019-08-10 19.04.10.png

@IBActionのメソッドでボタンが押されたら処理するようにするとこのように出ます。
Swiftはプライベートで少ししか触っていないこともあり、アンラップしないといけないぐらいにしか分からないレベルでめちゃ苦戦しました汗

解決方法

公式サンプルのfirebase/quickstart-iosのソースコードを参考にしました。
authentication/AuthenticationExampleSwift/MainViewController.swiftにTwitter連携しているソースコードがあるため、その部分だけを抜き取り、調整して組み込んだところ以下の内容でエラーは無くなりました。

import UIKit
import Firebase
import FirebaseAuth

class ViewController: UIViewController {

    var twitterProvider : OAuthProvider?

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

        self.twitterProvider = OAuthProvider(providerID:"twitter.com");
    }

    @IBAction func login(_ sender: Any) {
        self.twitterProvider?.getCredentialWith(_: nil){ (credential, error) in
            if error != nil {
                // Handle error.
            }
            if let credential = credential {
                Auth.auth().signIn(with: credential) { (authResult, error) in
                    if error != nil {
                        // Handle error.
                    }
                    // User is signed in.
                    // IdP data available in authResult.additionalUserInfo.profile.
                    // Twitter OAuth access token can also be retrieved by:
                    // authResult.credential.accessToken
                    // Twitter OAuth ID token can be retrieved by calling:
                    // authResult.credential.idToken
                    // Twitter OAuth secret can be retrieved by calling:
                    // authResult.credential.secret
                }
            }
        }
    }
}

たぶんprovider?が必要だったんですかね?笑

accessTokensecretの取得

本来の目的であるaccessTokensecretの取得を上記のソースコードで実装する際、同じくアンラップ関連のエラーが表示されます。

Value of optional type 'AuthDataResult?' must be unwrapped to refer to member 'credential' of wrapped base type 'AuthDataResult'

Stack Overflowと記事を参考に実装中...w

 

4
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
1