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

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

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

Bluetoothとは短距離の間でデータを交換するための無線通信規格である。固定・モバイル両方のデバイスから、短波の電波送信を行うことで、高いセキュリティをもつパーソナルエリアネットワーク(PAN)を構築する。

iOS

iOSとは、Apple製のスマートフォンであるiPhoneやタブレット端末のiPadに搭載しているオペレーションシステム(OS)です。その他にもiPod touch・Apple TVにも搭載されています。

Swift

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

Q&A

0回答

377閲覧

MultipeerConnectivityのデータの送受信について

voubu

総合スコア6

Bluetooth

Bluetoothとは短距離の間でデータを交換するための無線通信規格である。固定・モバイル両方のデバイスから、短波の電波送信を行うことで、高いセキュリティをもつパーソナルエリアネットワーク(PAN)を構築する。

iOS

iOSとは、Apple製のスマートフォンであるiPhoneやタブレット端末のiPadに搭載しているオペレーションシステム(OS)です。その他にもiPod touch・Apple TVにも搭載されています。

Swift

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

0グッド

0クリップ

投稿2018/02/26 00:28

編集2018/02/26 02:57

MultipeerConnectivityでプロフィールアプリを作っているのですが、データは一つしか送受信できないのでしょうか?
UIimageとString型のtextを一緒に送信して受け取ろうとしているのですが、受け取れたとしてもtextのみ表示されます。
どうすれば2つ以上のデータを送受信できるのでしょうか?

swift

1import UIKit 2import MultipeerConnectivity 3 4var proImage = UIImage() 5var image = UIImage() 6var nameText = String() 7 8class TableViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, MCBrowserViewControllerDelegate, MCSessionDelegate { 9 10 let serviceType = "LCOC-Chat" 11 12 var browser : MCBrowserViewController! 13 var assistant : MCAdvertiserAssistant! 14 var session : MCSession! 15 var peerID: MCPeerID! 16 17@IBOutlet weak var tableView: UITableView! 18 19 override func viewDidLoad() { 20 super.viewDidLoad() 21 // Do any additional setup after loading the view. 22 tableView.delegate = self 23 tableView.dataSource = self 24 25 self.peerID = MCPeerID(displayName: UIDevice.current.name) 26 self.session = MCSession(peer: peerID) 27 self.session.delegate = self 28 29 // create the browser viewcontroller with a unique service name 30 self.browser = MCBrowserViewController(serviceType:serviceType, 31 session:self.session) 32 33 self.browser.delegate = self; 34 35 self.assistant = MCAdvertiserAssistant(serviceType:serviceType, 36 discoveryInfo:nil, session:self.session) 37 38 // tell the assistant to start advertising our fabulous chat 39 self.assistant.start() 40 } 41 42 override func didReceiveMemoryWarning() { 43 super.didReceiveMemoryWarning() 44 // Dispose of any resources that can be recreated. 45 } 46 47 override func viewDidAppear(_ animated: Bool) { 48 tableView.reloadData() 49 } 50 51 @IBAction func searchBtn(_ sender: Any) { 52 // Bundle up the text in the message field, and send it off to all 53 // connected peers 54 55 let dataImage = UIImagePNGRepresentation(proImage) as! NSData 56 let dataText = nameText.data(using: String.Encoding.utf8, allowLossyConversion: false) 57 let databirthplace = birthplaceText.data(using: String.Encoding.utf8, allowLossyConversion: false) 58 let dataskill = skillText.data(using: String.Encoding.utf8) 59 let dataself = selfText.data(using: String.Encoding.utf8) 60 61 62 do { 63 try session.send(dataImage as Data, toPeers: session.connectedPeers, with: .unreliable) 64 } catch let error as NSError { 65 print("Error sending data: (error.localizedDescription)") 66 } 67 68 69 do { 70 try session.send(dataText!, toPeers: session.connectedPeers, with: .unreliable) 71 } catch let error as NSError { 72 print("Error sending data: (error.localizedDescription)") 73 } 74 } 75 76@IBAction func showBrowser(_ sender: Any) { 77 self.present(self.browser, animated: true, completion: nil) 78 } 79 80 func browserViewControllerDidFinish( 81 _ browserViewController: MCBrowserViewController) { 82 // Called when the browser view controller is dismissed (ie the Done 83 // button was tapped) 84 85 self.dismiss(animated: true, completion: nil) 86 } 87 88 func browserViewControllerWasCancelled( 89 _ browserViewController: MCBrowserViewController) { 90 // Called when the browser view controller is cancelled 91 92 self.dismiss(animated: true, completion: nil) 93 } 94 95 func session(_ session: MCSession, didReceive data: Data, 96 fromPeer peerID: MCPeerID) { 97 // Called when a peer sends an NSData to us 98 99 // This needs to run on the main queue 100 DispatchQueue.main.async { 101 102 let dataText: String = NSString(data: data as Data, encoding: String.Encoding.utf8.rawValue) as! String 103 104 //image = dataImage 105 text = dataText 106 //birthplace = databirthplace 107 //skill = dataskill 108 //selfs = dataself 109 110 } 111 } 112 113 // The following methods do nothing, but the MCSessionDelegate protocol 114 // requires that we implement them. 115 func session(_ session: MCSession, 116 didStartReceivingResourceWithName resourceName: String, 117 fromPeer peerID: MCPeerID, with progress: Progress) { 118 119 // Called when a peer starts sending a file to us 120 } 121 122 func session(_ session: MCSession, 123 didFinishReceivingResourceWithName resourceName: String, 124 fromPeer peerID: MCPeerID, 125 at localURL: URL?, withError error: Error?) { 126 // Called when a file has finished transferring from another peer 127 } 128 129 func session(_ session: MCSession, didReceive stream: InputStream, 130 withName streamName: String, fromPeer peerID: MCPeerID) { 131 // Called when a peer establishes a stream with us 132 } 133 134 func session(_ session: MCSession, peer peerID: MCPeerID, 135 didChange state: MCSessionState) { 136 // Called when a connected peer changes state (for example, goes offline) 137 138 } 139 140 // セルの個数を指定するデリゲートメソッド(必須) 141 func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 142 return exchangeProfile.count 143 } 144 145 // セルに値を設定するデータソースメソッド(必須) 146 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 147 return UITableViewCell() 148 } 149 150 /// セルが選択された時に呼ばれるデリゲートメソッド 151 func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 152 performSegue(withIdentifier: "details", sender: nil) 153 } 154}

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

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

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

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

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

fuzzball

2018/02/28 01:09

session(_:didReceive:fromPeer:) が一度しか呼ばれないのでしょうか?
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだ回答がついていません

会員登録して回答してみよう

アカウントをお持ちの方は

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問