iOSXMPP登录、添加好友、退出

//  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反编译, 则会是图片。、



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值