-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshizzeeps.m
140 lines (104 loc) · 3.61 KB
/
shizzeeps.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
//
// shizzeeps.m
// shizzeeps
//
// Created by Sue Brown on 11/14/09.
// Copyright 2009 House of Crunchy. All rights reserved.
//
#import "shizzeeps.h"
#import "JSON.h"
#import "place.h"
#define DEBUG 0
#if DEBUG
#import "discovery.h"
#endif
@implementation shizzeeps
@synthesize count;
@synthesize dict;
@synthesize results;
@synthesize places;
@synthesize delegate;
@synthesize callback;
@synthesize errorCallback;
- (void)dealloc {
[super dealloc];
[shizzeepsResponseData release];
[dict release];
[results release];
[places release];
}
/* init
Get the data off the Internets
This callback pattern came from a tutorial here:
http://brandontreb.com/objective-c-programming-tutorial-creating-a-twitter-client-part-1/
-------------------------------------------------------------------------- */
- (void) init:(id)requestDelegate requestSelector:(SEL)requestSelector{
// Set the delegate and selector
self.delegate = requestDelegate;
self.callback = requestSelector;
NSString *shizzeepsURL = @"http://shizzeeps.com/bin/ajax.php?f=shizzeepsstatic&city=pdx";
shizzeepsResponseData = [[NSMutableData alloc] init];
NSURL *url = [[NSURL alloc] initWithString: shizzeepsURL];
NSURLRequest *request = [[NSURLRequest alloc] initWithURL: url];
NSURLConnection *connection = [[NSURLConnection alloc]
initWithRequest:request delegate:self];
[connection release];
[request release];
[url release];
}
#pragma mark -
#pragma mark Connection Callbacks
/* connection callbacks
-------------------------------------------------------------------------- */
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
[shizzeepsResponseData setLength:0];
//NSLog(@"connection did receive response");
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
//NSLog(@"connection appending data: %@", [data description]);
[shizzeepsResponseData appendData:data];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
NSLog(@"connection did finish loading");
NSString *responseString = [[NSString alloc] initWithData:shizzeepsResponseData encoding:NSUTF8StringEncoding];
// the JSON library adds a category to NSString, which gives us new methods, like JSONValue.
self.dict = [[responseString JSONValue] retain];
self.results = [[self.dict objectForKey:@"results"] retain];
self.count = [[[self.results valueForKey:@"count"] description] intValue];
// build up an array of place objects and add them to our instance variable, places (an array)
self.places = [[NSMutableArray alloc] initWithCapacity:self.count-1];
for (int i=0; i<self.count; i++) {
NSDictionary *curplace = [[self.results valueForKey:@"places"] objectAtIndex:i];
place *oPlace = [[place alloc] init];
oPlace.placeDict = curplace;
[oPlace initPlace];
[self.places insertObject:oPlace atIndex:i];
[place release];
}
#if DEBUG
discovery *disc = [[discovery alloc] init];
disc.theArray = self.results;
disc.discover;
[disc release];
exit(1);
#endif
if(delegate && callback) {
if([delegate respondsToSelector:self.callback]) {
[delegate performSelector:self.callback];
} else {
NSLog(@"No response from delegate");
}
}
// memory cleanup
[responseString release];
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
// inform the user
NSLog(@"Connection failed! Error - %@ %@",
[error localizedDescription],
[[error userInfo] objectForKey:NSErrorFailingURLStringKey]);
if(errorCallback) {
[delegate performSelector:errorCallback withObject:error];
}
}
@end