【问题标题】:iOS 7 Multipeer ConnectivityiOS 7 多人连接
【发布时间】:2013-07-31 18:59:24
【问题描述】:

我正在使用 Multipeer 连接框架制作 iOS 7 应用程序,但我无法让两台设备相互识别。我浏览了文档和 wwdc 视频,除此之外,关于这个框架的信息非常有限。有没有人有使用新的点对点功能的经验并且可以提供帮助?

这基本上是我到目前为止所拥有的。 browserVC 显示在屏幕上,但当我在两台设备上运行该应用程序时未找到任何设备。

MCPeerID *peer = [[MCPeerID alloc] initWithDisplayName:@"user"];
  _session =  [[MCSession alloc] initWithPeer:peer];
  NSString *service = @"nsync";

  _session.delegate = self;

  MCAdvertiserAssistant *assistant =[[MCAdvertiserAssistant alloc] initWithServiceType:service
                                                                         discoveryInfo:nil
                                                                               session:_session];
  [assistant start];


  MCBrowserViewController *browserVC = [[MCBrowserViewController alloc] initWithServiceType:service
                                                                                    session:_session];
  browserVC.delegate = self;
  [self presentViewController:browserVC
                     animated:YES
                   completion:nil];

【问题讨论】:

  • 这个问题无法真正按原样回答,您可能希望使问题更具体或在其他论坛中提问,例如 Stack Exchange 聊天或 IRC。
  • Apple 开发者论坛又上线了 - 所以你也应该去那里试试。
  • 好的,谢谢。我仍然无法访问开发者论坛,但我猜我应该很快就能访问。

标签: ios ios7 multipeer-connectivity


【解决方案1】:

要让您的浏览器能够看到设备,您需要拥有一些其他正在投放广告的设备。我认为您的问题是您的 MCAdvertiserAssistant 超出范围并被释放,因为它仅存储在局部变量中。

这是我用来做广告的代码:

#define SERVICE_TYPE @"MyServiceType"
...

@property (nonatomic, strong) MCAdvertiserAssistant* advertiserAssistant;
...

self.peerId = [[MCPeerID alloc] initWithDisplayName:[[UIDevice currentDevice] name]];
self.advertiserSession = [[MCSession alloc] initWithPeer:self.peerId];
self.advertiserSession.delegate = self;
self.advertiserAssistant = [[MCAdvertiserAssistant alloc] initWithServiceType:SERVICE_TYPE discoveryInfo:nil session:self.advertiserSession];
[self.advertiserAssistant start];

请注意,我将广告客户助理存储在一个属性中,以便在创建它的方法完成后不会立即释放它。

然后浏览:

self.peerId = [[MCPeerID alloc] initWithDisplayName:[[UIDevice currentDevice] name]];
self.browserSession = [[MCSession alloc] initWithPeer:self.peerId];
self.browserSession.delegate = self;
self.browser = [[MCBrowserViewController alloc] initWithServiceType:SERVICE_TYPE session:self.browserSession];
self.browser.delegate = self;
[self presentViewController:self.browser animated:YES completion:nil];

【讨论】:

    【解决方案2】:

    不要将 MCAdvertiserAssistant *assistant 声明为局部变量,声明为类成员。

    【讨论】:

      【解决方案3】:

      我同意。

      我昨天刚试了一下,效果很好。除了您的MCAdvertiserAssistant 之外,您的代码似乎是正确的。应该设置为全局变量!

      正如 Greg 所说,您必须在至少与 wifi 或蓝牙连接的 2 台设备上运行您的应用程序(不需要互联网连接)。请注意,它不适用于蜂窝网络。

      【讨论】:

        【解决方案4】:

        我同意。一种对我有用的语法(至少让他们看到彼此;我仍然无法接受邀请...... :) 是:

        @property   (strong, nonatomic) MCSession   *theSession;
        @property   (strong, nonatomic) MCAdvertiserAssistant       *assistant;
        @property   (strong, nonatomic) MCBrowserViewController     *browserVC;
        

        然后,

            UIDevice *thisDevice = [UIDevice currentDevice];
        
            MCPeerID *aPeerID = [[ MCPeerID alloc ] initWithDisplayName: thisDevice.name];
            self.theSession = [[ MCSession alloc ] initWithPeer: aPeerID ];
            self.theSession.delegate = self;
        
            self.assistant = [[MCAdvertiserAssistant alloc] initWithServiceType:kServiceType
                                                                  discoveryInfo:nil
                                                                        session:self.theSession ];
            self.assistant.delegate = self;
            [ self.assistant start ];
        

            self.browserVC = [[MCBrowserViewController alloc] initWithServiceType:kServiceType session:self.theSession];
            self.browserVC.delegate = self;
            [ self.window.rootViewController presentViewController:self.browserVC animated:YES completion:nil];
        

        (必须使用rootViewController,因为我不是在我的主 VC 中这样做的。)

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-03-11
          • 2013-10-06
          • 1970-01-01
          相关资源
          最近更新 更多