Click here to Skip to main content
14,976,809 members
Home / Discussions / Objective-C and Swift
   

Objective-C and Swift

 
QuestionSimultaneous Microphone Access on iPhone Pin
Member 123773388-Mar-16 0:35
MemberMember 123773388-Mar-16 0:35 
AnswerMessage Closed Pin
22-Nov-16 22:38
MemberMember 1286606322-Nov-16 22:38 
QuestionObjective C first program from the book, warning messages Pin
Alla__10-Nov-15 1:44
MemberAlla__10-Nov-15 1:44 
AnswerRe: Objective C first program from the book, warning messages Pin
BharathReddy V12-Jan-16 2:04
MemberBharathReddy V12-Jan-16 2:04 
QuestionLinker command failed with exit code 1 (use -v to see invocation) in xcode 5.1.1 Pin
Member 1198376314-Sep-15 10:17
MemberMember 1198376314-Sep-15 10:17 
QuestionImagePickerController image size Pin
liel333-Aug-15 11:30
Memberliel333-Aug-15 11:30 
AnswerRe: ImagePickerController image size Pin
biji cabe18-Dec-16 22:25
Memberbiji cabe18-Dec-16 22:25 
QuestioniOS XMPP received message in coversation screen Pin
Member 1176696715-Jun-15 2:37
MemberMember 1176696715-Jun-15 2:37 
I am working on XMPP based project. i am able to send message and it displays in conversation screen but when i receive message it only show in alertview not able to see in conversation screen. help me please.


 - (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(messageReceived:) name:MessageRecivedNotif object:nil];

appdelegate=(AppDelegate*)[[UIApplication sharedApplication] delegate];
[self getAllMessagesArrayWithOppositeUser:_jid];

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(keyboardWasShown:)
                                             name:UIKeyboardDidShowNotification
                                           object:nil];


}

#pragma mark table delegate and datasource methods
 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return data.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tblobj dequeueReusableCellWithIdentifier:@"cell"];
UILabel *lbl;
if (cell==nil)
{
    cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];

    if (chatLblRight == 1)
    {
        lbl = [[UILabel alloc]initWithFrame:CGRectMake(200, 10, 150, 44)];
        [lbl setTextColor:[UIColor redColor]];
        [cell.contentView   addSubview:lbl];
    }


 }
 if (chatLblRight == 1)
 {
    lbl.text = [[data objectAtIndex:indexPath.row]valueForKey:@"text"];
    chatLblRight = 0;
    lbl = [[UILabel alloc]initWithFrame:CGRectMake(200, 10, 150, 44)];
    [lbl setTextColor:[UIColor redColor]];
    [cell.contentView   addSubview:lbl];

    return cell;
  }
  else
  {
    cell.textLabel.text=[[data objectAtIndex:indexPath.row]valueForKey:@"text"];
    return cell;
  }
}
-(void)messageReceived:(NSNotification*)notif
{
XMPPMessage *message=(XMPPMessage*)notif.object;
NSString *body = [[message elementForName:@"body"] stringValue];
NSMutableDictionary *dic_recive = [[NSMutableDictionary alloc]init];
[dic_recive setObject:body forKey:@"text"];
[data addObject:dic_recive];
chatLblRight = 1;
[tblobj reloadData];
}
 -(IBAction)btnsend:(id)sender
{
[txtmsg resignFirstResponder];
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.2];
[msgview setFrame:CGRectMake(0, self.view.bounds.size.height-53, self.view.bounds.size.width, 53)];
[UIView commitAnimations];
[((AppDelegate*)[[UIApplication sharedApplication]delegate]) sendMessage:txtmsg.text toUserWithJid:_jid];
NSMutableDictionary *dic_send = [[NSMutableDictionary alloc]init];
[dic_send setObject:txtmsg.text forKey:@"text"];
[data addObject:dic_send];
[tblobj reloadData];
txtmsg.text=@"";
 }
-(NSMutableArray *)getAllMessagesArrayWithOppositeUser:(XMPPJID *)xmppUser
{
XMPPMessageArchivingCoreDataStorage *storage = [XMPPMessageArchivingCoreDataStorage sharedInstance];
NSManagedObjectContext *moc = [storage mainThreadManagedObjectContext];
NSEntityDescription *entityDescription = [NSEntityDescription entityForName:@"XMPPMessageArchiving_Message_CoreDataObject"
                                                     inManagedObjectContext:moc];
NSFetchRequest *request = [[NSFetchRequest alloc]init];
[request setEntity:entityDescription];
NSError *error;
NSArray *messages = [moc executeFetchRequest:request error:&error];

data=[[NSMutableArray alloc] init];

@autoreleasepool {
    for (XMPPMessageArchiving_Message_CoreDataObject *message in messages) {

        if ([message.bareJid isEqual:xmppUser])
        {
            NSMutableDictionary *dict=[[NSMutableDictionary alloc] init];
            [dict setObject:message.body forKey:@"text"];
            [dict setObject:message.timestamp forKey:@"date"];
            if ([message isOutgoing]) {
                [dict setObject:@"1" forKey:@"type"];
                [dict setObject:[[message.bareJidStr componentsSeparatedByString:@"@"] firstObject] forKey:@"username"];
                [dict setObject:[UIColor greenColor] forKey:@"color"];
            }
            else{

                [dict setObject:@"2" forKey:@"type"];
                [dict setObject:[[message.bareJidStr componentsSeparatedByString:@"@"] firstObject] forKey:@"username"];
                //                    [dict setObject:userName forKey:@"username"];
                [dict setObject:[UIColor blueColor] forKey:@"color"];
             }
            [data addObject:dict];
          }
      }
  }

   return data;
}

