iOS 7多点连接
我正在用Multipeer连接框架制作一个iOS 7应用程序,但我无法让两台设备相互识别。我浏览了文档和wwdc视频,除此之外,关于这个框架的信息非常有限。是否有人有使用新的点对点功能的经验并能提供帮助 这是我到目前为止的基本情况。browserVC会显示在屏幕上,但当我在两台设备上运行应用程序时,找不到任何设备iOS 7多点连接,ios,ios7,multipeer-connectivity,Ios,Ios7,Multipeer Connectivity,我正在用Multipeer连接框架制作一个iOS 7应用程序,但我无法让两台设备相互识别。我浏览了文档和wwdc视频,除此之外,关于这个框架的信息非常有限。是否有人有使用新的点对点功能的经验并能提供帮助 这是我到目前为止的基本情况。browserVC会显示在屏幕上,但当我在两台设备上运行应用程序时,找不到任何设备 MCPeerID *peer = [[MCPeerID alloc] initWithDisplayName:@"user"]; _session = [[MCSession a
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];
不要将MCAdvertiserAssistant*assistant声明为局部变量,而应声明为类成员。若要让浏览器查看设备,您需要其他一些正在发布广告的设备。我认为您的问题在于,您的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];
我同意
我昨天刚试过,效果很好。除了您的MCAdvertiserAssistant
之外,您的代码似乎是正确的。应将其设置为全局变量
正如格雷格所说,你必须在两台设备上运行你的应用程序,这两台设备至少连接了wifi或蓝牙(不需要互联网连接)。请注意,它不能与蜂窝网络一起工作。我同意。一个对我有用的语法(至少能让他们看到彼此;我仍然难以接受邀请…:)是: 然后后来,
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 ];
及
(必须使用
rootViewController
,因为我不是在我的主VC中这样做的。)这个问题不能按原样回答,你可能想让问题更具体,或者在其他论坛上提问,比如Stack Exchange聊天室或IRC。苹果开发者论坛又开了——所以你也应该试试。好的,谢谢。我仍然无法访问开发者论坛,但我猜我很快就能访问。
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];