JKになりたい

何か書きたいことを書きます。主にWeb方面の技術系記事が多いかも。

Xcode8でFirebaseのCrash Reportingを使う

Xcode8でFirebaseのCrash Reportingを使おうとしたらハマったのでその時の話をメモっときます。

(1) Firebaseの導入

まずは、https://firebase.google.com/docs/ios/setup に書いてある通りにFirebaseを導入していきます。

そして、iOSシミュレータ上でRUNしてみると、以下のようなエラーが出ました。

<FIRInstanceID/WARNING> STOP!! Will reset deviceID from memory.

<FIRInstanceID/WARNING> Failed to fetch default token Error Domain=com.firebase.iid Code=6 “(null)”

f:id:deeptoneworks:20161121133109p:plain

明らかにうまくいってないです。どうやらこれKeyChain周りのせいで発生するっぽいです。
というのも、Xcode8のシミュレータからはKeyChainにアクセスできないというバグがあるらしいのです。。><

これに対処するために、 CaoabukutuesのKeychain sharingをONにします。
f:id:deeptoneworks:20161121133942p:plain

これでうまくいくはずです。

(2) Crash Repotingの導入

次に、Crash Reportingを導入します。
これも、先と同様に https://firebase.google.com/docs/crash/ios を眺めながら勧めていきます。

そして、最後のスクリプトの追加をする部分、ドキュメントには以下のように書いています。

# Replace this path with the path to the key you just downloaded
JSON_FILE=Path/To/ServiceAccount.json
# Replace this with the GOOGLE_APP_ID from your GoogleService-Info.plist file
GOOGLE_APP_ID=1:my:app:id
defaults write com.google.SymbolUpload version -integer 1 # creates file if it does not exist
JSON=$(cat "${JSON_FILE}")
/usr/bin/plutil -replace "app_${GOOGLE_APP_ID//:/_}" -json "${JSON}" "$HOME/Library/Preferences/com.google.SymbolUpload.plist"
"${PODS_ROOT}"/FirebaseCrash/upload-sym

・・・しかし、これで実行すると下記のようなエラーが発生し、クラッシュ情報を送信することができません。 f:id:deeptoneworks:20161121144200p:plain

そこで、スクリプトを以下のように書き換えます。(勿論、JSONFILEとGOOGLEAPP_IDは適切なものに書き換えて下さい)

# Replace this path with the path to the key you just downloaded
JSON_FILE=Path/To/ServiceAccount.json
# Replace this with the GOOGLE_APP_ID from your GoogleService-Info.plist file
GOOGLE_APP_ID=1:my:app:id
defaults write com.google.SymbolUpload version -integer 1
JSON=$(cat "${JSON_FILE}")
/usr/bin/plutil -replace "app_${GOOGLE_APP_ID//:/_}" -json "${JSON}" "$HOME/Library/Preferences/com.google.SymbolUpload.plist"
"${PODS_ROOT}"/FirebaseCrash/upload-sym "${JSON_FILE}"

これで動きます。

実際にassert(false)を実行しクラッシュさせた後、この行を消して再起動すると・・

f:id:deeptoneworks:20161121144703p:plain

クラッシュ情報が送信できました!
20分程度経つと、無事ダッシュボードにも反映されました。よかった。

参考

ios - Firebase upload symbol files build error: Unexpected argument 'ServiceAccount.json' - Stack Overflow

Google Developers Japan: iOS 10、Xcode 8、Swift 3

[iOS 10] Xcode8のシミュレーターでは、Keychainへのアクセスがエラーとなる | Developers.IO