1.下载并且导入XMPP
2.导入需要的框架
3.build setting --> Header Search Path 设置为 /usr/include/libxml2 -->Other Linker Flags 设置为-lxml2
4.
AppDelegate 里面添加
#import <CoreData/CoreData.h>
#import "XMPPFramework.h"
#import "GCDAsyncSocket.h"
#import "XMPP.h"
#import "XMPPLogging.h"
#import "XMPPReconnect.h"
#import "XMPPCapabilitiesCoreDataStorage.h"
#import "XMPPRosterCoreDataStorage.h"
#import "XMPPvCardAvatarModule.h"
#import "XMPPvCardCoreDataStorage.h"
#import "DDLog.h"
#import "DDTTYLogger.h"
5.代理
<
UIApplicationDelegate, XMPPRosterDelegate
XMPPStreamDelegate>6.实现代理方法,并且连接网络,这个可以参照
7.
- (void)xmppStreamDidConnect:(XMPPStream *)sender
{
NSLog(@"与主机连接成功,发送密码进行授权");
DDLogVerbose(@"%@: %@", THIS_FILE, THIS_METHOD);
isXmppConnected = YES;
NSError *error = nil;
if (isReg == NO) {
if (![xmppStream registerWithPassword:password error:&error]) {
DDLogError(@"注册发送密码错误Error authenticating: %@", error);
}else{
isReg = YES;
}
}else{
if (![[self xmppStream] authenticateWithPassword:password error:&error])
{
DDLogError(@"验证密码错误Error authenticating: %@", error);
}
}
}
//这里注意,没有注册的需要先注册
账号为:user@主机名
我犯了个错就是@后面是主机名,我写的是IP地址,结果注册成功,一直登陆失败。。。
需要特别注意
- (void)xmppStreamDidAuthenticate:(XMPPStream *)sender
{
DDLogVerbose(@"授权成功%@: %@", THIS_FILE, THIS_METHOD);
[self goOnline];
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"登录成功"
message:@"登录成功"
delegate:nil
cancelButtonTitle:@"Ok"
otherButtonTitles:nil];
[alertView show];
}
- (void)xmppStream:(XMPPStream *)sender didNotAuthenticate:(NSXMLElement *)error
{
NSLog(@"调用失败%@: %@", THIS_FILE, THIS_METHOD);
NSLog(@"%@",[[error elementForName:@"error"] stringValue]);
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"密码错误"
message:@"密码错误"
delegate:nil
cancelButtonTitle:@"Ok"
otherButtonTitles:nil];
[alertView show];
}
- (void)xmppStream:(XMPPStream *)sender didReceiveMessage:(XMPPMessage *)message
{
NSLog(@"发送消息%@: %@", THIS_FILE, THIS_METHOD);
- (void)xmppStreamDidRegister:(XMPPStream *)sender {
NSLog(@"注册成功");
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"注册成功"
message:@"注册成功"
delegate:nil
cancelButtonTitle:@"Ok"
otherButtonTitles:nil];
[alertView show];
isReg = YES;
}
//注册失败
- (void)xmppStream:(XMPPStream *)sender didNotRegister:(NSXMLElement *)error
{
DDLogVerbose(@"%@: %@", THIS_FILE, THIS_METHOD);
NSLog(@"%@",[[error elementForName:@"error"] stringValue]);
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"注册失败"
message:@"注册失败"
delegate:nil
cancelButtonTitle:@"Ok"
otherButtonTitles:nil];
[alertView show];
isReg = NO;
}
#pragma mark XMPPRosterDelegate
- (void)xmppRoster:(XMPPRoster *)sender didReceiveBuddyRequest:(XMPPPresence *)presence
{
NSLog(@"好友名称%@,好友状态%@",presence.name,presence.status);
DDLogVerbose(@"%@: %@", THIS_FILE, THIS_METHOD);
XMPPUserCoreDataStorageObject *user = [xmppRosterStorage userForJID:[presence from]
xmppStream:xmppStream
managedObjectContext:[self managedObjectContext_roster]];
NSString *displayName = [user displayName];
NSString *jidStrBare = [presence fromStr];
NSString *body = nil;
if (![displayName isEqualToString:jidStrBare])
{
body = [NSString stringWithFormat:@"Buddy request from %@ <%@>", displayName, jidStrBare];
}
else
{
body = [NSString stringWithFormat:@"Buddy request from %@", displayName];
}
if ([[UIApplication sharedApplication] applicationState] == UIApplicationStateActive)
{
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:displayName
message:body
delegate:nil
cancelButtonTitle:@"Not implemented"
otherButtonTitles:nil];
[alertView show];
}
else
{
// We are not active, so use a local notification instead
UILocalNotification *localNotification = [[UILocalNotification alloc] init];
localNotification.alertAction = @"Not implemented";
localNotification.alertBody = body;
[[UIApplication sharedApplication] presentLocalNotificationNow:localNotification];
}
}
//这里发的只是部分代码,,详细的可以百度参考,,,