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

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

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

Firebaseは、Googleが提供するBasSサービスの一つ。リアルタイム通知可能、並びにアクセス制御ができるオブジェクトデータベース機能を備えます。さらに認証機能、アプリケーションのログ解析機能などの利用も可能です。

Swift

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

Q&A

解決済

1回答

198閲覧

JSQMessagesViewControllerを使ってチャットを作成したい

takahiro00

総合スコア84

Firebase

Firebaseは、Googleが提供するBasSサービスの一つ。リアルタイム通知可能、並びにアクセス制御ができるオブジェクトデータベース機能を備えます。さらに認証機能、アプリケーションのログ解析機能などの利用も可能です。

Swift

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

0グッド

0クリップ

投稿2019/07/12 13:04

編集2019/07/12 13:13

前提・実現したいこと

swiftでJSQMessagesViewControllerを使ってチャット機能を作成したいのですが、
メッセージを送信して、メッセージがFirebaseに登録された後に、
AppDelegate.swiftで「signal SIGABRT」になってしまいます。
おそらくメッセージを表示する際にエラーになっていると思うのですが、

以下のサイトを参考にしています。(ほとんどコピペ)
https://www.hfoasi8fje3.work/entry/2018/05/25/191259

ご教授よろしくお願いいたします。

発生している問題・エラーメッセージ

{ from = user1; name = test; text = "\U3044\U3044\U3044"; } 2019-07-12 21:59:52.630175+0900 FirebaseTest[19386:1064265] *** Assertion failure in -[FirebaseTest.ViewController collectionView:avatarImageDataForItemAtIndexPath:], /Users/tetetete/Swift/FirebaseTest/Pods/JSQMessagesViewController/JSQMessagesViewController/Controllers/JSQMessagesViewController.m:508 2019-07-12 21:59:52.636260+0900 FirebaseTest[19386:1064265] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'ERROR: required method not implemented: -[JSQMessagesViewController collectionView:avatarImageDataForItemAtIndexPath:]' *** First throw call stack: ( 0 CoreFoundation 0x000000010dde36fb __exceptionPreprocess + 331 1 libobjc.A.dylib 0x000000010d387ac5 objc_exception_throw + 48 2 CoreFoundation 0x000000010dde3482 +[NSException raise:format:arguments:] + 98 3 Foundation 0x000000010c17c927 -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 194 4 JSQMessagesViewController 0x000000010c76aea1 -[JSQMessagesViewController collectionView:avatarImageDataForItemAtIndexPath:] + 305 5 JSQMessagesViewController 0x000000010c76bfb6 -[JSQMessagesViewController collectionView:cellForItemAtIndexPath:] + 3782 6 UIKitCore 0x0000000117557767 -[UICollectionView _createPreparedCellForItemAtIndexPath:withLayoutAttributes:applyAttributes:isFocused:notify:] + 383 7 UIKitCore 0x00000001175575e2 -[UICollectionView _createPreparedCellForItemAtIndexPath:withLayoutAttributes:applyAttributes:] + 31 8 UIKitCore 0x000000011755ca8c -[UICollectionView _updateVisibleCellsNow:] + 6120 9 UIKitCore 0x0000000117561bb6 -[UICollectionView layoutSubviews] + 365 10 UIKitCore 0x00000001181ad9c1 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 1417 11 QuartzCore 0x000000010cd91eae -[CALayer layoutSublayers] + 173 12 QuartzCore 0x000000010cd96b88 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 396 13 QuartzCore 0x000000010cda2ee4 _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 72 14 QuartzCore 0x000000010cd123aa _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 328 15 QuartzCore 0x000000010cd49584 _ZN2CA11Transaction6commitEv + 608 16 QuartzCore 0x000000010cd49ede _ZN2CA11Transaction17observer_callbackEP19__CFRunLoopObservermPv + 76 17 CoreFoundation 0x000000010dd4a0f7 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 23 18 CoreFoundation 0x000000010dd445be __CFRunLoopDoObservers + 430 19 CoreFoundation 0x000000010dd44c31 __CFRunLoopRun + 1505 20 CoreFoundation 0x000000010dd44302 CFRunLoopRunSpecific + 626 21 GraphicsServices 0x0000000110b9b2fe GSEventRunModal + 65 22 UIKitCore 0x0000000117cdfba2 UIApplicationMain + 140 23 FirebaseTest 0x0000000109dc6a1b main + 75 24 libdyld.dylib 0x000000010ef5a541 start + 1 ) libc++abi.dylib: terminating with uncaught exception of type NSException (lldb)

該当のソースコード

swift

