对于多重连接,有没有办法自定义广告客户邀请? UIalert说" iPhone用户想要连接"我想改变它来说别的吗?我已经尝试过,但我一直都会遇到错误。
我的代码在下方,我将底部的自定义邀请分开。
MPC HANDLER:
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)
}
CUSTOM INVITATION:
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
}
}
答案 0 :(得分:0)
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
}