这里记录下我配置XMPP的过程
首先下载XMPP:https://github.com/robbiehanson/XMPPFramework/
将如下文件考入过程中:
添加依赖框架
添加XML框架的依赖路径:
这样就配置完了。下面说下如何进行登陆。当然服务器要自己先搭好,如果没有搭好可以看前一篇。
登陆代码:
@interface XMPPModel()<XMPPStreamDelegate> @property (nonatomic , strong) XMPPStream *xmppStream; @end
- (BOOL) connect
{
[self setupStream];
//这里为了减少代码数量所以直接在这里写死了。大家测试时直接修改就好。
// 从本地取得用户名和密码和服务器地址
NSString *userId = @"dujia"; //用户名
NSString *password = @"zxcvbnm"; //密码
NSString *server = @"10.86.102.70"; //服务器的IP
if (![_xmppStream isDisconnected]) {
return YES;
}
// 下面3个参数分别是 用户名 domain 域名 resource 这个ming
XMPPJID *jid = [XMPPJID jidWithUser:userId domain:@"shenqi" resource:@"Ework"];
[_xmppStream setMyJID:jid];
[_xmppStream setHostName:server];
[_xmppStream setHostPort:5222];
NSError *error = nil;
if (![_xmppStream connectWithTimeout:3.0 error:&error]) {
NSLog(@"%@",error.userInfo);
}
return YES;
}
- (void)setupStream
{
_xmppStream = [[XMPPStream alloc] init];
[_xmppStream addDelegate:self delegateQueue:dispatch_get_main_queue()];
}- (void)xmppStreamDidConnect:(XMPPStream *)sender
{
NSError *error = nil;
if (![sender authenticateWithPassword:@"zxcvbnm" error:&error]) {
NSLog(@"%@",error.userInfo);
}
}
这样就登陆成功了。下面再说下退出。
- (void)goOffline
{
XMPPPresence *presence = [XMPPPresence presenceWithType:@"unavailable"];
[_xmppStream sendElement:presence];
[_xmppStream disconnect];
}
原文地址:http://blog.csdn.net/qqmcy/article/details/42102611