XMPP接受發送消息

  在現階段的通訊服務中,各類標準都有,所以會出現沒法實現相互連通,而XMPP(Extensible Message and presence Protocol)協議的出現。實現了整個及時通訊服務協議的互通。有了這個協議以後。使用不論什麼一個組織或者我的提供的即便通訊服務,均可以無障礙的與其它的及時通訊服務的用戶進行交流。好比google 公司2005年推出的Google talk就是一款基於XMPP協議的即時通訊軟件。ide

如下咱們就談論一下怎樣簡單的使用XMPP的接收和發送消息fetch

一、在XMPPFramework.h中將需要用到的頭文件打開

 

二、在storyboard建立展現消息頁面

 

三、定義查詢結果存儲器並且進行初始化

//消息存檔
@property(nonatomic,strong) XMPPMessageArchiving * messageArch;

//消息存檔存儲模型
@property(nonatomic,strong) XMPPMessageArchivingCoreDataStorage * messageStore;

四、在上面storyboard建立的控制器中。加入例如如下代碼

AppDelegate * delgate=[UIApplication sharedApplication].delegate;
    
    //初始化頭像
    XMPPJID * fromJid=[XMPPJID jidWithString:self.fromJid];
    NSData * fromData=[delgate.vCardAvatarModule photoDataForJID:fromJid];
    self.fromImage=[[UIImage  alloc] initWithData:fromData];

    
    NSString * userName= [[NSUserDefaults standardUserDefaults] objectForKey:@"userName"];
    XMPPJID * tojid=[XMPPJID jidWithString:userName];
    
    //設置圖片模型
    NSData * toData=[delgate.vCardAvatarModule photoDataForJID:tojid];
    self.meImage=[[UIImage  alloc] initWithData:toData];
    
    if (self.fromImage==nil) {
        self.fromImage=[UIImage imageNamed:@"defalut"];
    }
    
    if (self.meImage==nil) {
        self.meImage=[UIImage imageNamed:@"defalut"];
    }

 
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(showKey:) name:UIKeyboardWillShowNotification object:nil];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(hideKey:) name:UIKeyboardWillHideNotification object:nil];
    
    //初始化數據存儲
  NSString *user=  [[NSUserDefaults standardUserDefaults] objectForKey:@"userName"];
    
    //初始化請求
    NSFetchRequest * request=[[NSFetchRequest alloc] initWithEntityName:@"XMPPMessageArchiving_Message_CoreDataObject"];
    request.predicate=[NSPredicate predicateWithFormat:@"bareJidStr=%@ and streamBareJidStr=%@",self.fromJid,user];
    
    
    //定義排序
    NSSortDescriptor * des=[NSSortDescriptor sortDescriptorWithKey:@"timestamp" ascending:YES];
    [request setSortDescriptors:@[des]];
    
    //獲取上下文
    NSManagedObjectContext *context=[delgate.messageStore mainThreadManagedObjectContext];
    
    //初始化結果存儲器
    fetch=[[NSFetchedResultsController alloc] initWithFetchRequest:request managedObjectContext:context sectionNameKeyPath:nil cacheName:nil];
    
    //設置代理
    fetch.delegate=self;
    
    //開始查詢
    [fetch performFetch:nil];

五、創實現tableView的代理方法

-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    id <NSFetchedResultsSectionInfo>  info=fetch.sections[section];
        NSLog(@"===%ld",info.numberOfObjects);
    return [info numberOfObjects];
    
}
- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    XMPPMessageArchiving_Message_CoreDataObject * obj=[fetch objectAtIndexPath:indexPath];
    JRChatTableViewCell * cell=nil;
    if (obj.isOutgoing) {
         cell=[tableView dequeueReusableCellWithIdentifier:@"cellto"];
    }else{
         cell=[tableView dequeueReusableCellWithIdentifier:@"cellfrom"];
    }
    
    
    //設置頭像
    cell.image.image=self.meImage;
    cell.selectionStyle=UITableViewCellSelectionStyleNone;
    [cell setText:obj.body WithFlag:obj.isOutgoing ];
    
    return cell;

}

六、添加鍵盤控制

  [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(showKey:) name:UIKeyboardWillShowNotification object:nil];
    
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(hideKey:) name:UIKeyboardWillHideNotification object:nil];
- (void) showKey:(NSNotification * ) notify{

    
    CGFloat time=[notify.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue]  ;
    
    CGRect frame=[notify.userInfo[UIKeyboardFrameBeginUserInfoKey] CGRectValue];
    [UIView animateWithDuration:time animations:^{
        self.keyView.transform=CGAffineTransformMakeTranslation(0, frame.size.height*-1);
    }];
    
}

- (void) hideKey:(NSNotification * ) notify{
    
    
    CGFloat time=[notify.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue]  ;
    [UIView animateWithDuration:time animations:^{
        self.keyView.transform=CGAffineTransformIdentity;
    }];
    
}

七、發送消息

-(BOOL)textFieldShouldReturn:(UITextField *)textField{
    [textField resignFirstResponder];
    
    //發送消息
    
    //1 取出文本
    AppDelegate * delgate=[UIApplication sharedApplication].delegate;
    
    XMPPJID * jid=[XMPPJID jidWithString:self.fromJid];
    
    //初始化消息體
    XMPPMessage * message=[XMPPMessage messageWithType:@"chat" to:jid];
    [message addBody:self.ketf.text];
    
    //發送消息
    [delgate.stream sendElement:message];
    
    //將消息置空
    self.ketf.text=nil;
    return YES;
}

 

  想要了解不少其它內容的小夥伴,可以點擊查看源代碼。親自執行測試。google

  疑問諮詢或技術交流,請增長官方QQ羣:JRedu技術交流 (452379712)
atom

 

做者: 傑瑞教育
出處: http://blog.csdn.net/jerehedu/ 
本文版權歸煙臺傑瑞教育科技有限公司和CSDN共同擁有。歡迎轉載,但未經做者容許必須保留此段聲明,且在文章頁面明顯位置給出原文鏈接,不然保留追究法律責任的權利。
相關文章
相關標籤/搜索