Day.04.26 XMPP 登陆 注册

1.引入XMPP文件夹
2.在build Phases引入libxml2 libsolv
3.在build settings搜search Paths 更改/usr/include/libxml2
4.创建一个pch文件

import

define kXMPP_MESSAGE_CHANGE @"XMPP_MESSAGE_CHANGE"

5.build settings 更改pch文件名

编译没有错误

viewController.h

#import "ViewController.h"
#import "XMPPFramework.h"

@interface ViewController ()
{
    XMPPStream *_xmppStream;
    
    XMPPRoster *_xmppRoster;//管理好友列表
    
    XMPPRosterMemoryStorage *_xmppRosterMemoryStorage;//好友存储
}
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    //登录
    [self login];

    //注册
//    [self registerUser];
}

//创建stream
-(void)createStream
{
    //初始化xmppStream对象,用来连接服务器
    _xmppStream = [[XMPPStream alloc] init];
    
    //设置代理
    [_xmppStream addDelegate:self delegateQueue:dispatch_get_main_queue()];
    
    //设置服务器的端口号
    [_xmppStream setHostPort:5222];
    
    //设置服务器地址
    [_xmppStream setHostName:@"127.0.0.1"];
    
    
    //创建好友管理模块
    
    //好友存储容器
    _xmppRosterMemoryStorage = [[XMPPRosterMemoryStorage alloc] init];
    
    //好友花名册管理
    _xmppRoster = [[XMPPRoster alloc] initWithRosterStorage:_xmppRosterMemoryStorage];
    
    //在xmppStream中激活好友管理模块
    [_xmppRoster activate:_xmppStream];
    
    //设置代理
    [_xmppRoster addDelegate:self delegateQueue:dispatch_get_main_queue()];
    
    //同步服务器中好友到本地
    [_xmppRoster setAutoFetchRoster:YES];
   
}

-(void)login
{
    [self createStream];
       //创建用户
    XMPPJID *userJID = [XMPPJID jidWithUser:@"admin" domain:@"wangjin" resource:@"iOS"];
    
    //将用户设置给Stream
    [_xmppStream setMyJID:userJID];
    
    //声明ERROR
    NSError *error;
    //连接服务器
    [_xmppStream connectWithTimeout:10 error:&error];
    
    if (error) {
        NSLog(@"error is:%@",error);
    }
}

-(void)registerUser
{
    [self createStream];
    
    //创建用户
    XMPPJID *userJid = [XMPPJID jidWithUser:@"qwer" domain:@"wangjin" resource:@"iOS"];
    
    [_xmppStream setMyJID:userJid];
    
    //连接服务器
    [_xmppStream connectWithTimeout:10 error:nil];
    
}

#pragma mark --------- XMPPDelegate
-(void)xmppStreamDidConnect:(XMPPStream *)sender
{
    NSLog(@"连接成功");
    
    //连接成功后验证用户密码
    [sender authenticateWithPassword:@"admin" error:nil];
    
    
    //注册用户
//    [sender registerWithPassword:@"qwer" error:nil];
    
}

-(void)xmppStreamDidDisconnect:(XMPPStream *)sender withError:(NSError *)error
{
    NSLog(@"连接失败");
}

#pragma mark ---------- 验证成功或失败
-(void)xmppStreamDidAuthenticate:(XMPPStream *)sender
{
    NSLog(@"验证成功");
}

-(void)xmppStream:(XMPPStream *)sender didNotAuthenticate:(DDXMLElement *)error
{
    NSLog(@"验证失败");
}

#pragma mark ---------- 注册消息
-(void)xmppStreamDidRegister:(XMPPStream *)sender
{
    NSLog(@"注册成功");
}

-(void)xmppStream:(XMPPStream *)sender didNotRegister:(DDXMLElement *)error
{
    NSLog(@"注册失败");
}

#pragma mark ---------- 好友列表
//开始同步好友
-(void)xmppRosterDidBeginPopulating:(XMPPRoster *)sender
{
    NSLog(@"开始同步好友");
}

-(void)xmppRosterDidEndPopulating:(XMPPRoster *)sender
{
    NSLog(@"结束好友同步");
    
    NSArray *arr = [_xmppRosterMemoryStorage unsortedUsers];
    
    NSLog(@"friends is:%@",arr);
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

你可能感兴趣的:(Day.04.26 XMPP 登陆 注册)