iOS 搭建xmpp聊天的具體步驟如下:
馬關(guān)網(wǎng)站建設(shè)公司創(chuàng)新互聯(lián),馬關(guān)網(wǎng)站設(shè)計制作,有大型網(wǎng)站制作公司豐富經(jīng)驗。已為馬關(guān)超過千家提供企業(yè)網(wǎng)站建設(shè)服務(wù)。企業(yè)網(wǎng)站搭建\外貿(mào)網(wǎng)站制作要多少錢,請找那個售后服務(wù)好的馬關(guān)做網(wǎng)站的公司定做!
聊天室
[cpp] view plain copy
print?
//初始化聊天室
XMPPJID *roomJID = [XMPPJID jidWithString:ROOM_JID];
xmppRoom = [[XMPPRoom alloc] initWithRoomStorage:self jid:roomJID];
[xmppRoom activate:xmppStream];
[xmppRoom addDelegate:self delegateQueue:dispatch_get_main_queue()];
[cpp] view plain copy
print?
//創(chuàng)建聊天室成功
- (void)xmppRoomDidCreate:(XMPPRoom *)sender
{
DDLogInfo(@"%@: %@", THIS_FILE, THIS_METHOD);
}
[cpp] view plain copy
print?
//加入聊天室,使用昵稱
[xmppRoom joinRoomUsingNickname:@"quack" history:nil];
[cpp] view plain copy
print?
//獲取聊天室信息
- (void)xmppRoomDidJoin:(XMPPRoom *)sender
{
[xmppRoom fetchConfigurationForm];
[xmppRoom fetchBanList];
[xmppRoom fetchMembersList];
[xmppRoom fetchModeratorsList];
}
如果房間存在,會調(diào)用委托
[cpp] view plain copy
print?
// 收到禁止名單列表
- (void)xmppRoom:(XMPPRoom *)sender didFetchBanList:(NSArray *)items;
// 收到好友名單列表
- (void)xmppRoom:(XMPPRoom *)sender didFetchMembersList:(NSArray *)items;
// 收到主持人名單列表
- (void)xmppRoom:(XMPPRoom *)sender didFetchModeratorsList:(NSArray *)items;
房間不存在,調(diào)用委托
[cpp] view plain copy
print?
- (void)xmppRoom:(XMPPRoom *)sender didNotFetchBanList:(XMPPIQ *)iqError;
- (void)xmppRoom:(XMPPRoom *)sender didNotFetchMembersList:(XMPPIQ *)iqError;
- (void)xmppRoom:(XMPPRoom *)sender didNotFetchModeratorsList:(XMPPIQ *)iqError;
離開房間
[cpp] view plain copy
print?
[xmppRoom deactivate:xmppStream];
[cpp] view plain copy
print?
//離開聊天室
- (void)xmppRoomDidLeave:(XMPPRoom *)sender
{
DDLogVerbose(@"%@: %@", THIS_FILE, THIS_METHOD);
}
其他代理
[cpp] view plain copy
print?
//新人加入群聊
- (void)xmppRoom:(XMPPRoom *)sender occupantDidJoin:(XMPPJID *)occupantJID
{
DDLogVerbose(@"%@: %@", THIS_FILE, THIS_METHOD);
}
//有人退出群聊
- (void)xmppRoom:(XMPPRoom *)sender occupantDidLeave:(XMPPJID *)occupantJID
{
DDLogVerbose(@"%@: %@", THIS_FILE, THIS_METHOD);
}
//有人在群里發(fā)言
- (void)xmppRoom:(XMPPRoom *)sender didReceiveMessage:(XMPPMessage *)message fromOccupant:(XMPPJID *)occupantJID
{
DDLogVerbose(@"%@: %@", THIS_FILE, THIS_METHOD);
}
1.使用XMPPFramework前的準(zhǔn)備,獲取XmppStream和激活要用的組件,在AppDelegate添加代碼。以后要用xmppStream時,要通過AppDelegate獲取。下面的代碼是在AppDelegate.m中進(jìn)行的相關(guān)組件的初始化,代碼如下
(1)實例化XMPPStream
//創(chuàng)建xmppstream
self.xmppStream = [[XMPPStream alloc]init];
(2)創(chuàng)建重連組件,并在xmppStream中激活
1 //創(chuàng)建重寫連接組件
2 xmppReconnect= [[XMPPReconnect alloc] init];
3 //使組件生效
4 [xmppReconnect activate:self.xmppStream];
(3)創(chuàng)建message部分的內(nèi)容,接受的消息我們保存在本地數(shù)據(jù)庫中,我們要顯示的時候是從數(shù)據(jù)庫中獲取的。在初始化消息組件的時候,要指定保存策略,一般可以選的是CoreData還是內(nèi)存。指定完保存策略后實例化Message是要關(guān)聯(lián)保存策略,之后也是需要在XMPPStream中進(jìn)行激活的,最后要獲取CoreData的上下文。代碼如下:
復(fù)制代碼
1 //創(chuàng)建消息保存策略(規(guī)則,規(guī)定)
2 messageStorage = [XMPPMessageArchivingCoreDataStorage sharedInstance];
3 //用消息保存策略創(chuàng)建消息保存組件
4 xmppMessageArchiving = [[XMPPMessageArchiving alloc]initWithMessageArchivingStorage:messageStorage];
5 //使組件生效
6 [xmppMessageArchiving activate:self.xmppStream];
7 //提取消息保存組件的coreData上下文
8 self.xmppManagedObjectContext = messageStorage.mainThreadManagedObjectContext;
復(fù)制代碼
(4),初始化獲取好友列表的相關(guān)組件并指定保存策略,和上面的代碼步驟極為相似。這也能看出來在XMPPFramework中進(jìn)行組件的初始化步驟是差不多的。下面我們設(shè)定自動獲取花名冊,代碼如下:
復(fù)制代碼
1 xmppRosterStorage = [[XMPPRosterCoreDataStorage alloc] init];
2 xmppRoster = [[XMPPRoster alloc] initWithRosterStorage:xmppRosterStorage];
3 //自動獲取用戶列表
4 xmppRoster.autoFetchRoster = YES;
5 xmppRoster.autoAcceptKnownPresenceSubscriptionRequests = YES;
6
7 [xmppRoster activate:self.xmppStream];
8 self.xmppRosterManagedObjectContext = xmppRosterStorage.mainThreadManagedObjectContext;
復(fù)制代碼
2.登陸模塊的實現(xiàn)
登陸時就是用戶輸入JID和Password,然后連接服務(wù)器和驗證密碼,如果認(rèn)證成功則跳轉(zhuǎn)到好友列表才Controller,同時把JID和Password存儲到UserDefaults中便于下次自動連接。下面的代碼就是登陸部分的代碼(LoginViewController.m):
(1).通過應(yīng)用代理獲取XMPPStream,并注冊回調(diào),代碼如下:
復(fù)制代碼
1 -(void) initXmpp
2 {
3 //獲取應(yīng)用的xmppSteam(通過Application中的單例獲取)
4 UIApplication *application = [UIApplication sharedApplication];
5 id delegate = [application delegate];
6 self.xmppStream = [delegate xmppStream];
7
8 //注冊回調(diào)
9 [self.xmppStream addDelegate:self delegateQueue:dispatch_get_main_queue()];
10 }
復(fù)制代碼
(2).創(chuàng)建JID連接服務(wù)器
復(fù)制代碼
1 //連接服務(wù)器
2 -(void) xmppConnect
3 {
4 if (![self.userNameTextFiled.text isEqualToString:@""] self.userNameTextFiled.text != nil)
5 {
6 //1.創(chuàng)建JID
7 XMPPJID *jid = [XMPPJID jidWithUser:self.userNameTextFiled.text domain:MY_DOMAIN resource:@"iPhone"];
8
9 //2.把JID添加到xmppSteam中
10 [self.xmppStream setMyJID:jid];
11
12 //連接服務(wù)器
13 NSError *error = nil;
14 [self.xmppStream connectWithTimeout:10 error:error];
15 if (error)
16 {
17 NSLog(@"連接出錯:%@",[error localizedDescription]);
18 }
19
20 }
21 else
22 {
23 UIAlertView *alter = [[UIAlertView alloc] initWithTitle:@"提示" message:@"用戶名不能為空" delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:nil];
24 [alter show];
25 }
26 }
復(fù)制代碼
(3).連接成后需要認(rèn)證密碼,代碼如下:
復(fù)制代碼
1 //連接后的回調(diào)
2 -(void)xmppStreamDidConnect:(XMPPStream *)sender
3 {
4 if (![self.passwordTextFiled.text isEqualToString:@""] self.passwordTextFiled.text != nil)
5 {
6 //連接成功后認(rèn)證用戶名和密碼
7 NSError *error = nil;
8 [self.xmppStream authenticateWithPassword:self.passwordTextFiled.text error:error];
9 if (error)
10 {
11 NSLog(@"認(rèn)證錯誤:%@",[error localizedDescription]);
12 }
13 }
14 else
15 {
16 UIAlertView *alter = [[UIAlertView alloc] initWithTitle:@"提示" message:@"密碼不能為空" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:nil];
17 [alter show];
18 }
19 }
復(fù)制代碼
(4)密碼認(rèn)證成功后的回調(diào)
復(fù)制代碼
1 //認(rèn)證成功后的回調(diào)
2 -(void)xmppStreamDidAuthenticate:(XMPPStream *)sender
3 {
4 NSLog(@"登陸成功");
5
6 //密碼進(jìn)入userDefault
7 NSUserDefaults *userDefult = [NSUserDefaults standardUserDefaults];
8 [userDefult setObject:self.userNameTextFiled.text forKey:@"username"];
9 [userDefult setObject:self.passwordTextFiled.text forKey:@"password"];
10
11 //設(shè)置在線狀態(tài)
12 XMPPPresence * pre = [XMPPPresence presence];
13 [self.xmppStream sendElement:pre];
14
15 UIStoryboard *storybard = [UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]];
16 UIViewController *viewController = [storybard instantiateViewControllerWithIdentifier:@"mainController"];
17 [self presentViewController:viewController animated:YES completion:^{
18 }];
19 }
復(fù)制代碼
(5)密碼認(rèn)證失敗后的回調(diào)
1 //認(rèn)證失敗的回調(diào)
2 -(void)xmppStream:sender didNotAuthenticate:(DDXMLElement *)error
3 {
4 NSLog(@"認(rèn)證失敗");
5 }
(6),二次登陸自動連接代碼:
復(fù)制代碼
1 // 如果已登錄就直接填充密碼登陸
2 NSUserDefaults *userDefult = [NSUserDefaults standardUserDefaults];
3
4 NSString *userName = [userDefult objectForKey:@"username"];
5 NSString *password = [userDefult objectForKey:@"password"];
6 NSLog(@"%@,%@",userName,password);
7 if (userName != nil password != nil ![userName isEqualToString:@""] ![password isEqualToString:@""])
8 {
9 self.userNameTextFiled.text = userName;
10 self.passwordTextFiled.text = password;
11 [self xmppConnect];
12 }
復(fù)制代碼
3.獲取好友列表的XMPPFramework的代碼實現(xiàn)
在獲取用戶列表的代碼中就會用到我們之前注冊的Roster的內(nèi)容,因為我們在實例化Roster的時候指定的保存策略是用CoreData進(jìn)行保存的,并且是自動獲取好友列表。所以在獲取好友列表的TableViewController中我們只需要通過CoreData來獲取好友列表即可。下面將給出獲取好友列表的核心代碼:
(1),獲取Roster對應(yīng)的上下文,用于獲取存儲在Roster相應(yīng)實體中的數(shù)據(jù)
1 //獲取Roster的上下文
2 UIApplication *application = [UIApplication sharedApplication];
3 id delegate = [application delegate];
4 self.xmppRosterManagedObjectContext = [delegate xmppRosterManagedObjectContext];
(2).獲取FetchRequst對象,并指定CoreData實體類,之后添加排序規(guī)則,代碼如下:
復(fù)制代碼
1 //從CoreData中獲取數(shù)據(jù)
2 //通過實體獲取FetchRequest實體
3 NSFetchRequest *request = [[NSFetchRequest alloc] initWithEntityName:NSStringFromClass([XMPPUserCoreDataStorageObject class])];
4 //添加排序規(guī)則
5 NSSortDescriptor * sortD = [NSSortDescriptor sortDescriptorWithKey:@"jidStr" ascending:YES];
6 [request setSortDescriptors:@[sortD]];
復(fù)制代碼
(3).獲取FetchedResultController并注冊回調(diào),用于自動刷新TableView,代碼如下:
1 //獲取FRC
2 self.fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:request managedObjectContext:self.xmppRosterManagedObjectContext sectionNameKeyPath:nil cacheName:nil];
3 self.fetchedResultsController.delegate = self;
(4)獲取存儲的內(nèi)容
復(fù)制代碼
1
2 //獲取內(nèi)容
3 NSError * error;
4 ;
5 if (![self.fetchedResultsController performFetch:error])
6 {
7 NSLog(@"%s %@",__FUNCTION__,[error localizedDescription]);
8 }
復(fù)制代碼
至于如何在TableView上顯示FetchedResultController獲取的數(shù)據(jù),請參考之前的博客:IOS開發(fā)之表視圖愛上CoreData。
最近聯(lián)系人的代碼和歷史表情的代碼類似,請參考之前的博客:iOS開發(fā)之微信聊天工具欄的封裝
聊頁面的實現(xiàn)請參考之前的博客:iOS開發(fā)之微信聊天頁面實現(xiàn)
今天的XMPPFramework就先到這兒吧,內(nèi)容也挺多的了,其實XMPPFramework中的組件使用方法都差不多,首先第初始化內(nèi)存,然后進(jìn)行相關(guān)配置,在后就是在XMPPStream中激活,最后就是如何使用了。
作者:青玉伏案
iPhone的ios系統(tǒng)有一個功能是push(即推送),可以從蘋果服務(wù)器將你的信息在線推送給你
00手機(jī)推送服務(wù)是指定向?qū)⑿畔崟r送達(dá)手機(jī)的服務(wù)。與常見的輪詢方式(偽推送)相比區(qū)別主要在于兩點(diǎn),一是否長聯(lián)網(wǎng),二是到達(dá)實時性。
可以在系統(tǒng)主屏幕界面的設(shè)置進(jìn)入,會找到一個叫“通知”的項,點(diǎn)開,將你所需推送服務(wù)的開關(guān)都打開
即時通訊肯定少不了通訊協(xié)議,在ios開發(fā)中基本上都使用基于XMPP 的協(xié)議來做即時通訊。在oc下有封裝好的XMPP協(xié)議庫,你在開發(fā)時可以調(diào)用XMPP的API來進(jìn)行開發(fā)。通訊的服務(wù)器可以用ejabberd來搭建。知道的就這些了
注釋掉就好了。
這個是個宏定義的判斷,只在MAC OSX10.5以下有效,現(xiàn)在都10.11了,這個注釋掉完全不影響,而且你開發(fā)的是手機(jī)應(yīng)用,更不影響了。