在現階段的通訊服務中,各類標準都有,所以會出現沒法實現相互連通,而XMPP(Extensible Message and presence Protocol)協議的出現。實現了整個及時通訊服務協議的互通。有了這個協議以後。使用不論什麼一個組織或者我的提供的即便通訊服務,均可以無障礙的與其它的及時通訊服務的用戶進行交流。好比google 公司2005年推出的Google talk就是一款基於XMPP協議的即時通訊軟件。ide
如下咱們就談論一下怎樣簡單的使用XMPP的接收和發送消息fetch
//消息存檔 @property(nonatomic,strong) XMPPMessageArchiving * messageArch; //消息存檔存儲模型 @property(nonatomic,strong) XMPPMessageArchivingCoreDataStorage * messageStore;
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];
-(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羣: (452379712)
atom