- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return 20; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath]; cell.textLabel.text = @"Vasiliy Pupkin"; return cell; }
#import <UIKit/UIKit.h> // @interface NKLoginViewController : UIViewController <UITextFieldDelegate> @end
#import "NKLoginViewController.h" @interface NKLoginViewController () @property (weak, nonatomic) IBOutlet UITextField *emailTextField; @property (weak, nonatomic) IBOutlet UITextField *passwordTextField; - (IBAction)loginTouched:(UIButton *)sender; @end @implementation NKLoginViewController #pragma mark - UITextFieldDelegate - // , "Done" - (BOOL)textFieldShouldReturn:(UITextField *)textField { [textField resignFirstResponder]; return YES; } #pragma mark - Button methods - // "". - (IBAction)loginTouched:(UIButton *)sender { [self performSegueWithIdentifier:@"SegueToChatList" sender:self]; }
#import "NKChatViewController.h" @interface NKChatViewController () @property (weak, nonatomic) IBOutlet UITableView *tableView; @property (weak, nonatomic) IBOutlet UITextField *messageTextField; - (IBAction)sendTouched:(UIButton *)sender; @end @implementation NKChatViewController #pragma mark - View Controller life cycle - - (void)viewDidAppear:(BOOL)animated { [super viewDidAppear:animated]; // , [_messageTextField becomeFirstResponder]; } #pragma mark - UITableViewDataSource - - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return 20; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"]; cell.textLabel.text = @" "; cell.detailTextLabel.text = @", ?"; return cell; } #pragma mark - Button methods - - (IBAction)sendTouched:(UIButton *)sender { } @end
#import <UIKit/UIKit.h> #import <SocialCommunication/SocialCommunication.h> @interface NKAppDelegate : C2CallAppDelegate <UIApplicationDelegate> @property (strong, nonatomic) UIWindow *window; @end
@implementation NKAppDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { self.affiliateid = @"6B9DF5671444320162B"; self.secret = @"2fd9cd18aa4d957a4030c0455101646d"; return [super application:application didFinishLaunchingWithOptions:launchOptions]; } @end
#import "NKChat.h" #import <SocialCommunication/SocialCommunication.h> @implementation NKChat #pragma mark - Singleton pattern - // 1 + (instancetype)sharedManager { static NKChat *sharedChat = nil; static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ sharedChat = [self new]; }); return sharedChat; } #pragma mark - Accessors - // 2 - (NSArray *)chatHistory { return [self fetchChatHistory]; } #pragma mark - General methods - // 3 - (void)login:(NSString *)email password:(NSString *)password success:(void(^)())successBlock failure:(void(^)())failureBlock { NSDictionary *dictionary = @{@"EMail":email, @"Password":password}; [[C2CallPhone currentPhone] registerUser:dictionary withCompletionHandler:^(BOOL success, NSString *result) { if (success) { [[C2CallPhone currentPhone] startC2CallPhone]; successBlock(); } else { failureBlock(); } }]; } // 4 - (void)logout { [(C2CallAppDelegate *)[UIApplication sharedApplication].delegate logoutUser]; } // 5 - (void)sendMessage:(NSString *)message toUser:(NSString *)userId { [[C2CallPhone currentPhone] submitMessage:message toUser:userId]; } // 6 - (NSArray *)fetchChatHistory { // Managed Object NSFetchRequest *request = [[SCDataManager instance] fetchRequestForChatHistory:YES]; NSFetchedResultsController *controller = [[SCDataManager instance] fetchedResultsControllerWithFetchRequest:request sectionNameKeyPath:nil cacheName:nil]; NSError *error; [controller performFetch:&error]; // NSMutableArray *result = [NSMutableArray array]; for (NSManagedObject *chat in controller.fetchedObjects) { // NSArray *chatKeys = @[@"contact", @"lastTimestamp", @"missedEvents"]; NSMutableDictionary *inChat = [[chat dictionaryWithValuesForKeys:chatKeys] mutableCopy]; // NSMutableDictionary *dublicate = nil; for (NSMutableDictionary *dict in result) { if ([dict[@"contact"] isEqualToString:inChat[@"contact"]]) { dublicate = dict; break; } } // NSMutableArray *messages = (dublicate) ? dublicate[@"messages"] : [NSMutableArray array]; for (NSManagedObject *chatEvent in [chat valueForKey:@"chatHistory"]) { NSArray *chatEventKeys = [[[chatEvent entity] attributesByName] allKeys]; NSMutableDictionary *inChatEvent = [[chatEvent dictionaryWithValuesForKeys:chatEventKeys] mutableCopy]; // NSLog(@"%@",inChatEvent); inChatEvent[@"ManagedObject"] = chatEvent; [messages addObject:inChatEvent]; } [messages sortUsingDescriptors:@[[NSSortDescriptor sortDescriptorWithKey:@"timevalue" ascending:YES]]]; if (dublicate) { dublicate[@"messages"] = messages; [dublicate[@"ManagedObjects"] addObject:chat]; dublicate[@"missedEvents"] = @([dublicate[@"missedEvents"] intValue] + [inChat[@"missedEvents"] intValue]); if (!dublicate[@"name"]) dublicate[@"name"] = inChat[@"name"]; } else { inChat[@"messages"] = messages; inChat[@"ManagedObjects"] = [NSMutableArray arrayWithObject:chat]; } // if (!dublicate) [result addObject:inChat]; } // [result sortUsingDescriptors:@[[NSSortDescriptor sortDescriptorWithKey:@"lastTimestamp" ascending:NO]]]; // return [result copy]; } @end
#import "NKAppDelegate.h" @implementation NKAppDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { self.affiliateid = @"6B9DF5671444320162B"; self.secret = @"2fd9cd18aa4d957a4030c0455101646d"; [NKChat sharedManager]; return [super application:application didFinishLaunchingWithOptions:launchOptions]; } @end
- (IBAction)loginTouched:(UIButton *)sender { sender.enabled = NO; [[NKChat sharedManager] login:_emailTextField.text password:_passwordTextField.text success:^{ [self performSegueWithIdentifier:@"SegueToChatList" sender:self]; sender.enabled = YES; } failure:^{ sender.enabled = YES; }]; }
#import "NKChatListTableViewController.h" @interface NKChatListTableViewController () @end @implementation NKChatListTableViewController #pragma mark - UITableViewDataSource - - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return 2; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath]; cell.textLabel.text = (indexPath.row) ? @"nikita@borodutch.com" : @"luke@borodutch.com"; return cell; } @end
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(UITableViewCell *)sender { UIViewController *dest = segue.destinationViewController; dest.title = sender.textLabel.text; }
#import "NKChatViewController.h" #import <SocialCommunication/SocialCommunication.h> #import "NKChat.h" @interface NKChatViewController () @property (weak, nonatomic) IBOutlet UITableView *tableView; @property (weak, nonatomic) IBOutlet UITextField *messageTextField; - (IBAction)sendTouched:(UIButton *)sender; @end @implementation NKChatViewController { NSArray *tableData; } #pragma mark - View Controller life cycle - - (void)viewDidLoad { [super viewDidLoad]; // 1 tableData = [self getTableData]; } - (void)viewDidAppear:(BOOL)animated { [super viewDidAppear:animated]; // , [_messageTextField becomeFirstResponder]; // 2 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receivedMessage) name:@"kC2CallPhoneReceivedMessage" object:nil]; } #pragma mark - UITableViewDataSource - - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { // 3 return tableData.count; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { // 4 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"]; cell.textLabel.text = ([tableData[indexPath.row][@"eventType"] isEqualToString:@"MessageIn"]) ? self.title : @""; cell.detailTextLabel.text = tableData[indexPath.row][@"text"]; return cell; } #pragma mark - Button methods - - (IBAction)sendTouched:(UIButton *)sender { // 5 [[NKChat sharedManager] sendMessage:_messageTextField.text toUser:@"c45645f71465dcff18e"]; [self addMessage:_messageTextField.text]; _messageTextField.text = @""; } #pragma mark - General Methods - - (void)addMessage:(NSString *)message { // 6 NSMutableArray *mTableData = [tableData mutableCopy]; [mTableData addObject:@{@"text":message, @"eventType":@"MessageOut"}]; tableData = mTableData; [_tableView reloadData]; } - (void)receivedMessage { // 7 tableData = [self getTableData]; [_tableView reloadData]; } - (NSArray *)getTableData { // 8 for (NSDictionary *chat in [NKChat sharedManager].chatHistory) if ([chat[@"contact"] isEqualToString:self.title]) return chat[@"messages"]; return nil; } @end
Source: https://habr.com/ru/post/225079/
All Articles