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

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

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

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

Swift

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

iPhone

iPhoneとは、アップル社が開発・販売しているスマートフォンです。 同社のデジタルオーディオプレーヤーiPodの機能、電話機能、インターネットやメールなどのWeb通信機能の3つをドッキングした機器です。

Q&A

解決済

2回答

691閲覧

MessageKitを使用したチャットアプリでのテキスト送信について

shisen-t

総合スコア21

Xcode

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

Swift

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

iPhone

iPhoneとは、アップル社が開発・販売しているスマートフォンです。 同社のデジタルオーディオプレーヤーiPodの機能、電話機能、インターネットやメールなどのWeb通信機能の3つをドッキングした機器です。

0グッド

0クリップ

投稿2019/05/29 01:27

MessageKItをインストールしてチャットアプリの動きを見ようとしています。
シミュレータを用いてビルドをしてみたのですが、テキスト入力欄に文字を入力することができてもその後ボタンをタップして送信をすることができません。
調べたコードを参考にしつつ下記のコードで進めているのですが、送信できない理由等お分かりになる方がいらっしゃいましたら、教えていただけませんか。

ViewController

1コード 2import UIKit 3import MessageKit 4import SwiftyJSON 5import InputBarAccessoryView 6 7class ViewController: MessagesViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate { 8 9 var messageList: [MockMessage] = [] 10 11 lazy var formatter: DateFormatter = { 12 let formatter = DateFormatter() 13 formatter.dateStyle = .medium 14 return formatter 15 }() 16 17 override func viewDidLoad() { 18 super.viewDidLoad() 19 20 DispatchQueue.main.async { 21 // messageListにメッセージの配列をいれて 22 self.messageList = self.getMessages() 23 // messagesCollectionViewをリロードして 24 self.messagesCollectionView.reloadData() 25 // 一番下までスクロールする 26 self.messagesCollectionView.scrollToBottom() 27 } 28 29 messagesCollectionView.messagesDataSource = self 30 messagesCollectionView.messagesLayoutDelegate = self 31 messagesCollectionView.messagesDisplayDelegate = self 32 messagesCollectionView.messageCellDelegate = self 33 34 messageInputBar.delegate = self 35 messageInputBar.sendButton.tintColor = UIColor.lightGray 36 37 38 // メッセージ入力欄の左に画像選択ボタンを追加 39 let items = [ 40 makeButton(named: "clip.png").onTextViewDidChange { button, textView in 41 button.tintColor = UIColor.lightGray 42 button.isEnabled = textView.text.isEmpty 43 } 44 ] 45 items.forEach { $0.tintColor = .lightGray } 46 messageInputBar.setStackViewItems(items, forStack: .left, animated: false) 47 messageInputBar.setLeftStackViewWidthConstant(to: 45, animated: false) 48 49 50 // メッセージ入力時に一番下までスクロール 51 scrollsToBottomOnKeyboardBeginsEditing = true // default false 52 maintainPositionOnKeyboardFrameChanged = true // default false 53 54 55 } 56 57 // ボタンの作成 58 func makeButton(named: String) -> InputBarButtonItem { 59 return InputBarButtonItem() 60 .configure { 61 $0.spacing = .fixed(10) 62 $0.image = UIImage(named: named)?.withRenderingMode(.alwaysTemplate) 63 $0.setSize(CGSize(width: 30, height: 30), animated: true) 64 }.onSelected { 65 $0.tintColor = UIColor.gray 66 }.onDeselected { 67 $0.tintColor = UIColor.lightGray 68 }.onTouchUpInside { _ in 69 print("Item Tapped") 70 } 71 } 72 73 // サンプル用に適当なメッセージ 74 func getMessages() -> [MockMessage] { 75 return [ 76 createMessage(text: "静岡県"), 77 ] 78 } 79 80 func createMessage(text: String) -> MockMessage { 81 let attributedText = NSAttributedString(string: text, attributes: [.font: UIFont.systemFont(ofSize: 15), 82 .foregroundColor: UIColor.black]) 83 return MockMessage(attributedText: attributedText, sender: otherSender(), messageId: UUID().uuidString, date: Date()) 84 } 85 86 87 override func didReceiveMemoryWarning() { 88 super.didReceiveMemoryWarning() 89 } 90} 91 92extension ViewController: MessagesDataSource { 93 func currentSender() -> SenderType { 94 return Sender(id: "123", displayName: "自分") 95 } 96 97 98 func otherSender() -> Sender { 99 return Sender(id: "456", displayName: "相手") 100 } 101 102 func numberOfSections(in messagesCollectionView: MessagesCollectionView) -> Int { 103 return messageList.count 104 } 105 106 func messageForItem(at indexPath: IndexPath, in messagesCollectionView: MessagesCollectionView) -> MessageType { 107 return messageList[indexPath.section] 108 } 109 110 // メッセージの上に文字を表示 111 func cellTopLabelAttributedText(for message: MessageType, at indexPath: IndexPath) -> NSAttributedString? { 112 if indexPath.section % 3 == 0 { 113 return NSAttributedString( 114 string: MessageKitDateFormatter.shared.string(from: message.sentDate), 115 attributes: [NSAttributedString.Key.font: UIFont.boldSystemFont(ofSize: 10), 116 NSAttributedString.Key.foregroundColor: UIColor.darkGray] 117 ) 118 } 119 return nil 120 } 121 122 // メッセージの上に文字を表示(名前) 123 func messageTopLabelAttributedText(for message: MessageType, at indexPath: IndexPath) -> NSAttributedString? { 124 let name = message.sender.displayName 125 return NSAttributedString(string: name, attributes: [NSAttributedString.Key.font: UIFont.preferredFont(forTextStyle: .caption1)]) 126 } 127 128 // メッセージの下に文字を表示(日付) 129 func messageBottomLabelAttributedText(for message: MessageType, at indexPath: IndexPath) -> NSAttributedString? { 130 let dateString = formatter.string(from: message.sentDate) 131 return NSAttributedString(string: dateString, attributes: [NSAttributedString.Key.font: UIFont.preferredFont(forTextStyle: .caption2)]) 132 } 133} 134 135// メッセージのdelegate 136extension ViewController: MessagesDisplayDelegate { 137 138 // メッセージの色を変更(デフォルトは自分:白、相手:黒) 139 func textColor(for message: MessageType, at indexPath: IndexPath, in messagesCollectionView: MessagesCollectionView) -> UIColor { 140 return isFromCurrentSender(message: message) ? .white : .darkText 141 } 142 143 // メッセージの背景色を変更している(デフォルトは自分:緑、相手:グレー) 144 func backgroundColor(for message: MessageType, at indexPath: IndexPath, in messagesCollectionView: MessagesCollectionView) -> UIColor { 145 return isFromCurrentSender(message: message) ? 146 UIColor(red: 69/255, green: 193/255, blue: 89/255, alpha: 1) : 147 UIColor(red: 230/255, green: 230/255, blue: 230/255, alpha: 1) 148 } 149 150 // メッセージの枠を吹き出しにする 151 func messageStyle(for message: MessageType, at indexPath: IndexPath, in messagesCollectionView: MessagesCollectionView) -> MessageStyle { 152 let corner: MessageStyle.TailCorner = isFromCurrentSender(message: message) ? .bottomRight : .bottomLeft 153 return .bubbleTail(corner, .curved) 154 } 155 156 // アイコンをセット 157 func configureAvatarView(_ avatarView: AvatarView, for message: MessageType, at indexPath: IndexPath, in messagesCollectionView: MessagesCollectionView) { 158 159 let avatar = Avatar(initials: "人") 160 avatarView.set(avatar: avatar) 161 } 162} 163 164 165// 各ラベルの高さを設定(デフォルト0なので必須) 166extension ViewController: MessagesLayoutDelegate { 167 168 func cellTopLabelHeight(for message: MessageType, at indexPath: IndexPath, in messagesCollectionView: MessagesCollectionView) -> CGFloat { 169 if indexPath.section % 3 == 0 { return 10 } 170 return 0 171 } 172 173 func messageTopLabelHeight(for message: MessageType, at indexPath: IndexPath, in messagesCollectionView: MessagesCollectionView) -> CGFloat { 174 return 16 175 } 176 177 func messageBottomLabelHeight(for message: MessageType, at indexPath: IndexPath, in messagesCollectionView: MessagesCollectionView) -> CGFloat { 178 return 16 179 } 180} 181 182extension ViewController: MessageCellDelegate { 183 // メッセージをタップした時 184 func didTapMessage(in cell: MessageCollectionViewCell) { 185 print("Message tapped") 186 } 187} 188 189extension ViewController: MessageInputBarDelegate { 190 // メッセージ送信ボタンをタップした時 191 func messageInputBar(_ inputBar: MessageInputBar, didPressSendButtonWith text: String) { 192 for component in inputBar.inputTextView.components { 193 if let image = component as? UIImage { 194 195 let imageMessage = MockMessage(image: image, sender: currentSender() , messageId: UUID().uuidString, date: Date()) 196 messageList.append(imageMessage) 197 messagesCollectionView.insertSections([messageList.count - 1]) 198 199 } else if let text = component as? String { 200 201 let attributedText = NSAttributedString(string: text, attributes: [.font: UIFont.systemFont(ofSize: 15), 202 .foregroundColor: UIColor.white]) 203 let message = MockMessage(attributedText: attributedText, sender: currentSender() , messageId: UUID().uuidString, date: Date()) 204 messageList.append(message) 205 messagesCollectionView.insertSections([messageList.count - 1]) 206 } 207 } 208 inputBar.inputTextView.text = String() 209 messagesCollectionView.scrollToBottom() 210 } 211}

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

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

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

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

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

guest

回答2

0

ベストアンサー

ご存知かもしれませんが、送信ボタンに関する記述は MessageInputBarDelegate内で行っています。
このような記述だと、messageListにmessageをappend(追加)しただけの状態です。messageListの再表示もしてない上、Mockmessageも初期化で”静岡県”が出てるだけです。

解決策としては、自作のDB(データベース)を作成して取り出す方法があるかと思います。
リサーチしてみると、Firebaseなどでリアルタイムで行えるので高速化できるみたいです。ぜひ調べてみてください。

投稿2019/12/27 05:33

KakeruMasuda

総合スコア74

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

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

0

MessageKItのバージョンでは?

https://teratail.com/questions/187805#reply-327497

投稿2019/11/18 05:08

smapira

総合スコア33

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問