2014-01-26 52 views
1

我正在使用Multipeer連接框架的應用程序。到目前爲止,一切進展順利,我實施了程序化瀏覽和邀請。Multipeer連接 - 狀態不變

我的問題是,當用戶接受邀請時,瀏覽器沒有收到狀態改變 - 從而不創建會話。

這是廣告客戶收到的邀請方法,我使用與塊集成的操作表創建了該方法。

- (void)advertiser:(MCNearbyServiceAdvertiser *)advertiser 
didReceiveInvitationFromPeer:(MCPeerID *)peerID 
     withContext:(NSData *)context 
invitationHandler:(void(^)(BOOL accept, MCSession *session))invitationHandler 
{ 

    [UIActionSheet showInView:self.view 
        withTitle:[[NSString alloc]initWithFormat:@"%@ would like to share %@ information with you.",peerID.displayName, (NSString *)context] 
      cancelButtonTitle:@"Cancel" 
     destructiveButtonTitle:@"Deny" 
      otherButtonTitles:@[@"Accept"] 
        tapBlock:^(UIActionSheet *actionSheet, NSInteger buttonIndex) { 
         NSLog(@"%i",buttonIndex==1?true:false); 
         MCSession *newSession=[[MCSession alloc]initWithPeer:[[MCPeerID alloc] initWithDisplayName:@"CRAP23456"]]; 
         [newSession setDelegate: self]; 
         NSLog(@"button index %i ",buttonIndex==1?true:false); 
         invitationHandler(buttonIndex==1?YES:NO,newSession); 
        }]; 
} 

上述方法正在被調用,並且邀請處理程序正在返回正確的值。

我從瀏覽器端的實現非常簡單 - 這是當用戶接受/拒絕方法時應該調用的方法。但是,只有當用戶拒絕邀請時纔會被調用:

- (void)session:(MCSession *)session peer:(MCPeerID *)peerID didChangeState:(MCSessionState)state 
{ 
    NSLog(@"%d",state); 
    NSLog(@"%ld",(long)MCSessionStateConnected); 
} 

在此先感謝。

James。

回答

1

我就遇到了這個問題了。我在瀏覽器端的代碼是這樣的:

MCSession *session = [[MCSession alloc] initWithPeer:[self peerID]]; 
session.delegate = self; 
[browser invitePeer:peerID toSession:session withContext:nil timeout:30.0f]; 

這樣做的問題是,瀏覽器不會保留到會話的引用,因此ARC來自各地,並清除它的另一端有機會之前接受。

將其更改爲下面的固定問題:

_session = [[MCSession alloc] initWithPeer:[self peerID]]; 
_session.delegate = self; 
[browser invitePeer:peerID toSession:_session withContext:nil timeout:30.0f]; 

..其中_session是我的課伊娃。

HTH

+0

謝謝你提出這種類型的問題。事實證明,瀏覽器和廣告客戶也存在這個問題。一旦我對他們進行ARC持久參考,系統就開始工作。 –

3

我希望這些人會有所幫助:

  • 實施session:didReceiveCertificate:fromPeer:certificateHandler: 我讀here,這是必要的。

  • 保持兩個同伴之間的瀏覽和廣告單向交易;也就是說,如果兩者都瀏覽,則不要接受兩端的邀請(至少不接受邀請並通過您在invitationHandler()中瀏覽的同一會話)。

  • 裹在didChangeState你的代碼在這樣的塊: dispatch_async(dispatch_get_main_queue(), ^{ NSLog(@"%d",state); NSLog(@"%ld",(long)MCSessionStateConnected); });