多人连接自定义邀请

Posted

技术标签:

【中文标题】多人连接自定义邀请【英文标题】:Multipeer connectivity custom invitation 【发布时间】:2017-05-23 04:33:27 【问题描述】:

对于多人连接,是否有任何方法可以自定义广告客户邀请? UIalert 说“iPhone 用户想连接”我想把它改成说别的吗?我已经尝试过了,但我不断收到错误。

我的代码在下方,我在底部分隔了自定义邀请。

MPC 处理程序:

class MPCHANDLER: NSObject, MCSessionDelegate 

    var peerID:MCPeerID!
    var session:MCSession!
    var browser:MCBrowserViewController!
    var advertiser:MCAdvertiserAssistant? = nil
    var invitationHandler: ((Bool, MCSession?)->Void)!

    func setupPeerWithDisplayName (_ displayName:String)
        peerID = MCPeerID(displayName: displayName)
    

    func setupSession()
        session = MCSession(peer: peerID)
        session.delegate = self
    

    func setupBrowser()
        browser = MCBrowserViewController(serviceType: "game", session: session)

    

    func advertiseSelf(_ advertise:Bool)
        if advertise
            advertiser = MCAdvertiserAssistant(serviceType: "game", discoveryInfo: nil, session: session)
            advertiser!.start()
        else
            advertiser!.stop()
            advertiser = nil
        
    


    func session(_ session: MCSession, peer peerID: MCPeerID, didChange state: MCSessionState) 
        let userInfo = ["peerID":peerID,"state":state.rawValue] as [String : Any]
        DispatchQueue.main.async(execute:  () -> Void in
            NotificationCenter.default.post(name: Notification.Name(rawValue: "MPC_DidChangeStateNotification"), object: nil, userInfo: userInfo)
        )

    

    func session(_ session: MCSession, didReceive data: Data, fromPeer peerID: MCPeerID) 
        let userInfo = ["data":data, "peerID":peerID] as [String : Any]
        DispatchQueue.main.async(execute:  () -> Void in
            NotificationCenter.default.post(name: Notification.Name(rawValue: "MPC_DidReceiveDataNotification"), object: nil, userInfo: userInfo)
        )

    

    func session(_ session: MCSession, didFinishReceivingResourceWithName resourceName: String, fromPeer peerID: MCPeerID, at localURL: URL, withError error: Error?) 

    

    func session(_ session: MCSession, didStartReceivingResourceWithName resourceName: String, fromPeer peerID: MCPeerID, with progress: Progress) 

    

    func session(_ session: MCSession, didReceive stream: InputStream, withName streamName: String, fromPeer peerID: MCPeerID) 

    



查看控制器:

var currentPlayer:String!

var appDelegate:AppDelegate!

override func viewDidLoad() 
    super.viewDidLoad()


    appDelegate = UIApplication.shared.delegate as! AppDelegate
    appDelegate.MPCHandler.setupPeerWithDisplayName(UIDevice.current.name)
    appDelegate.MPCHandler.setupSession()
    appDelegate.MPCHandler.advertiseSelf(true)

    NotificationCenter.default.addObserver(self, selector: #selector(ViewController.peerChangedStateWithNotification(_:)), name: NSNotification.Name(rawValue: "MPC_DidChangeStateNotification"), object: nil)

    NotificationCenter.default.addObserver(self, selector: #selector(ViewController.handleReceivedDataWithNotification(_:)), name: NSNotification.Name(rawValue: "MPC_DidReceiveDataNotification"), object: nil)

  

@IBAction func LINK(_ sender: Any) 
    if appDelegate.MPCHandler.session != nil
        appDelegate.MPCHandler.setupBrowser()
        appDelegate.MPCHandler.browser.delegate = self

        self.present(appDelegate.MPCHandler.browser, animated: true, completion: nil)

   

           func peerChangedStateWithNotification(_ notification: Notification) 

    let userInfo = notification.userInfo!

    let state = userInfo["state"] as! Int

    if state != MCSessionState.connecting.rawValue
        self.navigationItem.title = "Connected"
    

        func handleReceivedDataWithNotification(_ notification: Notification) 
    let userInfo = notification.userInfo!
    print(userInfo)


     func browserViewControllerDidFinish(_ browserViewController: MCBrowserViewController) 
    appDelegate.MPCHandler.browser.dismiss(animated: true, completion: nil)


         func browserViewControllerWasCancelled(_ browserViewController: MCBrowserViewController) 
    appDelegate.MPCHandler.browser.dismiss(animated: true, completion: nil)

自定义邀请:

func invitationWasReceived(_ fromPeer: String) 
self.createAlert(title: "HI", message: "TAP TO CONNECT TO USER")

 func createAlert (title: String, message:String)
 
    let alert = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.alert)
    alert.addAction(UIAlertAction(title: output.text, style: UIAlertActionStyle.default, handler:  (action) in
  
  

【问题讨论】:

@Eric Aya..感谢您的编辑! :) 【参考方案1】:

MCAdvertiserAssistant 是一个高级类。它自己实现了大部分低级操作,包括邀请警报。要创建自定义邀请提醒,您必须使用 MCNearbyServiceAdvertiser 作为广告商。

定义广告客户:

var advertiser: MCNearbyServiceAdvertiser!

初始化:

advertiser = MCNearbyServiceAdvertiser(peer: peerID, discoveryInfo: nil, serviceType: "hello-world")
advertiser.delegate = self
advertiser.startAdvertisingPeer()

并在委托函数中显示您的警报:

func advertiser(_ advertiser: MCNearbyServiceAdvertiser, didReceiveInvitationFromPeer peerID: MCPeerID, withContext context: Data?, invitationHandler: @escaping (Bool, MCSession?) -> Void) 
    // Show custom alert here

【讨论】:

以上是关于多人连接自定义邀请的主要内容,如果未能解决你的问题,请参考以下文章

IOS7 多点连接使用发现信息创建自定义邀请

游戏中心对接会自定义邀请

Android技术分享| Android 自定义View多人视频通话控件

Android技术分享| Android 自定义View多人视频通话控件

Unity 中 Google Games Services 回合制多人游戏的自定义 UI

如何将自定义字段添加到 devise_invitable 邀请表单