For multipeer connectivity is there any way to customize the advertiser invitation? The UIalert that says "iPhone user would like to connect" I want to change it to say something else? I've tried it but I keep getting errors.
My code is below I separated the custom invitation at the bottom.
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) {
}
}
VIEW CONTROLLER:
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
}
}
MCAdvertiserAssistant
is an high-level class. It implements most of the low-level operations itself, including the invitation alert. In order to create a custom invitation alert, you must use MCNearbyServiceAdvertiser
instead as advertiser.
Define advertiser:
var advertiser: MCNearbyServiceAdvertiser!
Initialize:
advertiser = MCNearbyServiceAdvertiser(peer: peerID, discoveryInfo: nil, serviceType: "hello-world")
advertiser.delegate = self
advertiser.startAdvertisingPeer()
And present your alert in delegate function:
func advertiser(_ advertiser: MCNearbyServiceAdvertiser, didReceiveInvitationFromPeer peerID: MCPeerID, withContext context: Data?, invitationHandler: @escaping (Bool, MCSession?) -> Void) {
// Show custom alert here
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.