Creating a custom MCBrowserViewController

狂风中的少年 提交于 2019-12-03 08:31:59
  1. Set your View Controller as the delegate to MCNearbyServiceBrowser and MCSession (i.e. <MCNearbyServiceBrowserDelegate, MCSessionDelegate>)
  2. Create a property for your MCNearbyServiceBrowser (and MCSession)
  3. In viewDidLoad (or whichever trigger suits your pattern) of your View Controller:

    _myPeerID = [[MCPeerID alloc] initWithDisplayName:[UIDevice currentDevice].name];
    _mySession = [[MCSession alloc] initWithPeer:_myPeerID];
    [_mySession setDelegate:self];
    _browser = [[MCNearbyServiceBrowser alloc]initWithPeer:_myPeerID serviceType:@"connectme"];
    [_browser setDelegate:self];
    [_browser startBrowsingForPeers];
    
  4. Implement the - (void)browser:(MCNearbyServiceBrowser *)browser foundPeer:(MCPeerID *)peerID withDiscoveryInfo:(NSDictionary *)info method as so:

    1. Add every found peer into an array for the data source of your UITableView. Typically you'd get the peerID.displayName.
    2. Call [tableView reloadData].

Check out MCSessionP2P, a demo app that illustrates the ad-hoc networking features of MCSession. SessionController conforms to MCSessionDelegate, MCNearbyServiceBrowserDelegate and MCNearbyServiceAdvertiserDelegate and acts as the datasource for a UITableView. The app advertises itself via Wi-Fi or Bluetooth and programmatically connects to available peers, establishing a peer-to-peer network.

Yazid's answer was working for me. Next step, to connect to a peer that was found during startBrowsingForPeers use

_browser.invitePeer(peerID, toSession: _mySession, withContext: nil, timeout: 30.0)

(SWIFT notation here)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!