/
IPhoneWSConsumerViewController.m
259 lines (212 loc) · 7.16 KB
/
IPhoneWSConsumerViewController.m
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
// Created by Matthew McCullough of Ambient Ideas, LLC on 11/9/08.
// Free for any use for any purpose. No license restrictions.
#import "IPhoneWSConsumerViewController.h"
@implementation IPhoneWSConsumerViewController
@synthesize txtContestantName,lblStatus,pckContestants;
@synthesize pickerData;
- (IBAction) addContestant:(id) sender {
NSLog (@"addContestant");
[txtContestantName resignFirstResponder];
NSLog (@"startAnimating");
[activityIndicator startAnimating];
NSString* statusText;
if([txtContestantName.text length] == 0) {
statusText = @"No status";
}
else {
statusText = [[NSString alloc] initWithFormat:@"Contestant added: %@!",txtContestantName.text];
[self.pickerData addObject:txtContestantName.text];
[self.pckContestants reloadComponent:0];
}
lblStatus.text = statusText;
//Web Service call
NSLog (@"webService AddName");
NSLog(statusText);
[self initiateRESTAddName:txtContestantName.text];
[statusText release];
}
- (IBAction) pickWinner:(id) sender {
NSLog (@"pickWinner");
NSString* statusText;
NSString* winnerName = @"DEFAULT";
//Call WS
winnerName = [self initiateRESTPickWinner];
statusText = [@"Winner is: " stringByAppendingString: winnerName];
lblStatus.text = statusText;
//Find which row in the data array this name is
int rowForWinningContestant = 0;
NSString *currentString = nil;
for (rowForWinningContestant = 0; rowForWinningContestant < [pickerData count]; rowForWinningContestant++) {
currentString = [pickerData objectAtIndex:rowForWinningContestant];
if ([currentString isEqualTo:winnerName]) {
[pckContestants selectRow:rowForWinningContestant inComponent:0 animated:YES];
}
}
[winnerName release];
}
/**
* Release the keyboard display (hide it)
*/
- (BOOL)textFieldShouldReturn:(UITextField *)theTextField
{
NSLog (@"textFieldShouldReturn");
[theTextField resignFirstResponder];
return YES;
}
/**
* Load the initial empty array for contestants in the pickerView
*/
- (void)viewDidLoad
{
NSLog (@"viewDidLoad");
//NSMutableArray *array = [[NSMutableArray alloc] initWithObjects: @"Luke", @"Leia", @"Han", nil];
NSMutableArray *array = [[NSMutableArray alloc] initWithObjects: nil];
self.pickerData = array;
[array release];
[activityIndicator stopAnimating];
}
/**
* How many columns. Just 1 at this time.
*/
- (NSInteger)numberOfComponentsInPickerView: (UIPickerView *)pickerView
{
return 1;
}
/**
* How many rows in the pickerview? As many as are in the data backing store.
*/
- (NSInteger)pickerView: (UIPickerView *)pickerView
numberOfRowsInComponent: (NSInteger)component
{
return [pickerData count];
}
/**
* Get the title for a row in the pickerview
*/
- (NSString *)pickerView:(UIPickerView *)pickerView
titleForRow: (NSInteger)row
forComponent: (NSInteger)component
{
return [pickerData objectAtIndex: row];
}
//WEB SERVICE
//////////////////////////////////////////
/**
* Add a name to the contestant list via a web service call.
* Appears to leak memory on the sendSynchronousRequest call.
* Apple acknowledges. Hasn't fixed it since October 2008 at a minimum on the 2.2 firmware/SDK
* http://lists.apple.com/archives/Macnetworkprog/2008/Nov/msg00013.html
* and
* http://discussions.apple.com/thread.jspa?messageID=8200590
*
*/
- (void)initiateRESTAddName:(NSString*) contestantName
{
[activityIndicator startAnimating];
NSString *baseURLString = @"http://Opus.local:9090/drawing/";
NSString *urlString = [[NSString alloc] initWithFormat:@"%@%@", baseURLString, contestantName];
NSURL *url = [[NSURL alloc] initWithString:urlString];
NSMutableURLRequest *req = [[NSMutableURLRequest alloc] initWithURL:url];
[req setHTTPMethod:@"PUT"];
//Asynchronous
NSURLConnection *connection = [[NSURLConnection alloc]
initWithRequest:req
delegate:self];
[urlString release];
[url release];
[req release];
[connection release];
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
NSLog (@"connectionDidReceiveResponse");
NSLog(@"Response Code: %d", [response statusCode]);
NSLog(@"Content-Type: %@", [[response allHeaderFields] objectForKey:@"Content-Type"]);
//Good response codes are 200 through 299
if ([response statusCode] >= 200 && [response statusCode] < 300 && [response statusCode] != 204) {
//Call was successful
NSLog(@"Web service call deemed successful based on status code.");
}
else {
//Bad response codes are 204 (null payload) and 400 series
UIAlertView *errorAlert = [[UIAlertView alloc]
initWithTitle: @"Error"
message: [[NSString alloc] initWithFormat:@"Error in Web Service call. Response code %d", [response statusCode]]
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[errorAlert show];
[errorAlert release];
}
}
- (void)connection:(NSURLConnection *)connection
didReceiveData:(NSData *)data {
NSLog (@"connectionDidReceiveData");
NSString *newText = [[NSString alloc]
initWithData:data
encoding:NSUTF8StringEncoding];
if (newText != NULL) {
NSLog(newText);
//[self appendTextToView:newText];
[newText release];
}
}
- (void) connectionDidFinishLoading: (NSURLConnection*) connection {
NSLog (@"connectionDidFinishLoading");
[activityIndicator stopAnimating];
}
-(void) connection:(NSURLConnection *)connection
didFailWithError: (NSError *)error {
UIAlertView *errorAlert = [[UIAlertView alloc]
initWithTitle: [error localizedDescription]
message: [error localizedFailureReason]
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[errorAlert show];
[errorAlert release];
[activityIndicator stopAnimating];
NSLog (@"Connection Failed with Error");
}
/**
* Pick a winner from the contestant list via a web service call and return their name.
*/
- (NSString*)initiateRESTPickWinner
{
NSString *baseURLString = @"http://Opus.local:9090/drawing/";
NSURL *url = [[NSURL alloc] initWithString:baseURLString];
NSLog(@"Pick Winner URL: %d", *url);
NSMutableURLRequest *req = [[NSMutableURLRequest alloc] initWithURL:url];
[req setHTTPMethod:@"GET"];
NSLog(@"Pick Winner request: %d", *req);
BOOL success = false;
int retryCount = 0;
NSString *result = nil;
// Retry the web service call up to three times
while (success == false && retryCount <3) {
NSHTTPURLResponse* response = nil;
NSError* error = nil;
NSData *responseData = [NSURLConnection sendSynchronousRequest:req
returningResponse:&response
error:&error];
result = [[NSString alloc] initWithData:responseData
encoding:NSUTF8StringEncoding];
NSLog(@"Response Code: %d", [response statusCode]);
NSLog(@"Content-Type: %@", [[response allHeaderFields]
objectForKey:@"Content-Type"]);
//Log the usual HTTP 200 through 299 responses
if ([response statusCode] >= 200 && [response statusCode] < 300) {
NSLog(@"Result: %@", result);
success = true;
}
//HTTP 204 is a NULL Payload which we occasionally and inexplicably get. Highly reproducible.
if ([response statusCode] == 204) {
[result release];
success = false;
}
retryCount++;
}
[url release];
[req release];
return result;
}
@end