Where is my mistake ?

EDIT:-
- (void)xmppStream:(XMPPStream *)sender didReceiveMessage:(XMPPMessage *)message
{
    
    DDLogVerbose(@"%@: %@", THIS_FILE, THIS_METHOD);
[[self xmppStream] sendElement:message];
    
    UILocalNotification *localNotification = [[UILocalNotification alloc] init];
    localNotification.alertAction = @"OK";
    localNotification.fireDate = [NSDate date];
    //localNotification.alertBody = xmppmessage;
    localNotification.soundName = UILocalNotificationDefaultSoundName;
    [[UIApplication sharedApplication] presentLocalNotificationNow:localNotification];

    
    
    
    // A simple example of inbound message handling.
    
   if ([message isChatMessageWithBody])
    {
        XMPPUserCoreDataStorageObject *user = [xmppRosterStorage userForJID:[message from]
                                                                 xmppStream:xmppStream
                                                       managedObjectContext:[self managedObjectContext_roster]];
        
        NSString *body = [[message elementForName:@"body"] stringValue];
        NSString *displayName = [user displayName];
        
        if ([[UIApplication sharedApplication] applicationState] == UIApplicationStateActive)
        {
            UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:displayName
                                                                message:body
                                                               delegate:nil
                                                      cancelButtonTitle:@"Ok"
                                                      otherButtonTitles:nil];
            [alertView show];
        }
        else
        {
            // We are not active, so use a local notification instead
            UILocalNotification *localNotification = [[UILocalNotification alloc] init];
            localNotification.alertAction = @"Ok";
            localNotification.alertBody = [NSString stringWithFormat:@"From: %@\n\n%@",displayName,body];
            
            [[UIApplication sharedApplication] presentLocalNotificationNow:localNotification];
        }
    }


modified 16-Jun-15 2:45am.

SuggestionRe: iOS XMPP received message in coversation screen Pin
Richard MacCutchan15-Jun-15 4:19
mveRichard MacCutchan15-Jun-15 4:19 
GeneralRe: iOS XMPP received message in coversation screen Pin
Member 1176696715-Jun-15 20:42
MemberMember 1176696715-Jun-15 20:42 
AnswerRe: iOS XMPP received message in coversation screen Pin
Dennis E White17-Jun-15 5:54
professionalDennis E White17-Jun-15 5:54 
Questionmake a system monitor using obj c. Pin
Member 116779679-May-15 13:00
MemberMember 116779679-May-15 13:00 
AnswerRe: make a system monitor using obj c. Pin
Richard MacCutchan9-May-15 21:10
mveRichard MacCutchan9-May-15 21:10 
GeneralRe: make a system monitor using obj c. Pin
Dennis E White15-May-15 5:25
professionalDennis E White15-May-15 5:25 
GeneralRe: make a system monitor using obj c. Pin
Richard MacCutchan15-May-15 5:41
mveRichard MacCutchan15-May-15 5:41 
GeneralRe: make a system monitor using obj c. Pin
Dennis E White15-May-15 6:04
professionalDennis E White15-May-15 6:04 
GeneralApp that converts human years to dog years Pin
Member 115920608-Apr-15 15:44
MemberMember 115920608-Apr-15 15:44 
GeneralRe: App that converts human years to dog years Pin
Dennis E White11-Apr-15 9:34
professionalDennis E White11-Apr-15 9:34 
GeneralRe: App that converts human years to dog years Pin
Richard MacCutchan11-Apr-15 22:05
mveRichard MacCutchan11-Apr-15 22:05 
GeneralRe: App that converts human years to dog years Pin
Dennis E White13-Apr-15 10:09
professionalDennis E White13-Apr-15 10:09 
QuestionCan anyone help me solve this project? Pin
Member 115772012-Apr-15 13:46
MemberMember 115772012-Apr-15 13:46 
AnswerRe: Can anyone help me solve this project? Pin
Sascha Lefèvre2-Apr-15 14:23
professionalSascha Lefèvre2-Apr-15 14:23 
AnswerRe: Can anyone help me solve this project? Pin
Dennis E White11-Apr-15 9:32
professionalDennis E White11-Apr-15 9:32 
AnswerRe: Can anyone help me solve this project? Pin
Peter Webb5-May-15 3:30
MemberPeter Webb5-May-15 3:30 
QuestionLaunch Safari from another app then Autofill username and password Pin
picardy8-Jan-15 8:45
Memberpicardy8-Jan-15 8:45 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.