简体   繁体   中英

Creating a custom MCBrowserViewController

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. Set your View Controller as the delegate to MCNearbyServiceBrowser and MCSession (ie <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)

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM