问题
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
andMCSession
(i.e.<MCNearbyServiceBrowserDelegate, MCSessionDelegate>
) - Create a property for your
MCNearbyServiceBrowser
(andMCSession
) 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];
Implement the
- (void)browser:(MCNearbyServiceBrowser *)browser foundPeer:(MCPeerID *)peerID withDiscoveryInfo:(NSDictionary *)info
method as so:- Add every found peer into an array for the data source of your
UITableView
. Typically you'd get thepeerID.displayName
. - Call
[tableView reloadData]
.
- Add every found peer into an array for the data source of your
回答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