質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Xcode

Xcodeはソフトウェア開発のための、Appleの統合開発環境です。Mac OSXに付随するかたちで配布されています。

Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

API

APIはApplication Programming Interfaceの略です。APIはプログラムにリクエストされるサービスがどのように動作するかを、デベロッパーが定めたものです。

Q&A

解決済

1回答

386閲覧

[Swift]Googleドライブ サンプルがうごかない[Xcode9]

moe_1023

総合スコア11

Xcode

Xcodeはソフトウェア開発のための、Appleの統合開発環境です。Mac OSXに付随するかたちで配布されています。

Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

API

APIはApplication Programming Interfaceの略です。APIはプログラムにリクエストされるサービスがどのように動作するかを、デベロッパーが定めたものです。

0グッド

0クリップ

投稿2018/06/05 14:18

前提・実現したいこと

こんにちは。
作成しているアプリケーションに、保存や保存データの取得を目的にGoogleドライブAPIを利用したいと考えています。しかし、
iOS Quickstart通りにしているつもりなのですが動作してくれません。
コードはビルドできるのですが、Sign Inボタンを押したところで
SIGABRTエラーがでてしまいます。
vi Podfileの内容は以下の通りです。
イメージ説明

該当のソースコード

iOS Quickstart通りのものとなります。
手順としては、
pod init→pod Install→open testD.xcworkspace→イメージ説明
へplistを挿入→infoのURL TypesへREVERSED_CLIENT_IDをかきこむ→Clean+Builed→ソースコードを書き込む↓

■AppDelegate

Swift

