Creating a custom MCBrowserViewController

此生再无相见时 提交于 2020-01-01 03:29:09

问题


Is there a way to create a UITableView housing the same information found in an MCBrowserViewController? My current code only allows a standard view to be pushed that is not in the same design as my app:

self.browserVC = [[MCBrowserViewController alloc] initWithServiceType:@"chat" session:self.mySession];
[self presentViewController:self.browserVC animated:YES completion:nil];

Any ideas? Thanks in advance!


回答1:


  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].



回答2:


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.




回答3:


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)



来源:https://stackoverflow.com/questions/19617882/creating-a-custom-mcbrowserviewcontroller

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