Skip to content

Instantly share code, notes, and snippets.

@ToeJamson
Last active December 19, 2015 20:48
Show Gist options
  • Select an option

  • Save ToeJamson/6015582 to your computer and use it in GitHub Desktop.

Select an option

Save ToeJamson/6015582 to your computer and use it in GitHub Desktop.
(void)pubnubClient:(PubNub *)client didReceiveMessage:(PNMessage *)message {
NSMutableDictionary *groupMessages = [[NSMutableDictionary alloc] init];
[groupMessages setDictionary: [[Globals sharedInstance] groupMess]];
NSMutableDictionary *nameDict = [[NSMutableDictionary alloc] init];
[nameDict setDictionary: [[Globals sharedInstance] nameDict]];
NSMutableString *groupName = [[NSMutableString alloc] init];
NSString* text = [[message.message allKeys] objectAtIndex:0];
NSString* group = [message.message objectForKey: text];
NSString* member = @""; // So we know who sent message
bool isFound = false;
// When checking, we append the name of the group to the name of
// the group member, and then compare
// This is done for added security. See (IBAction) sendButton in
// ViewController for more details
for(id key in nameDict) {
id value = [nameDict objectForKey: key];
for(id name in value) {
id num = [[[Globals sharedInstance] nameNumber] objectForKey: name];
if([group isEqual: [key stringByAppendingString: num]]){
groupName = [NSMutableString stringWithString: key];
isFound = true;
member = name; // So we know who sent message
break;
}
}
}
if(!isFound)
return;
NSString *str = [PNJSONSerialization stringFromJSONObject: text];
str = [str substringWithRange:NSMakeRange(1, [str length] - 2)];
//str = [str stringByAppendingString: member]; So we know who sent message No need because now using dict->array->dict
NSMutableArray *arr = [groupMessages valueForKey: groupName];
NSArray *dateSender = [[NSArray alloc] initWithObjects: [NSDate dateWithTimeIntervalSinceNow:0], member, nil];
NSMutableDictionary *msgDate = [[NSMutableDictionary alloc] init];
[msgDate setObject: dateSender forKey: str];
[arr addObject: msgDate];
[groupMessages removeObjectForKey: groupName];
[groupMessages setObject: arr forKey: groupName];
[[Globals sharedInstance] setGroupMess: groupMessages];
// Save variables
[[Globals sharedInstance] saveVariables];
}
-(IBAction)sendButton:(id)sender {
//Bubble data code
bubbleTable.typingBubble = NSBubbleTypingTypeNobody;
// Send message in dictionary form to all group memebers
NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
[dict setDictionary: [[Globals sharedInstance] nameDict]];
NSArray* names = [dict valueForKey: [[Globals sharedInstance] selectedGroupName]];
PNChannel *channels[[names count]];
// Set dictionary: Value is channel, key is msg.
// The value is actually the name of the group, with the name of the member append on
// For added security.
// Suppose someone sends a message to "Family" and they mistakenly add you to that group
// You will get the message, though that person is not part of your group!
// Thus we append names on the back for added security
NSMutableDictionary *msg = [[NSMutableDictionary alloc] init];
[msg setValue:[[[Globals sharedInstance] selectedGroupName] stringByAppendingString: [[Globals sharedInstance] userNumber]] forKey:message.text];
for(int j = 0; j < [names count]; j++) {
PNChannel *c = [PNChannel channelWithName: names[j] shouldObservePresence:YES];
channels[j] = c;
[PubNub sendMessage: msg toChannel:channels[j]];
}
NSMutableDictionary * msgDict = [[NSMutableDictionary alloc] init];
[msgDict setDictionary: [[Globals sharedInstance] groupMess]];
NSMutableArray *temp = [[NSMutableArray alloc] init];
temp = [msgDict valueForKey: [[Globals sharedInstance] selectedGroupName]] ;
NSArray *dateSender = [[NSArray alloc] initWithObjects: [NSDate dateWithTimeIntervalSinceNow:0], @"Me", nil];
NSMutableDictionary *msgDate = [[NSMutableDictionary alloc] init];
[msgDate setObject: dateSender forKey: message.text];
[temp addObject: msgDate];
[msgDict removeObjectForKey: [[Globals sharedInstance] selectedGroupName]];
[msgDict setObject: temp forKey: [[Globals sharedInstance] selectedGroupName]];
[[Globals sharedInstance] setGroupMess: msgDict];
// Call to ViewWillAppear so that most recent messages viewed
[self viewWillAppear: YES];
message.text = @"";
[message resignFirstResponder];
// Save variables
[[Globals sharedInstance] saveVariables];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment