// xmpp登录之前。 我们应该做的就是 遵守XMPP 的协议, 代理
#pragma XMPP 连接
- (AppDelegate *)appDelegate
{
AppDelegate *delegate = (AppDelegate *)[[UIApplicationsharedApplication]delegate];
// delegate.chatDelegate = self;
return delegate;
}
- (void)connectToOpenfire
{
if (![selfallInformationReady]) {
return;
}
[[selfappDelegate]myConnect];
}
-(BOOL)allInformationReady
{
NSString *strxmppassword = @"123456";
[[[selfappDelegate]xmppStream]setHostName:Ip];
[[[selfappDelegate]xmppStream]setHostPort:5222];
[[NSUserDefaults standardUserDefaults]setObject:IpforKey:@"host"];
[[NSUserDefaults standardUserDefaults]setObject:[NSStringstringWithFormat:@"%@@%@/%@",strXmppid,@"www.xmpp.com",@"spark"]forKey:@"myjid"];
[[NSUserDefaults standardUserDefaults]setObject:strxmppasswordforKey:@"password"];
return YES;
}
// 添加好友 (通过JID 添加)
NSString *stris = [NSStringstringWithFormat:@"@"jid"@www.xmpp.com"];
NSLog(@"nihao %@",stris);
[[[selfappDelegate]xmppRoster]addUser:[XMPPJIDjidWithString:stris]withNickname:@" "];
//获取好友
#pragma get Friend
- (AppDelegate *)appDelegate
{
AppDelegate *delegate = (AppDelegate *)[[UIApplicationsharedApplication]delegate];
delegate.chatDelegate = self;
return delegate;
}
-(void)getData
{
if ([myToolsNetworkReachable])// 判断是否有网络
{
// 通过JID 获取好友。 可从控制台看出与是否有好友关系 (部分变量根据自己项目而写)
NSManagedObjectContext *context = [[[selfappDelegate]xmppRosterStorage]mainThreadManagedObjectContext];
NSEntityDescription *entity = [NSEntityDescriptionentityForName:@"XMPPUserCoreDataStorageObject"inManagedObjectContext:context];
NSFetchRequest *request = [[NSFetchRequestalloc]init];
[request setEntity:entity];
NSError *error ;
NSArray *friends = [context executeFetchRequest:request error:&error];
NSLog(@" arrFriend %@",friends);
for (int i =0; i < [friendscount]; i++)
{
XMPPUserCoreDataStorageObject *object = [friendsobjectAtIndex:i];
if (![[[object jid] user] isEqualToString:@"(null)"])
{
[arrFriend addObject:object];
}
}
for (int j =0 ; j < [arrFriendcount]; j++)
{
XMPPUserCoreDataStorageObject *object = [arrFriendobjectAtIndex:j];
XMPPJID *fromJid = [object jid];
NSMutableDictionary *dictMess = [[NSMutableDictionaryalloc]initWithObjectsAndKeys:fromJid,@"name",@"",@"lastmessage",@"离线",@"status",nil];
if ([[[[object primaryResource] presence] type]isEqualToString:@"available"])
{
[dictMess setObject:@"在线"forKey:@"status"];
}
else
{
[dictMess setObject:@"离线"forKey:@"status"];
}
[arrFriendMessage addObject:dictMess];
}
}else
{
UIAlertView *altNeterror = [[UIAlertViewalloc]initWithTitle:@"警告"message:@"失去连接,请检查网路" delegate:selfcancelButtonTitle:@"确定"otherButtonTitles:nil,nil];
[altNeterror show];
}
//聊天界面
#pragma mark - my method
- (AppDelegate *)appDelegate
{
AppDelegate *delegate = (AppDelegate *)[[UIApplicationsharedApplication]delegate];
delegate.chatDelegate = self;
return delegate;
}
- (void)getMessageDataWithMessage:(XMPPMessage *)message
{
NSManagedObjectContext *context = [[selfappDelegate].xmppMessageArchivingCoreDataStoragemainThreadManagedObjectContext];
NSEntityDescription *entityDescription = [NSEntityDescriptionentityForName:@"XMPPMessageArchiving_Message_CoreDataObject"inManagedObjectContext:context];
NSFetchRequest *request = [[NSFetchRequestalloc]init];
[request setEntity:entityDescription];
NSError *error ;
NSArray *messages = [context executeFetchRequest:request error:&error]; // 获取到的所有聊天记录
// 注意 为了分别自己发送的喝别人发送的消息。我们可以在发送的时候添加一个标示、 (接收左边朋友消息,右边显示自己发送消息);
XMPP 自带了聊天记录、以及离线消息。 离线消息较多的时候会有点卡。
发送图片 通过BASE64 将图片转化为字节流。 发送过去之后在通过BASE64反编译, 则会是图片。、