简体   繁体   English

创建自定义MCBrowserViewController

[英]Creating a custom MCBrowserViewController

Is there a way to create a UITableView housing the same information found in an MCBrowserViewController ? 有没有办法创建一个UITableView容纳MCBrowserViewController的相同信息? My current code only allows a standard view to be pushed that is not in the same design as my app: 我当前的代码只允许推送与我的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> ) 将View Controller设置为MCNearbyServiceBrowserMCSession的委托(即<MCNearbyServiceBrowserDelegate, MCSessionDelegate>
  2. Create a property for your MCNearbyServiceBrowser (and MCSession ) 为您的MCNearbyServiceBrowser (和MCSession )创建一个属性
  3. In viewDidLoad (or whichever trigger suits your pattern) of your View Controller: 在视图控制器的viewDidLoad (或适合您的模式的触发器)中:

     _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: 实现- (void)browser:(MCNearbyServiceBrowser *)browser foundPeer:(MCPeerID *)peerID withDiscoveryInfo:(NSDictionary *)info方法如下:

    1. Add every found peer into an array for the data source of your UITableView . 将每个找到的对等体添加到UITableView的数据源的数组中。 Typically you'd get the peerID.displayName . 通常,您将获得peerID.displayName
    2. Call [tableView reloadData] . 调用[tableView reloadData]

Check out MCSessionP2P , a demo app that illustrates the ad-hoc networking features of MCSession . 退房MCSessionP2P ,演示应用程序,说明了临时网络功能MCSession SessionController conforms to MCSessionDelegate , MCNearbyServiceBrowserDelegate and MCNearbyServiceAdvertiserDelegate and acts as the datasource for a UITableView . SessionController符合MCSessionDelegateMCNearbyServiceBrowserDelegateMCNearbyServiceAdvertiserDelegate并充当UITableView的数据源。 The app advertises itself via Wi-Fi or Bluetooth and programmatically connects to available peers, establishing a peer-to-peer network. 该应用程序通过Wi-Fi或蓝牙进行广告宣传,并以编程方式连接到可用的对等点,建立点对点网络。

Yazid's answer was working for me. 亚齐德的回答对我有用。 Next step, to connect to a peer that was found during startBrowsingForPeers use 下一步,连接到在startBrowsingForPeers使用期间找到的对等方

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

(SWIFT notation here) (SWIFT表示法)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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