Swift2.0 Multipeer Connectivity 无法连接
Posted
技术标签:
【中文标题】Swift2.0 Multipeer Connectivity 无法连接【英文标题】:Swift2.0 Multipeer Connectivity Cannot Connect 【发布时间】:2016-03-27 10:30:16 【问题描述】:我有一个问题,我制作了一个小型 Multipeer Connectivity 应用 a tutorial 我稍微重写了代码,以便它可以与 swift 2.0 一起使用。 一切正常,但如果我从 iPhone 6s 向我的模拟发送邀请,它不会连接,为什么?
代码:
import MultipeerConnectivity
import UIKit
class ViewController: UIViewController, MCSessionDelegate, MCBrowserViewControllerDelegate
var browser : MCBrowserViewController!
var assistant : MCAdvertiserAssistant!
var session : MCSession!
var peerID: MCPeerID!
@IBOutlet var chatView: UITextView!
@IBOutlet var messageField: UITextField!
override func viewDidLoad()
super.viewDidLoad()
self.peerID = MCPeerID(displayName: UIDevice.currentDevice().name)
print( UIDevice.currentDevice().name )
//self.session = MCSession(peer: peerID, securityIdentity: nil, encryptionPreference: .Required)
self.session = MCSession(peer: self.peerID)
self.session.delegate = self
// create the browser viewcontroller with a unique service name
self.browser = MCBrowserViewController(serviceType: "LCOC-Chat", session: self.session)
self.browser.delegate = self
// tell the assistant to start advertising our fabulous chat
self.assistant = MCAdvertiserAssistant(serviceType:"LCOC-Chat", discoveryInfo:nil, session:self.session)
self.assistant.start()
override func didReceiveMemoryWarning()
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
@IBAction func showBrowser(sender: UIButton)
self.presentViewController(self.browser, animated: true, completion: nil)
@IBAction func sendChat(sender: UIButton)
if self.session.connectedPeers.count > 0
if let msg = self.messageField.text!.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)
// 3
do
try self.session.sendData(msg, toPeers: self.session.connectedPeers, withMode: MCSessionSendDataMode.Unreliable)
catch let error as NSError
let ac = UIAlertController(title: "Send error", message: error.localizedDescription, preferredStyle: .Alert)
ac.addAction(UIAlertAction(title: "OK", style: .Default, handler: nil))
presentViewController(ac, animated: true, completion: nil)
//-----------------this might ne3ed to be UNCOMMENTED!!!
//self.updateChat(self.messageField.text!, fromPeer: self.peerID)
self.messageField.text = ""
func updateChat(text : String, fromPeer peerID: MCPeerID)
// Appends some text to the chat view
print("Incoming msg: \(text)")
// If this peer ID is the local device's peer ID, then show the name
// as "Me"
var name : String
switch peerID
case self.peerID:
name = "Me"
default:
name = peerID.displayName
// Add the name to the message and display it
let message = "\(name): \(text)\n"
self.chatView.text = self.chatView.text + message
func browserViewControllerDidFinish(browserViewController: MCBrowserViewController)
dismissViewControllerAnimated(true, completion: nil)
func browserViewControllerWasCancelled(browserViewController: MCBrowserViewController)
dismissViewControllerAnimated(true, completion: nil)
func browserViewController(browserViewController: MCBrowserViewController, shouldPresentNearbyPeer peerID: MCPeerID, withDiscoveryInfo info: [String : String]?) -> Bool
return true
//---------------------------------------Session
func session(session: MCSession, didReceiveData data: NSData, fromPeer peerID: MCPeerID)
// Called when a peer sends an NSData to us
// This needs to run on the main queue
dispatch_async(dispatch_get_main_queue())
print("Incoming data:::")
//let msg = NSString(data: data, encoding: NSUTF8StringEncoding)
let msg = String(data: data, encoding: NSUTF8StringEncoding)!
self.updateChat(msg, fromPeer: peerID)
func session(session: MCSession, peer peerID: MCPeerID, didChangeState state: MCSessionState)
switch state
case MCSessionState.Connected:
print("Connected: \(peerID.displayName)")
case MCSessionState.Connecting:
print("Connecting: \(peerID.displayName)")
case MCSessionState.NotConnected:
print("Not Connected: \(peerID.displayName)")
// The following methods do nothing, but the MCSessionDelegate protocol
// requires that we implement them.
func session(session: MCSession, didStartReceivingResourceWithName resourceName: String, fromPeer peerID: MCPeerID, withProgress progress: NSProgress)
func session(session: MCSession, didFinishReceivingResourceWithName resourceName: String, fromPeer peerID: MCPeerID, atURL localURL: NSURL, withError error: NSError?)
func session(session: MCSession, didReceiveStream stream: NSInputStream, withName streamName: String, fromPeer peerID: MCPeerID)
【问题讨论】:
multipeer connectivity test on iphone and a simulator simultaneously的可能重复 【参考方案1】:multiplier 可以通过 蓝牙/wifi 连接。如果您有相同的 wifi 网络两个设备(iPhone 和模拟器)。它可以连接。
但如果您在两个设备中都没有 wifi。 multiplier 将尝试通过蓝牙建立连接。由于模拟器不不支持蓝牙,所以这种情况下 multiplier 连接可能失败
【讨论】:
【参考方案2】:让我稍微改一下 Jamil 的回应……如果 iPhone 开启了蓝牙,它将使用蓝牙而不是 WiFi。由于模拟器不支持蓝牙,所以无法连接。尝试在关闭蓝牙的情况下连接。
【讨论】:
以上是关于Swift2.0 Multipeer Connectivity 无法连接的主要内容,如果未能解决你的问题,请参考以下文章
MultiPeer 框架避免使用 MCBrowserView?
如何使用swift 4查看Multipeer Connectivity框架中的MCBrowserViewController
使用 Multipeer Connectivity 发送图像问题