1 2import GoogleSignIn 3import UIKit 4 5@UIApplicationMain 6class AppDelegate: UIResponder, UIApplicationDelegate { 7 8 var window: UIWindow? 9 10 func applicationDidFinishLaunching(_ application: UIApplication) { 11 // Initialize Google sign-in. 12 GIDSignIn.sharedInstance().clientID = "<CLIENT_IDをいれている>" 13 } 14 15 func application(_ application: UIApplication, 16 open url: URL, sourceApplication: String?, annotation: Any) -> Bool { 17 return GIDSignIn.sharedInstance().handle(url, 18 sourceApplication: sourceApplication, 19 annotation: annotation) 20 } 21 22 @available(iOS 9.0, *) 23 func application(_ app: UIApplication, open url: URL, 24 options: [UIApplicationOpenURLOptionsKey : Any]) -> Bool { 25 let sourceApplication = options[UIApplicationOpenURLOptionsKey.sourceApplication] as? String 26 let annotation = options[UIApplicationOpenURLOptionsKey.annotation] 27 return GIDSignIn.sharedInstance().handle(url, 28 sourceApplication: sourceApplication, 29 annotation: annotation) 30 } 31 32} 33

■ViewCOntroller

Swift

1import GoogleAPIClientForREST 2import GoogleSignIn 3import UIKit 4 5class ViewController: UIViewController, GIDSignInDelegate, GIDSignInUIDelegate { 6 7 8 // If modifying these scopes, delete your previously saved credentials by 9 // resetting the iOS simulator or uninstall the app. 10 private let scopes = [kGTLRAuthScopeDriveReadonly] 11 12 private let service = GTLRDriveService() 13 let signInButton = GIDSignInButton() 14 let output = UITextView() 15 16 override func viewDidLoad() { 17 super.viewDidLoad() 18 print("hoge1") 19 20 // Configure Google Sign-in. 21 GIDSignIn.sharedInstance().delegate = self 22 GIDSignIn.sharedInstance().uiDelegate = self 23 GIDSignIn.sharedInstance().scopes = scopes 24 GIDSignIn.sharedInstance().signInSilently() 25 26 // Add the sign-in button. 27 view.addSubview(signInButton) 28 29 // Add a UITextView to display output. 30 output.frame = view.bounds 31 output.isEditable = false 32 output.contentInset = UIEdgeInsets(top: 20, left: 0, bottom: 20, right: 0) 33 output.autoresizingMask = [.flexibleHeight, .flexibleWidth] 34 output.isHidden = true 35 view.addSubview(output); 36 print("hoge2") 37 } 38 39 func sign(_ signIn: GIDSignIn!, didSignInFor user: GIDGoogleUser!, 40 withError error: Error!) { 41 if let error = error { 42 print("hoge3") 43 showAlert(title: "Authentication Error", message: error.localizedDescription) 44 self.service.authorizer = nil 45 } else { 46 print("hoge4") 47 self.signInButton.isHidden = true 48 self.output.isHidden = false 49 self.service.authorizer = user.authentication.fetcherAuthorizer() 50 listFiles() 51 } 52 } 53 54 // List up to 10 files in Drive 55 func listFiles() { 56 print("hoge5") 57 let query = GTLRDriveQuery_FilesList.query() 58 query.pageSize = 10 59 service.executeQuery(query, 60 delegate: self, 61 didFinish: #selector(displayResultWithTicket(ticket:finishedWithObject:error:)) 62 ) 63 } 64 65 // Process the response and display output 66 @objc func displayResultWithTicket(ticket: GTLRServiceTicket, 67 finishedWithObject result : GTLRDrive_FileList, 68 error : NSError?) { 69 print("hoge6") 70 if let error = error { 71 showAlert(title: "Error", message: error.localizedDescription) 72 print("hoge7") 73 return 74 } 75 76 var text = ""; 77 if let files = result.files, !files.isEmpty { 78 text += "Files:\n" 79 for file in files { 80 print("hoge8") 81 text += "(file.name!) ((file.identifier!))\n" 82 } 83 } else { 84 print("hoge9") 85 text += "No files found." 86 } 87 output.text = text 88 } 89 90 91 // Helper for showing an alert 92 func showAlert(title : String, message: String) { 93 let alert = UIAlertController( 94 title: title, 95 message: message, 96 preferredStyle: UIAlertControllerStyle.alert 97 ) 98 let ok = UIAlertAction( 99 title: "OK", 100 style: UIAlertActionStyle.default, 101 handler: nil 102 ) 103 alert.addAction(ok) 104 present(alert, animated: true, completion: nil) 105 } 106} 107 108

試したこと

Printしてどこに異常があるのか確かめたところ、ボタンを押した後にViewControllerのif let error = errorの部分で、showAlert()に飛ばされていることはわかりました。
それからSIGABRTエラーになってしまいます。

ほんとうにどこが悪いのかがわからず困っています。
CocoaPodを使用したのもはじめてですので、Podfileの画像もあげさせていただきました。
ひとつきになるのは、pod initの際にiOS Quickstartではplatform8.0と記載してありましたが、サンプルコードでは9.0となっていたので、上記画像のままで実行しています。もしかするとそれが通らない理由なのでしょうか?

現在のバージョンでGoogleAPIを利用しているかたがおられましたら、参考にされたサイトや方法をお教え願えないでしょうか。
初学者の質問となり、申し訳ありませんがよろしくおねがいいたします。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

func showAlert()の中にもprint()を記載して、
どこまで実際にコードが走ってるか確認して見てはどうでしょうか?

また、showAlert()の中身に問題がないか
(見た感じないとは思いますが、)
変数に適当に値を導入して、
viewDidLoadで一度呼び出して
問題なく動くか確認して見てはどうでしょう?

自分としては、
error.localizedDescriptionがStringなのかが、
気になる所です。
それもString型の変数を用意して代入し、
printできるか確認して見てはどうですか?
(errorが出てないと確認できないかもしれませんが。。。)

投稿2018/06/05 23:28

編集2018/06/05 23:35
hameji

総合スコア1380

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

moe_1023

2018/06/07 05:40

まず、さっそくのご回答ありがとうございました。エラー文を載せていなくてもうしわけありません。 show Alret()ですが、アプリを起動した際に一度動作していました。最初と最後にprintを挿入したところ、 printhogeshowAlertStart 2018-06-07 14:23:40.651576+0900 testDrive[4045:290791] Warning: Attempt to present <UIAlertController: 0x7fb456052a00> on <testDrive.ViewController: 0x7fb45540dee0> whose view is not in the window hierarchy! printhogeshowAlertEnd なります。 Sign Inボタンを押した際はshow Alret()は起動せず、 2018-06-07 14:25:58.007050+0900 testDrive[4045:290791] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Your app is missing support for the following URL schemes: com>.googleusercontent.apps.<541254043248-br393m2f2edm3epandnlijiv65t3sqe9' *** First throw call stack: ( 0 CoreFoundation 0x000000010e5c21e6 __exceptionPreprocess + 294 1 libobjc.A.dylib 0x000000010dc57031 objc_exception_throw + 48 2 CoreFoundation 0x000000010e637975 +[NSException raise:format:] + 197 3 testDrive 0x000000010cb745f8 -[GIDSignIn signInWithOptions:] + 242 4 testDrive 0x000000010cb7864d -[GIDSignInButton pressed] + 240 5 UIKit 0x000000010f155448 -[UIApplication sendAction:to:from:forEvent:] + 83 6 UIKit 0x000000010f2d0804 -[UIControl sendAction:to:forEvent:] + 67 7 UIKit 0x000000010f2d0b21 -[UIControl _sendActionsForEvents:withEvent:] + 450 8 UIKit 0x000000010f2cfa69 -[UIControl touchesEnded:withEvent:] + 580 9 UIKit 0x000000010f1ca11f -[UIWindow _sendTouchesForEvent:] + 2729 10 UIKit 0x000000010f1cb821 -[UIWindow sendEvent:] + 4086 11 UIKit 0x000000010f16f370 -[UIApplication sendEvent:] + 352 12 UIKit 0x000000010fab057f __dispatchPreprocessedEventFromEventQueue + 2796 13 UIKit 0x000000010fab3194 __handleEventQueueInternal + 5949 14 CoreFoundation 0x000000010e564bb1 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17 15 CoreFoundation 0x000000010e5494af __CFRunLoopDoSources0 + 271 16 CoreFoundation 0x000000010e548a6f __CFRunLoopRun + 1263 17 CoreFoundation 0x000000010e54830b CFRunLoopRunSpecific + 635 18 GraphicsServices 0x000000011236ca73 GSEventRunModal + 62 19 UIKit 0x000000010f1540b7 UIApplicationMain + 159 20 testDrive 0x000000010cb6ca87 main + 55 21 libdyld.dylib 0x0000000112d6f955 start + 1 22 ??? 0x0000000000000001 0x0 + 1 ) libc++abi.dylib: terminating with uncaught exception of type NSException (lldb) となって終了します。このエラーメッセージから調べてみます。いまだ解決していないのでもしなにかお気付きの点があればお教え願えれば幸いです。
hameji

2018/06/07 09:37

whose view is not in the window hierarchy! はつまづきやすいとこですね。 呼び出し元がUIViewControllerでなければならないとこを 間違ったとこから呼び出しているよってことです。 正しく設定するにはshowAlertの引数にcontroller: UIViewControllerを追加し、 呼び出す時に、controller: selfを追加すれば改善すると思います。 https://stackoverflow.com/questions/29257670/alertcontroller-is-not-in-the-window-hierarchy 1個目の答えの class AlertHelper 以下を参考にしてください。
moe_1023

2018/06/09 01:16

ご回答ありがとうございます。早速試したところ、showAlert()が正常に動作するようになりました!ありがとうございます! しかし、以下の Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Your app is missing support for the following URL schemes: com>.googleusercontent.apps.<541254043248-br393m2f2edm3epandnlijiv65t3sqe9' が動作せず、URLタイプスキーマが根本の問題であるようです。 ターゲットプロジェクトのURLタイプにはREVERSED_CLIENT_ID(google.com...)とBundle_ID(組織名.プロジェクト名)を設定しているのですがこのようなエラーが出てしまいます。
moe_1023

2018/06/10 05:31

質問内容がタイトルより具体的になってきたので、新たに質問します。Alertの件につきまして、解決にご尽力していただきありがとうございました!
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問