2015-05-25 70 views
-1

我已經用Parse構建了帳戶後端,但是,我不知道如何搜索並加入與其他用戶的隨機聊天。當我點擊聊天按鈕時,我想讓它搜索並加入聊天。我應該看什麼來實現這一點?謝謝!如何在Parse上實現隨機聊天功能?

編輯:幾乎在那裏,只是不能搜索/找到另一個用戶。

//------------------------------------------------------------------------------------------------------------------------------------------------- 
- (void)actionChat:(NSString *)groupId 
//------------------------------------------------------------------------------------------------------------------------------------------------- 
{ 
    ChatView *chatView = [[ChatView alloc] initWith:groupId]; 
    // chatView.hidesBottomBarWhenPushed = YES; 
    [self.navigationController pushViewController:chatView animated:YES]; // This JSQMessageViewController 
} 


- (IBAction)startChat:(id)sender { // The button 

    PFQuery *query = [PFUser query]; 

    [query whereKey:@"objectId" notEqualTo:[PFUser currentUser].objectId]; 
    [query setSkip:arc4random()%2]; 
    [query getFirstObjectInBackgroundWithBlock:^(PFObject *object, NSError *error) { 
     if (!object) { 
      NSLog(@"The getFirstObject request failed."); 

     } else { 
      //You now have a random user from your Database, do what you want with it. 

       PFUser *user1 = [PFUser currentUser]; 

       NSString *groupId = StartPrivateChat(user1,object); 
       [self actionChat:groupId]; 

     } 
    }]; 
} 




//------------------------------------------------------------------------------------------------------------------------------------------------- 
    NSString* StartPrivateChat(PFUser *user1, PFUser *user2) 
    //------------------------------------------------------------------------------------------------------------------------------------------------- 
    { 
     NSString *id1 = user1.objectId; 
     NSString *id2 = user2.objectId; 
     //--------------------------------------------------------------------------------------------------------------------------------------------- 
     NSString *groupId = ([id1 compare:id2] < 0) ? [NSString stringWithFormat:@"%@%@", id1, id2] : [NSString stringWithFormat:@"%@%@", id2, id1]; 
     //--------------------------------------------------------------------------------------------------------------------------------------------- 
     NSArray *members = @[user1.objectId, user2.objectId]; 
     //--------------------------------------------------------------------------------------------------------------------------------------------- 
     // CreateRecentItem(user1, groupId, members, user2[PF_USER_FULLNAME]); 
     // CreateRecentItem(user2, groupId, members, user1[PF_USER_FULLNAME]); 
     //--------------------------------------------------------------------------------------------------------------------------------------------- 
     return groupId; 
    } 
+1

通常用戶還來到開發商像這樣的問題。唯一的區別是,他們支付了數千美元....道德:我們不是在這裏給你一個功能齊全的應用程序,我們在這裏幫助你解決一個特定的問題。 –

+0

@LordZsolt我不希望代碼是爲我寫的。關於如何實施它的正確方向只是一點,檢查X提供這個聊天。 – George

+0

你有試過Google嗎?甚至還有一個關於它的視頻,除了其他436,000個「ios Parse聊天」之外,還有第5或第6個結果。對不起,我的粗魯,但你應該閱讀http://stackoverflow.com/help/how-to-ask並在發佈問題之前相應地採取行動。 –

回答

1
PFQuery *query = [PFUser query]; 

[query whereKey:@"objectId" notEqualTo:[PFUser currentUser].objectId]; 
[query setSkip:arc4random()%YOUR_TOTAL_USERS]; 
[query getFirstObjectInBackgroundWithBlock:^(PFObject *object, NSError *error) { 
      if (!object) { 
       NSLog(@"The getFirstObject request failed."); 

      } else { 
       //You now have a random user from your Database, do what you want with it. 
      } 
     }];