iOS 7 Multipeer Connectivity
问题描述:
我正在用Multipeer连接框架制作iOS 7应用程序,但我无法让两个设备识别对方。我浏览过文档和wwdc视频,除此之外,关于此框架的信息非常有限。有没有人有与新的点对点功能合作的经验,并可以提供帮助?iOS 7 Multipeer Connectivity
这里基本上是我到目前为止。 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];
答
不声明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或蓝牙连接2台的设备上运行你的应用程序(不需要互联网连接)。请注意,它不适用于蜂窝网络。
答
我同意。为我工作语法(至少让他们看到彼此,我仍然有麻烦接受邀请... :)是:
@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)
这个问题不能REA LLY回答是,是,你可能想使这个问题更加具体,或要求在不同的论坛上的问题,如堆栈Exchange聊天或IRC。 –
而苹果开发者论坛再次上演 - 所以你也应该尝试一下。 – Abizern
好的,谢谢。我仍然无法访问开发者论坛,但我猜测我应该很快就能做到。 –