forked from balderdashy/sails-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathViewController.m
More file actions
165 lines (130 loc) · 5.54 KB
/
ViewController.m
File metadata and controls
165 lines (130 loc) · 5.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
//
// ViewController.m
// SampleAppiOS
//
// Created by Reza Fatahi on 8/8/13.
// Copyright (c) 2013 Rex Fatahi. All rights reserved.
//
#import "ViewController.h"
@interface ViewController () <NSStreamDelegate>
{
SocketIO* socketIO;
__weak IBOutlet UILabel *oLabel;
__weak IBOutlet UITextField *oTextField;
__weak IBOutlet UITextView *oTextView;
__weak IBOutlet UITableView *oTableView;
NSMutableData* mutableData;
NSString* suggestedFilename;
NSInputStream* inputStream;
NSOutputStream* outputStream;
NSArray* sailsArray;
}
- (IBAction)submitText:(id)sender;
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
oLabel.hidden = YES;
//handshake
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://localhost:1337/"]
cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData
timeoutInterval:10];
[request setHTTPMethod: @"GET"];
NSError *requestError;
NSURLResponse *urlResponse = nil;
NSData *response1 = [NSURLConnection sendSynchronousRequest:request returningResponse:&urlResponse error:&requestError];
//converts response to string (index.html)
NSString* stringFromData = [[NSString alloc] initWithData:response1 encoding:NSUTF8StringEncoding];
NSLog(@"data converted to string ==> string = %@", stringFromData);
//init socket.io
socketIO = [[SocketIO alloc] initWithDelegate:self];
[socketIO connectToHost:@"localhost" onPort:1337];
}
#pragma mark-table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (sailsArray.count == 0) {
return 1;
}
double delayInSeconds = 2.0;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
oLabel.hidden = YES;
});
return sailsArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"id"];
if (cell == nil) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"id"];
}
if (sailsArray.count == 0) {
cell.textLabel.text = @"array does not exist (yet)";
return cell;
}
//declare string, assign to value at indexPath from array
//array may be made from [dictionary allKeys];
NSString* string = [[sailsArray objectAtIndex:indexPath.row] valueForKey:@"text"];
NSString* subString = [[sailsArray objectAtIndex:indexPath.row] valueForKey:@"createdAt"];
//set string to textLabel of cell
[cell.textLabel setText:string];
[cell.detailTextLabel setText:subString];
return cell;
}
#pragma mark -textField delegate
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
NSUInteger newLength = [textField.text length] + [string length] - range.length;
if (newLength>30) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Your Text" message:@"is too lengthy" delegate:self cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
[alert show];
return NO;
}
return YES;
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return YES;
}
#pragma mark - NSURLConnectionDelegate
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
mutableData = [NSMutableData data];
//suggestedFilename = response.suggestedFilename;
}
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
[mutableData appendData:data];
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection {
oTextView.text = [[NSString alloc] initWithData:mutableData encoding:NSUTF8StringEncoding];
NSMutableURLRequest* request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"http://localhost:1337/messages"] cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:10];
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
sailsArray = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
[oTableView reloadData];
}];
}
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
NSLog(@"Error");
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:error.localizedDescription delegate:self cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
[alert show];
}
#pragma mark -action methods
- (IBAction)submitText:(id)sender
{
[oTextField resignFirstResponder];
oLabel.hidden = NO;
NSMutableURLRequest *request = [NSMutableURLRequest
requestWithURL:[NSURL URLWithString:@"http://localhost:1337/messages"]];
NSString* inputString = oTextField.text;
NSString *params = [[NSString alloc] initWithFormat:@"text=%@", inputString];
oTextField.text = nil;
[request setHTTPMethod:@"POST"];
[request setHTTPBody:[params dataUsingEncoding:NSUTF8StringEncoding]];
[[NSURLConnection alloc] initWithRequest:request delegate:self];
}
@end