PeerId(MCPeerID)定制

时间:2016-07-01 23:57:28

标签: ios iphone swift bluetooth multipeer-connectivity

我最近构建了一个功能齐全的多重连接应用程序,在尝试找到一种方法让我的用户更改(或创建)个人 peerId 时遇到了一些麻烦。 MCBrowserViewController 选择连接到同一wifi网络的设备,但只显示其设备名称,不允许用户创建自己的用户名。我最好怎么做呢?我尝试过 initWithDisplayName()无效,并且在Objective-C中找不到与 Swift 中写的相关的资源。

import UIKit
import MultipeerConnectivity

class ViewController: UIViewController, MCBrowserViewControllerDelegate, MCSessionDelegate {

let serviceType = "LCOC-Chat"

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)
    self.session = MCSession(peer: peerID)
    self.session.delegate = self

    // create the browser viewcontroller with a unique service name
    self.browser = MCBrowserViewController(serviceType:serviceType,
        session:self.session)

    self.browser.delegate = self;

    self.assistant = MCAdvertiserAssistant(serviceType:serviceType,
        discoveryInfo:nil, session:self.session)

    // tell the assistant to start advertising our fabulous chat
    self.assistant.start()
}

@IBAction func sendChat(sender: UIButton) {
    // Bundle up the text in the message field, and send it off to all
    // connected peers

    let msg = self.messageField.text.dataUsingEncoding(NSUTF8StringEncoding,
        allowLossyConversion: false)

    var error : NSError?

    self.session.sendData(msg, toPeers: self.session.connectedPeers,
        withMode: MCSessionSendDataMode.Unreliable, error: &error)

    if error != nil {
        print("Error sending data: \(error?.localizedDescription)")
    }

    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

    // 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

}

@IBAction func showBrowser(sender: UIButton) {
    // Show the browser view controller
    self.presentViewController(self.browser, animated: true, completion: nil)
}

func browserViewControllerDidFinish(
    browserViewController: MCBrowserViewController!)  {
    // Called when the browser view controller is dismissed (ie the Done 
    // button was tapped)

    self.dismissViewControllerAnimated(true, completion: nil)
}

func browserViewControllerWasCancelled(
    browserViewController: MCBrowserViewController!)  {
    // Called when the browser view controller is cancelled

    self.dismissViewControllerAnimated(true, completion: nil)
}

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()) {

        var msg = NSString(data: data, encoding: NSUTF8StringEncoding)

        self.updateChat(msg, fromPeer: peerID)
    }
}

// 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!)  {

    // Called when a peer starts sending a file to us
}

func session(session: MCSession!,
    didFinishReceivingResourceWithName resourceName: String!,
    fromPeer peerID: MCPeerID!,
    atURL localURL: NSURL!, withError error: NSError!)  {
    // Called when a file has finished transferring from another peer
}

func session(session: MCSession!, didReceiveStream stream: NSInputStream!,
    withName streamName: String!, fromPeer peerID: MCPeerID!)  {
    // Called when a peer establishes a stream with us
}

func session(session: MCSession!, peer peerID: MCPeerID!,
    didChangeState state: MCSessionState)  {
    // Called when a connected peer changes state (for example, goes offline)

    }
 }

1 个答案:

答案 0 :(得分:1)

正如@ paulw11上面指出的那样,远程用户只看到设备名称的原因是因为你只传入了代码的这一行中的设备名称:

    self.peerID = MCPeerID(displayName: UIDevice.currentDevice().name)

例如,如果我希望我的设备在其他人的浏览器(tableview)中显示为Xaphod,那么在我的设备上,代码必须是:

    self.peerID = MCPeerID(displayName: "Xaphod")