我有一个实现 MCNearbyServiceAdvertiserDelegate 的类。在初始化时,如果我调用 MPCManager.advertiser.startadvertisingpeer(),我可以从客户端看到广告的对等点。我正在尝试实施一种方法来停止广告,更改广告商discoveryInfo,然后使用新的发现信息再次开始广告。
这些方法成功地停止了广告对等方,但它不会再次开始广告。你能帮我解决这个问题吗
代码详情
classMPCManager:NSObject,MCSessionDelegate,MCNearbyServiceBrowserDelegate, MCNearbyServiceAdvertiserDelegate {
var delegate: MPCManagerDelegate?
var session: MCSession!
var sessionList: [MCSession]! = []
var peer: MCPeerID!
var browser: MCNearbyServiceBrowser!
var advertiser: MCNearbyServiceAdvertiser!
var foundPeers:[NSDictionary]! = []
var invitationHandler: ((Bool, MCSession!)->Void)!
var discoveryInfo:[String: String]!
init(discoveryInfo:[String:String]!, peer:MCPeerID!) {
super.init()
self.discoveryInfo = discoveryInfo
self.peer = peer
session = MCSession(peer: self.peer)
session.delegate = self
browser = MCNearbyServiceBrowser(peer: peer, serviceType: "myservicetype")
browser.delegate = self
advertiser = MCNearbyServiceAdvertiser(peer: self.peer, discoveryInfo: self.discoveryInfo, serviceType: "myservicetype")
advertiser.delegate = self
}
/////some other methods
///the problematic method
func setAdvertising(discoveryInfo:[String:String]!){
print("setting advertising")
self.discoveryInfo = discoveryInfo
advertiser.stopAdvertisingPeer()
advertiser = nil
self.advertiser = MCNearbyServiceAdvertiser(peer: self.peer, discoveryInfo: self.discoveryInfo, serviceType: "myservicetype")
self.advertiser.delegate = self
print("advertising from set advertising")
advertiser.startAdvertisingPeer()
}