1import UIKit 2import Firebase 3 4@UIApplicationMain 5class AppDelegate: UIResponder, UIApplicationDelegate { 6 7 var window: UIWindow? 8 9 func application(_ application: UIApplication, 10 didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) 11 -> Bool { 12 FirebaseApp.configure() 13 return true 14 } 15 16 func applicationWillResignActive(_ application: UIApplication) { 17 // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. 18 // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game. 19 } 20 21 func applicationDidEnterBackground(_ application: UIApplication) { 22 // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 23 // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. 24 } 25 26 func applicationWillEnterForeground(_ application: UIApplication) { 27 // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background. 28 } 29 30 func applicationDidBecomeActive(_ application: UIApplication) { 31 // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface. 32 } 33 34 func applicationWillTerminate(_ application: UIApplication) { 35 // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. 36 } 37 38 39} 40

swift

1import UIKit 2import Firebase 3import JSQMessagesViewController 4 5class ViewController: JSQMessagesViewController { 6 7 // データベースへの参照を定義 8 var ref: DatabaseReference! 9 10 // メッセージ内容に関するプロパティ 11 var messages: [JSQMessage]? 12 // 背景画像に関するプロパティ 13 var incomingBubble: JSQMessagesBubbleImage! 14 var outgoingBubble: JSQMessagesBubbleImage! 15 16 func setupFirebase() { 17 // DatabaseReferenceのインスタンス化 18 ref = Database.database().reference() 19 20 // 最新25件のデータをデータベースから取得する 21 // 最新のデータが追加されるたびに最新データを取得する 22 ref.queryLimited(toLast: 25).observe(DataEventType.childAdded, with: { (snapshot) -> Void in 23 let snapshotValue = snapshot.value as! NSDictionary 24 let text = snapshotValue["text"] as! String 25 let sender = snapshotValue["from"] as! String 26 let name = snapshotValue["name"] as! String 27 print(snapshot.value!) 28 let message = JSQMessage(senderId: sender, displayName: name, text: text) 29 self.messages?.append(message!) 30 self.finishSendingMessage() 31 }) 32 } 33 34 override func viewDidLoad() { 35 super.viewDidLoad() 36 // Do any additional setup after loading the view, typically from a nib. 37 38 // クリーンアップツールバーの設定 39 inputToolbar!.contentView!.leftBarButtonItem = nil 40 // 新しいメッセージを受信するたびに下にスクロールする 41 automaticallyScrollsToMostRecentMessage = true 42 43 // 自分のsenderId, senderDisplayNameを設定 44 self.senderId = "user1" 45 self.senderDisplayName = "test" 46 47 // 吹き出しの設定 48 let bubbleFactory = JSQMessagesBubbleImageFactory() 49 self.incomingBubble = bubbleFactory?.incomingMessagesBubbleImage(with: UIColor.jsq_messageBubbleLightGray()) 50 self.outgoingBubble = bubbleFactory?.outgoingMessagesBubbleImage(with: UIColor.jsq_messageBubbleBlue()) 51 52 //メッセージデータの配列を初期化 53 self.messages = [] 54 setupFirebase() 55 } 56 57 override func didReceiveMemoryWarning() { 58 super.didReceiveMemoryWarning() 59 // Dispose of any resources that can be recreated. 60 } 61 62 // Sendボタンが押された時に呼ばれるメソッド 63 override func didPressSend(_ button: UIButton, withMessageText text: String, senderId: String, senderDisplayName: String, date: Date) { 64 65 //メッセージの送信処理を完了する(画面上にメッセージが表示される) 66 self.finishReceivingMessage(animated: true) 67 68 //firebaseにデータを送信、保存する 69 let post1 = ["from": senderId, "name": senderDisplayName, "text":text] 70 let post1Ref = ref.childByAutoId() 71 post1Ref.setValue(post1) 72 self.finishSendingMessage(animated: true) 73 74 //キーボードを閉じる 75 self.view.endEditing(true) 76 } 77 78 // アイテムごとに参照するメッセージデータを返す 79 override func collectionView(_ collectionView: JSQMessagesCollectionView, messageDataForItemAt indexPath: IndexPath) -> JSQMessageData { 80 return messages![indexPath.item] 81 } 82 83 // アイテムごとのMessageBubble(背景)を返す 84 override func collectionView(_ collectionView: JSQMessagesCollectionView, messageBubbleImageDataForItemAt indexPath: IndexPath) -> JSQMessageBubbleImageDataSource { 85 let message = self.messages?[indexPath.item] 86 if message?.senderId == self.senderId { 87 return self.outgoingBubble 88 } 89 return self.incomingBubble 90 } 91 92 // アイテムの総数を返す 93 override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 94 return messages!.count 95 } 96} 97

補足情報(FW/ツールのバージョンなど)

言語:Swift4
DB:Firebase

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

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

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

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

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

guest

回答1

0

ベストアンサー

こちらのライブラリは廃止されているようです。
Officially deprecating JSQMessagesViewController

投稿2019/07/12 13:54

fathy

総合スコア254

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

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

takahiro00

2019/07/12 15:56

え??使えないってことですか??
fathy

2019/07/12 16:00

開発者の方がもう更新しないという旨のブログを2年前に公開されています。 ご自身でライブラリの中身を対応させて使うこともできますが,他のライブラリを使った方が早いかと思います。
fathy

2019/07/12 16:13

MessageKit/MessageKit これがJSQMessagesViewControllerの代替のライブラリのようです。 すみません,間違って回答の方に送ってしまったので削除して置いてください。
takahiro00

2019/07/13 01:11

そうなのですね。。。。 MessageKit/MessageKitで作り直します。 ありがとうございました。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.50%

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

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

質問する

関連した質問