gpt4 book ai didi

cocoa-touch - 多点连接 : getting an invitation accepted (using built-in browser VC)

转载 作者:行者123 更新时间:2023-12-04 03:13:24 25 4
gpt4 key购买 nike

我正在尝试 follow the WWDC talk了解 MultipeerConnectivity 框架。在多次错误启动后,浏览器会显示对等点,并发出邀请。

但是当我在对等设备上按“接受”时,浏览器会不断显示“正在连接”。我以为 MCBrowserViewController处理了逻辑,我可以放松,直到浏览器的用户按下取消或完成,并且委托(delegate)方法被触发。我敢打赌,这很明显,但它让我无法理解。

这是我希望的相关代码。我在 AppDelegate 中有它。各种委托(delegate)方法中的 NSLog 语句被调用,正如我所期望的那样——除了 browserViewControllerDidFinish: 中的那个。当然。

请记住,浏览器和邀请确实会出现,所以是对的!

在@界面中...

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

在@实现中
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];
}

提前致谢!

最佳答案

感谢评论者提出的出色建议,这些建议让我发现了自己的错误。这里是:

如果您实现 MCSessionDelegate方法session:didReceiveCertificate:fromPeer:certificateHandler方法,它将拦截对等方连接到 session 的尝试。您应该在该方法中明确批准该连接或将其注释掉。

细节和经验教训:

除了我展示的代码之外,我还对各种委托(delegate)方法进行了粗略的实现。一个MCSessionDelegate方法是这样的:

- (void)          session:(MCSession *)session 
didReceiveCertificate:(NSArray *)certificate
fromPeer:(MCPeerID *)peerID
certificateHandler:(void (^)(BOOL))certificateHandler
{

}

扩展上面@Big-O Claire 的建议,我开始关注所有这些委托(delegate)方法,以防万一。当对等方点击 AdvertiserAssistant UI 中的 Accept 按钮时,这一事件被触发。

此方法使您有机会确定对等方是否合法,如果您不想连接,则不连接(使用 certificateHandler: )。苹果说,

Your app should inspect the nearby peer’s certificate, and then should decide whether to trust that certificate. Upon making that determination, your app should call the provided certificateHandler block, passing either YES (to trust the nearby peer) or NO (to reject it).



此外,您还会收到以下提示:

Important: The multipeer connectivity framework makes no attempt to validate the peer-provided identity or certificates in any way. If your delegate does not implement this method, all certificates are accepted automatically.



当我把这个方法注释掉时,连接就通过了——至少这个问题得到了解决。

关于cocoa-touch - 多点连接 : getting an invitation accepted (using built-in browser VC),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19017165/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com