IOS7 Multipeer Connectivity使用Discovery Info创建自定义邀请

时间:2013-10-01 18:41:45

标签: ios objective-c ios7 multipeer-connectivity

如何创建自定义邀请,以显示广告客户的discoveryInfo?

以下是我的广告客户的代码:

// create Discovery Info
NSArray *objects=[[NSArray alloc] initWithObjects:@"datguy",@"28", nil];
NSArray *keys = [[NSArray alloc] initWithObjects:@"Name",@"Age", nil];
self.dictionaryInfo = [[NSDictionary alloc] initWithObjects:objects forKeys:keys];

//  Setup Advertiser
self.advertiser = [[MCAdvertiserAssistant alloc] initWithServiceType:@"txt_msg_service" discoveryInfo:self.dictionaryInfo session:self.advertiseSession];
[self.advertiser start];

1 个答案:

答案 0 :(得分:4)

我认为你倒退了。 IFAIK,MCAdvertiserAssistant的discoveryInfo是广告商为浏览器传递的信息。 此信息将传递至:

- (void)browser:(MCNearbyServiceBrowser *)browser foundPeer:(MCPeerID *)peerID withDiscoveryInfo:(NSDictionary *)info

这是MCNearbyServiceBrowser类的委托方法。对此:

– browserViewController:shouldPresentNearbyPeer:withDiscoveryInfo:
Which is the delegate for the MCBrowserViewController class.

此信息应用于检查是否应为您的用户显示此广告客户。

如果您对拦截邀请尝试感兴趣,则应考虑使用MCNearbyServiceAdvertiser类而不是助手。这样您就可以委派didReceiveInvitationFromPeer方法并为其添加一些自定义逻辑:

// pedido para entrar na sala.
- (void)advertiser:(MCNearbyServiceAdvertiser *)advertiser didReceiveInvitationFromPeer:(MCPeerID *)peerID
       withContext:(NSData *)context
 invitationHandler:(void (^)(BOOL accept, MCSession *session))invitationHandler {
    // get the browser's info
    NSDictionary *peerInfo = (NSDictionary *) [NSKeyedUnarchiver unarchiveObjectWithData:context];

    //check if peer is valid for this room (add your custom logic in this method)
    if ([self isValidForThisRoom:peerInfo]) {
        //create an alert
        NSObject *clientName = [peerInfo valueForKey:@"playerName"];
        NSString *clientMessage = [[NSString alloc] initWithFormat:@"%@ wants to connect. Accept?", clientName];
        UIAlertView *alertView = [[UIAlertView alloc]
                initWithTitle:@"Accept Connection?"
                      message:clientMessage
                     delegate:self
            cancelButtonTitle:@"No"
            otherButtonTitles:@"Yes", nil];
        [alertView show];

        // copy the invitationHandler block to an array to use it later
        ArrayInvitationHandler = [NSArray arrayWithObject:[invitationHandler copy]];
    } else {
        // if the peer is not valid, decline the invitation
        invitationHandler(NO, _session);
    }
}

在alertview的clickedButtonAtIndex委托中,您可以执行以下操作:

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    //obtem a decisao do usuario
    BOOL accept = (buttonIndex != alertView.cancelButtonIndex) ? YES : NO;

    // obtem o invitationHandler que foi guardado do didReceiveInvitationFromPeer
    void (^invitationHandler)(BOOL, MCSession *) = [ArrayInvitationHandler objectAtIndex:0];

    // chama o invitationHandler passando a nossa session
    invitationHandler(accept, _session);
}

要注意的两件重要事情是:

  1. 您的会话对象应该是一个实例变量,而不是我在某些代码示例中看到的本地变量。根据我的经验,我刚刚创建了一个静态共享实例来存储会话对象。如果您想将会话从一个屏幕传递到另一个屏幕,这也是一个很好的计划。

  2. 请注意,您应该创建一个NSArray * ArrayInvitationHandler;用于存储块代码的变量。我试图做Block_copy,但是它有一些转换错误,所以{stack}在这里的someone决定存储在一个数组中,我觉得它很优雅。

  3. 无论如何,我使用此设置工作。希望它可以帮到你:D