9.添加好友 和删除

新建一个添加好友的ViewController为AddFriendViewController  和设置class

 连线text框 friendNameText

遵守<UITextFieldDlegate>

//设置文件框代理  //

-(BOOL)textFieldShouldReturn:(UITextField *)textField{

   //1.判断文本框是否输入内容   在.pch中导入 NSString+Helper.h头文件截断字符串 就不用每次都去导入头文件和LoginUser.h

  NSString *name = [textField.text trimstring];

  if (![name isEmptyString]){

    [self addFriendWithName:name];  //2.如果输入就调用添加好友方法

  }

  return YES;

 }

在viewDidLoad中 [_friendNameText becomeFirstResponder];  //进来就打开键盘

1. 自定义一个添加好友的方法

-(void)addFriendWithName:(NSString *)name{

   //1.判断是否由域名

  NSRange range = [name rangeOfString:@"@"];

 

  if (NSNotFound == renge.location){    //2.如果没有,添加域名完整的JID字符串  在name尾部添加域名

    name = [NSString stringWithFormat:@"%@@%@",name,[loginUser sharedLoginUser].hostName];

  }

  //3.判断是否与当前用户相同

  if([name isEqualToString:[LoginUser sharedLoginUser].myJIDName]){

  UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"温馨提示" message:@"自已不用添加自己"

                                              delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];

    [alert show];

  return;

  }

  //4.判断是否已经是自己的好友

  (在花名册模块    设置接收好友订阅请求

   [_xmppRoster setAutoAcceptKnownPresenceSubscriptionRequests:YES];

   [_xmppRoster activate:_xmppStream];  )  

  在文件中添加appDelegate代理头文件

    因为经常写就可以定义成宏  #define xmppDelegate (AppDelegate *)[[UIApplication sharedApplication]delegate]; 

  //5.发送添加好友请求  //双向添加

  [[xmppDelegate xmppRoster] subscribePresenceToUser:[XMPPJID jidWithString:name]];  

  //提示用户,并返回上一级页面

  

UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"添加好友请求已发送"

                                              delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];

   [alert show];

  

}

//遵守UIAlertViewDelegate代理  点击确定后跳转页面

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{

  [self.navigationController popViewControllerAnimated:YES];

}

   //单向添加

  在AppDelegate.m中遵守协议<XMPPStreamDelegate,XMPPRosterDelegate>

   在代理.m文件中 添加用户展现方法

   -(void)xmppStream:(XMPPStream *)sender didReceivePresence:(XMPPPresence *)presence{
  //接收用户展示数据  意思 接收用户发送添加好友消息

  // 判断 接收到的presence类型是否为subscribe

    if([presence.type isEqualToString:@"subscribe"]{

  //2.取出presence中的from的JID

  XMPPJID *from = [presence from];

  //3.接受来自from添加为好友的订阅请求 

  [_xmppRoster subscribePresenceToUser:from]; 

    }

  }

   -(void)xmppRoster:(XMPPRoster *)sender didReceivePresenceSubscriptionRequest:(XMPPPresence *)presence{

  //接收到其它用户的请求

  }

  //  表格代理方法、允许表格滑动删除  1、 实现tableView:canEditRowAtIndexPath:方法,允许表格边界

   

  -(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{ return YES;}

  -(void)tableView:UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{//2.提交编辑状态

  if(UITableViewCellEditingStyleDelegate == editingStyle){

  //删除数据  在OC开发中,是MVC架构,数据是绑定到表格,如果要删除表格中的数据,应该先删除数据,再刷新表格

  XMPPUserCoreDataStorageObject *user = [_fetchedResultsController objectAtIndexPath:indexPath];

//提示用户

  UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"是否删除好友" message:user.jidStr

                                              delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];

  _removedIndexPath = indexPath;  //记录要删除表格的索引

  [alert show];

  

}

  }

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{

  

  //删除好友

if (1 == buttonIndex){

  //用indexPathForSelectedRow是获取不到被删除的行

    XMPPUserCoreDataStorageObject *user = [_fetchedResultsController objectAtIndexPath:_removedIndexPath];

    [[xmppDelegate xmppRoster]removeUser:user.jid];

  //定义一个成员变量记录要删除表格的索引   NSIndexPath  *_removedIndexPath;

  }

}

  

原文地址:https://www.cnblogs.com/qq907374866/p/4277197.html