我有一个问题,我用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) {
}
}
2条答案
按热度按时间mwngjboj1#
倍增器可以通过*蓝牙/wifi*连接。如果你有相同的wifi网络两个设备(iPhone和模拟器)。它可以连接。
但如果你有没有wifi在两个设备。倍增器将尝试通过蓝牙连接。由于模拟器不不支持蓝牙,所以倍增器连接可以失败这种情况
klsxnrf12#
让我稍微重述一下Jamil的回答......如果iPhone打开了蓝牙,它将使用蓝牙而不是WiFi。由于模拟器不支持蓝牙,因此无法建立连接。请尝试在关闭蓝牙的情况下连接。