Tim*_*son 5 cocoa-touch invitation ios7 multipeer-connectivity
我正在尝试按照WWDC的讲话来了解MultipeerConnectivity框架.在许多错误启动之后,浏览器会显示对等方,并发出邀请.
但是当我在对等设备上按"接受"时,浏览器会一直显示"正在连接".我认为MCBrowserViewController照顾逻辑,我可以放松,直到浏览器的用户按下取消或完成,并且委托方法被触发.我敢打赌这是显而易见的,但它让我望而却步.
这是我希望的相关代码.我在AppDelegate中有它.各种委托方法中的NSLog语句按照我的预期被调用 - browserViewControllerDidFinish:当然除了那个.
请记住,浏览器和邀请确实出现了,所以有些事情是正确的!
在@interface中......
@property (strong, nonatomic) MCSession *theSession;
@property (strong, nonatomic) MCAdvertiserAssistant *assistant;
@property (strong, nonatomic) MCBrowserViewController *browserVC;
Run Code Online (Sandbox Code Playgroud)
在@implementation中
static NSString* const kServiceType = @"eeps-multi";
// called from viewDidAppear in the main ViewController
-(void) startSession
{
if (!self.theSession) {
UIDevice *thisDevice = [UIDevice currentDevice];
MCPeerID *aPeerID = [[ MCPeerID alloc ] initWithDisplayName: thisDevice.name];
self.theSession = [[ MCSession alloc ] initWithPeer: aPeerID ];
self.theSession.delegate = self;
} else {
NSLog(@"Session init skipped -- already exists");
}
}
// called from viewDidAppear in the main ViewController
- (void) startAdvertising
{
if (!self.assistant) {
self.assistant = [[MCAdvertiserAssistant alloc] initWithServiceType:kServiceType
discoveryInfo:nil
session:self.theSession ];
self.assistant.delegate = self;
[ self.assistant start ];
} else {
NSLog(@"Advertiser init skipped -- already exists");
}
}
// called from the main ViewController in response to a button press
- (void) startBrowsing
{
if (!self.browserVC){
self.browserVC = [[MCBrowserViewController alloc] initWithServiceType:kServiceType
session:self.theSession];
self.browserVC.delegate = self;
} else {
NSLog(@"Browser VC init skipped -- already exists");
}
[ self.window.rootViewController presentViewController:self.browserVC animated:YES completion:nil];
}
Run Code Online (Sandbox Code Playgroud)
提前致谢!
Tim*_*son 15
感谢评论者的出色建议,这让我发现了自己的错误.这是:
如果实现MCSessionDelegate方法session:didReceiveCertificate:fromPeer:certificateHandler方法,它将拦截对等方连接到会话的尝试.您应该在该方法中明确批准该连接或将其注释掉.
细节和经验教训:
除了我展示的代码之外,我还对各种委托方法进行了粗短的实现.一种MCSessionDelegate方法是这一种:
- (void) session:(MCSession *)session
didReceiveCertificate:(NSArray *)certificate
fromPeer:(MCPeerID *)peerID
certificateHandler:(void (^)(BOOL))certificateHandler
{
}
Run Code Online (Sandbox Code Playgroud)
扩展@ Big-O Claire的建议,我开始观察所有这些委托方法,Just In Case.当对等方点击AdvertiserAssistant UI中的"接受"按钮时,会触发此消息.
此方法让您有机会决定对等方是否合法,而不是连接(使用certificateHandler:),如果您不想这样做.Apple说,
您的应用应检查附近的同行证书,然后决定是否信任该证书.在做出该决定后,您的应用应该调用提供的certificateHandler块,传递YES(以信任附近的对等方)或NO(拒绝它).
另外,你得到这个提示:
要点:多重连接框架不会尝试以任何方式验证对等方提供的身份或证书.如果您的代理未实现此方法,则会自动接受所有证书.
当我评论这个方法时,连接经历了 - 至少这个问题已经解决了.
| 归档时间: |
|
| 查看次数: |
5444 次 |
| 最近记录: |