IOS 多点连接 - 只发送我的对象的一部分
IOS Multipeer Connectivity - Only sending part of my object
我创建了一个对象,该对象具有多个要发送的属性:1 个字符串 (sessiondate)、另一个对象的数组、1 个时间间隔和 1 个 Int (sessiondistance)。我使用多路连接发送这个对象,设备注册它们正在接收数据。但是,未收到整数。它在接收设备上变为零。
感谢您的帮助!
This is the relevant code
func session(_ session: MCSession, peer peerID: MCPeerID, didChange state: MCSessionState) {
切换状态{
案例MCSessionState.connected:
打印("Connected: (peerID.displayName)")
joinedSession = true
let hosting = ViewController().hostCheck()
print("InMCSessionConnected")
if(hosting == false){
}
if(hosting == true){
if(self.ConnectedUsers.text?.range(of: peerID.displayName) == nil){
DispatchQueue.main.async {
self.ConnectedUsers.text?.append(peerID.displayName + ", ")
}
}
HostingSession.noJoined = HostingSession.noJoined + 1;
print(HostingSession.noJoined)
print(SessionsManager.SessionInception.last?.SessionDistance)
print(SessionsManager.SessionInception.last?.SessionDate)
print("SendingData")
sendData(SessionsManager.SessionInception.last!)
print("SendingData")
}
case MCSessionState.connecting:
print("Connecting: \(peerID.displayName)")
case MCSessionState.notConnected:
print("Not Connected: \(peerID.displayName)")
joinedSession = false
}
}
func session(_ session: MCSession, didReceive data: Data, fromPeer peerID: MCPeerID) {
//This is called when data arrives
print("Starting Received Data")
let hosting = ViewController().hostCheck()
if(hosting == false){
let SessionSent = (NSKeyedUnarchiver.unarchiveObject(with: data) as! Sessions)
print(SessionSent.SessionDistance)
SessionsManager.SessionInception.append(SessionSent)//appends the session sent
//2 method gets the current date and line below appends this date to title
let currentSession = SessionsManager.SessionInception.last
let currentSessionDate = (currentSession?.SessionDate)!
print(currentSessionDate)
reloadView()
//Creates the table
TableView.rowHeight = 90
TableView.delegate = self
TableView.dataSource = self
TableView.reloadData()
print("Finished Received Data")
//ReadyButton.enabled = true
}
}
所以最后我通过单独发送对象的每个部分来完成这个工作。见下文:
func session(_ session: MCSession, peer peerID: MCPeerID, didChange state: MCSessionState) {
switch state {
case MCSessionState.connected:
print("Connected: \(peerID.displayName)")
joinedSession = true
let hosting = ViewController().hostCheck()
print("InMCSessionConnected")
if(hosting == false){
}
if(hosting == true){
if(self.ConnectedUsers.text?.range(of: peerID.displayName) == nil){
DispatchQueue.main.async {
self.ConnectedUsers.text?.append(peerID.displayName + ", ")
}
}
HostingSession.noJoined = HostingSession.noJoined + 1;
print(HostingSession.noJoined)
print(SessionsManager.SessionArray.last?.SessionDistance)
print(SessionsManager.SessionArray.last?.SessionDate)
print("SendingData")
sendData(SessionsManager.SessionArray.last!)
print("SendingData")
//MARK: Work around
sendDistance(SessionsManager.SessionArray.last!.SessionDistance!)
sendStartTime((SessionsManager.SessionArray.last?.startTime)!)
}
case MCSessionState.connecting:
print("Connecting: \(peerID.displayName)")
case MCSessionState.notConnected:
print("Not Connected: \(peerID.displayName)")
joinedSession = false
}
}
func session(_ session: MCSession, didReceive data: Data, fromPeer peerID: MCPeerID) {
//This is called when data arrives
print("Starting Received Data")
let hosting = ViewController().hostCheck()
if(hosting == false){
if let SessionSent = (NSKeyedUnarchiver.unarchiveObject(with: data) as? Sessions) {
SessionsManager.SessionArray.append(SessionSent)//appends the session sent
//2 method gets the current date and line below appends this date to title
let currentSession = SessionsManager.SessionArray.last
let currentSessionDate = (currentSession?.SessionDate)!
print(currentSessionDate)
reloadView()
//Creates the table
TableView.rowHeight = 90
TableView.delegate = self
TableView.dataSource = self
TableView.reloadData()
}//MARK: Work around
else if let distanceSent = (NSKeyedUnarchiver.unarchiveObject(with: data) as? Int) {
if (distanceSent < 100000){
SessionsManager.SessionArray.last?.SessionDistance = distanceSent
print(SessionsManager.SessionArray.last?.SessionDistance)
}else{
SessionsManager.SessionArray.last?.startTime = TimeInterval(distanceSent)
print(SessionsManager.SessionArray.last?.startTime)
}
}
print("Finished Received Data")
//ReadyButton.enabled = true
}
}
这些是发送子程序:
//MARK: Work around
func sendDistance(_ TheData: Int){
let dataToSend = NSKeyedArchiver.archivedData(withRootObject: TheData)
if mcSession.connectedPeers.count > 0 {//makes sure it is actually sending it to any peers before it attempts
do {
try mcSession.send(dataToSend, toPeers: mcSession.connectedPeers, with: .reliable)//Ensures delivery using reliable method, sends dataToSend to all peers connected on mcSession.connectedPeers
} catch let error as NSError {//Shows an error message if there is a problem
print(error)
}
}
}
func sendStartTime(_ TheData: TimeInterval){
let dataToSend = NSKeyedArchiver.archivedData(withRootObject: TheData)
if mcSession.connectedPeers.count > 0 {//makes sure it is actually sending it to any peers before it attempts
do {
try mcSession.send(dataToSend, toPeers: mcSession.connectedPeers, with: .reliable)//Ensures delivery using reliable method, sends dataToSend to all peers connected on mcSession.connectedPeers
} catch let error as NSError {//Shows an error message if there is a problem
print(error)
}
}
}
func sendData(_ TheData: NSObject){
let dataToSend = NSKeyedArchiver.archivedData(withRootObject: TheData)
if mcSession.connectedPeers.count > 0 {//makes sure it is actually sending it to any peers before it attempts
do {
try mcSession.send(dataToSend, toPeers: mcSession.connectedPeers, with: .reliable)//Ensures delivery using reliable method, sends dataToSend to all peers connected on mcSession.connectedPeers
} catch let error as NSError {//Shows an error message if there is a problem
print(error)
}
}
我创建了一个对象,该对象具有多个要发送的属性:1 个字符串 (sessiondate)、另一个对象的数组、1 个时间间隔和 1 个 Int (sessiondistance)。我使用多路连接发送这个对象,设备注册它们正在接收数据。但是,未收到整数。它在接收设备上变为零。
感谢您的帮助!
This is the relevant code
func session(_ session: MCSession, peer peerID: MCPeerID, didChange state: MCSessionState) { 切换状态{ 案例MCSessionState.connected: 打印("Connected: (peerID.displayName)")
joinedSession = true
let hosting = ViewController().hostCheck()
print("InMCSessionConnected")
if(hosting == false){
}
if(hosting == true){
if(self.ConnectedUsers.text?.range(of: peerID.displayName) == nil){
DispatchQueue.main.async {
self.ConnectedUsers.text?.append(peerID.displayName + ", ")
}
}
HostingSession.noJoined = HostingSession.noJoined + 1;
print(HostingSession.noJoined)
print(SessionsManager.SessionInception.last?.SessionDistance)
print(SessionsManager.SessionInception.last?.SessionDate)
print("SendingData")
sendData(SessionsManager.SessionInception.last!)
print("SendingData")
}
case MCSessionState.connecting:
print("Connecting: \(peerID.displayName)")
case MCSessionState.notConnected:
print("Not Connected: \(peerID.displayName)")
joinedSession = false
}
}
func session(_ session: MCSession, didReceive data: Data, fromPeer peerID: MCPeerID) {
//This is called when data arrives
print("Starting Received Data")
let hosting = ViewController().hostCheck()
if(hosting == false){
let SessionSent = (NSKeyedUnarchiver.unarchiveObject(with: data) as! Sessions)
print(SessionSent.SessionDistance)
SessionsManager.SessionInception.append(SessionSent)//appends the session sent
//2 method gets the current date and line below appends this date to title
let currentSession = SessionsManager.SessionInception.last
let currentSessionDate = (currentSession?.SessionDate)!
print(currentSessionDate)
reloadView()
//Creates the table
TableView.rowHeight = 90
TableView.delegate = self
TableView.dataSource = self
TableView.reloadData()
print("Finished Received Data")
//ReadyButton.enabled = true
}
}
所以最后我通过单独发送对象的每个部分来完成这个工作。见下文:
func session(_ session: MCSession, peer peerID: MCPeerID, didChange state: MCSessionState) {
switch state {
case MCSessionState.connected:
print("Connected: \(peerID.displayName)")
joinedSession = true
let hosting = ViewController().hostCheck()
print("InMCSessionConnected")
if(hosting == false){
}
if(hosting == true){
if(self.ConnectedUsers.text?.range(of: peerID.displayName) == nil){
DispatchQueue.main.async {
self.ConnectedUsers.text?.append(peerID.displayName + ", ")
}
}
HostingSession.noJoined = HostingSession.noJoined + 1;
print(HostingSession.noJoined)
print(SessionsManager.SessionArray.last?.SessionDistance)
print(SessionsManager.SessionArray.last?.SessionDate)
print("SendingData")
sendData(SessionsManager.SessionArray.last!)
print("SendingData")
//MARK: Work around
sendDistance(SessionsManager.SessionArray.last!.SessionDistance!)
sendStartTime((SessionsManager.SessionArray.last?.startTime)!)
}
case MCSessionState.connecting:
print("Connecting: \(peerID.displayName)")
case MCSessionState.notConnected:
print("Not Connected: \(peerID.displayName)")
joinedSession = false
}
}
func session(_ session: MCSession, didReceive data: Data, fromPeer peerID: MCPeerID) {
//This is called when data arrives
print("Starting Received Data")
let hosting = ViewController().hostCheck()
if(hosting == false){
if let SessionSent = (NSKeyedUnarchiver.unarchiveObject(with: data) as? Sessions) {
SessionsManager.SessionArray.append(SessionSent)//appends the session sent
//2 method gets the current date and line below appends this date to title
let currentSession = SessionsManager.SessionArray.last
let currentSessionDate = (currentSession?.SessionDate)!
print(currentSessionDate)
reloadView()
//Creates the table
TableView.rowHeight = 90
TableView.delegate = self
TableView.dataSource = self
TableView.reloadData()
}//MARK: Work around
else if let distanceSent = (NSKeyedUnarchiver.unarchiveObject(with: data) as? Int) {
if (distanceSent < 100000){
SessionsManager.SessionArray.last?.SessionDistance = distanceSent
print(SessionsManager.SessionArray.last?.SessionDistance)
}else{
SessionsManager.SessionArray.last?.startTime = TimeInterval(distanceSent)
print(SessionsManager.SessionArray.last?.startTime)
}
}
print("Finished Received Data")
//ReadyButton.enabled = true
}
}
这些是发送子程序:
//MARK: Work around
func sendDistance(_ TheData: Int){
let dataToSend = NSKeyedArchiver.archivedData(withRootObject: TheData)
if mcSession.connectedPeers.count > 0 {//makes sure it is actually sending it to any peers before it attempts
do {
try mcSession.send(dataToSend, toPeers: mcSession.connectedPeers, with: .reliable)//Ensures delivery using reliable method, sends dataToSend to all peers connected on mcSession.connectedPeers
} catch let error as NSError {//Shows an error message if there is a problem
print(error)
}
}
}
func sendStartTime(_ TheData: TimeInterval){
let dataToSend = NSKeyedArchiver.archivedData(withRootObject: TheData)
if mcSession.connectedPeers.count > 0 {//makes sure it is actually sending it to any peers before it attempts
do {
try mcSession.send(dataToSend, toPeers: mcSession.connectedPeers, with: .reliable)//Ensures delivery using reliable method, sends dataToSend to all peers connected on mcSession.connectedPeers
} catch let error as NSError {//Shows an error message if there is a problem
print(error)
}
}
}
func sendData(_ TheData: NSObject){
let dataToSend = NSKeyedArchiver.archivedData(withRootObject: TheData)
if mcSession.connectedPeers.count > 0 {//makes sure it is actually sending it to any peers before it attempts
do {
try mcSession.send(dataToSend, toPeers: mcSession.connectedPeers, with: .reliable)//Ensures delivery using reliable method, sends dataToSend to all peers connected on mcSession.connectedPeers
} catch let error as NSError {//Shows an error message if there is a problem
print(error)
}
}