Skip to content

Commit

Permalink
parsing returned NSData* object from connection in the appropriate NS…
Browse files Browse the repository at this point in the history
…URLConnection delegate method.
  • Loading branch information
Alex Olson committed Jan 6, 2011
1 parent f21ab25 commit 0df5232
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 20 deletions.
43 changes: 33 additions & 10 deletions Examples/CocoaApp/Kynetx.m
Expand Up @@ -29,39 +29,59 @@ - (id) initWithAppID:(id) input eventDomain:(id) domain {
}

- (void) signal:(NSString *) name params:(NSDictionary*) params {
// build NSURL object
// start with a NSString base url
// raise events to kns

// build the request URL
// start with a NSString base URL
NSString* baseURLstring = [NSString stringWithFormat:@"https://cs.kobj.net/blue/event/%@/%@/%@/", [self eventDomain], name, [self appID]];
// then construct NSURL with the dict of params and baseURLstring
NSURL* eventURL = [self URLFromDict:params withBaseURL:baseURLstring];


// construct a request object with eventURL
NSURLRequest* KNSRequest = [[[NSURLRequest alloc] initWithURL:eventURL] autorelease];
// then use that request to make a connection, delegate methods here
NSMutableURLRequest* KNSRequest = [[[NSURLRequest alloc] initWithURL:eventURL] autorelease];

// grab KNS cookies
NSArray* KNSCookies = [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookiesForURL:eventURL];
// get a dictionary of HTTP headers using the cookies
NSDictionary* headers = [NSHTTPCookie requestHeaderFieldsWithCookies:KNSCookies];
// set request headers from the dictionary of headers
[KNSRequest setAllHTTPHeaderFields:headers];
// then use that request to make a connection
// specifying that the current object should act as its delegate
[[NSURLConnection alloc] initWithRequest:KNSRequest delegate:self];
}

- (void) connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
// handle cookies

// cast base response to derived http url response class
// so we can access headers
NSHTTPURLResponse* KNSHTTPResponse = (NSHTTPURLResponse*) response;
// retrieve the cookies from the KNS response Set-Cookie header
// KNS just sends one Set-Cookie header, but this method will handle any
// number of returned Set-Cookie headers
NSArray* KNSCookies = [[NSHTTPCookie cookiesWithResponseHeaderFields:[KNSHTTPResponse allHeaderFields]
forURL:[KNSHTTPResponse URL]];
NSArray* KNSCookies = [NSHTTPCookie cookiesWithResponseHeaderFields:[KNSHTTPResponse allHeaderFields]
forURL:[KNSHTTPResponse URL]];
// add the KNSCookies to the shared cookie storage of the device
[[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookies:KNSCookies
forURL:[KNSHTTPResponse URL] mainDocumentURL:nil];
forURL:[KNSHTTPResponse URL] mainDocumentURL:nil];

}

- (void) connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
// handle JSON returned from KNS
// take returned data and parse it
// Then call KynetxDelegate method, passing it the
// dictionary of directives
// This is where we exit the current Kynetx
// object if everything goes well

NSArray* KNSDirectives = [self parseDirectives:data];
}

- (NSArray*) parseDirectives:(NSData*) response {
// parse json string of directives returned from KNS

// create an instance of the json parser
SBJsonParser* parser = [[[SBJsonParser alloc] init] autorelease];

Expand Down Expand Up @@ -98,6 +118,8 @@ - (NSArray*) parseDirectives:(NSData*) response {
}

- (NSURL*) URLFromDict:(NSDictionary*) params withBaseURL:(NSString*) URLstring {
// construct a NSURL from a dictionary of paramaters and a base URL string

// setup mutable string
NSMutableString* buildString = [[[NSMutableString alloc] init] autorelease];

Expand All @@ -111,7 +133,8 @@ - (NSURL*) URLFromDict:(NSDictionary*) params withBaseURL:(NSString*) URLstring
[buildString appendString:URLstring];
}

// loop over params dictionary, appending each key-value pair to url string
// loop over the params dictionary
// appending each key-value pair as we go
NSArray* keys = [params allKeys];
int count = [keys count];
for (int i = 0; i < count; i++) {
Expand All @@ -124,7 +147,7 @@ - (NSURL*) URLFromDict:(NSDictionary*) params withBaseURL:(NSString*) URLstring
}
}

// url is now complete
// at this point, URL is now constructed and ready to be returned

return [[[NSURL alloc] initWithString:buildString] autorelease];
}
Expand Down
43 changes: 33 additions & 10 deletions kynetx.m
Expand Up @@ -29,39 +29,59 @@ - (id) initWithAppID:(id) input eventDomain:(id) domain {
}

- (void) signal:(NSString *) name params:(NSDictionary*) params {
// build NSURL object
// start with a NSString base url
// raise events to kns

// build the request URL
// start with a NSString base URL
NSString* baseURLstring = [NSString stringWithFormat:@"https://cs.kobj.net/blue/event/%@/%@/%@/", [self eventDomain], name, [self appID]];
// then construct NSURL with the dict of params and baseURLstring
NSURL* eventURL = [self URLFromDict:params withBaseURL:baseURLstring];


// construct a request object with eventURL
NSURLRequest* KNSRequest = [[[NSURLRequest alloc] initWithURL:eventURL] autorelease];
// then use that request to make a connection, delegate methods here
NSMutableURLRequest* KNSRequest = [[[NSURLRequest alloc] initWithURL:eventURL] autorelease];

// grab KNS cookies
NSArray* KNSCookies = [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookiesForURL:eventURL];
// get a dictionary of HTTP headers using the cookies
NSDictionary* headers = [NSHTTPCookie requestHeaderFieldsWithCookies:KNSCookies];
// set request headers from the dictionary of headers
[KNSRequest setAllHTTPHeaderFields:headers];
// then use that request to make a connection
// specifying that the current object should act as its delegate
[[NSURLConnection alloc] initWithRequest:KNSRequest delegate:self];
}

- (void) connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
// handle cookies

// cast base response to derived http url response class
// so we can access headers
NSHTTPURLResponse* KNSHTTPResponse = (NSHTTPURLResponse*) response;
// retrieve the cookies from the KNS response Set-Cookie header
// KNS just sends one Set-Cookie header, but this method will handle any
// number of returned Set-Cookie headers
NSArray* KNSCookies = [[NSHTTPCookie cookiesWithResponseHeaderFields:[KNSHTTPResponse allHeaderFields]
forURL:[KNSHTTPResponse URL]];
NSArray* KNSCookies = [NSHTTPCookie cookiesWithResponseHeaderFields:[KNSHTTPResponse allHeaderFields]
forURL:[KNSHTTPResponse URL]];
// add the KNSCookies to the shared cookie storage of the device
[[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookies:KNSCookies
forURL:[KNSHTTPResponse URL] mainDocumentURL:nil];
forURL:[KNSHTTPResponse URL] mainDocumentURL:nil];

}

- (void) connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
// handle JSON returned from KNS
// take returned data and parse it
// Then call KynetxDelegate method, passing it the
// dictionary of directives
// This is where we exit the current Kynetx
// object if everything goes well

NSArray* KNSDirectives = [self parseDirectives:data];
}

- (NSArray*) parseDirectives:(NSData*) response {
// parse json string of directives returned from KNS

// create an instance of the json parser
SBJsonParser* parser = [[[SBJsonParser alloc] init] autorelease];

Expand Down Expand Up @@ -98,6 +118,8 @@ - (NSArray*) parseDirectives:(NSData*) response {
}

- (NSURL*) URLFromDict:(NSDictionary*) params withBaseURL:(NSString*) URLstring {
// construct a NSURL from a dictionary of paramaters and a base URL string

// setup mutable string
NSMutableString* buildString = [[[NSMutableString alloc] init] autorelease];

Expand All @@ -111,7 +133,8 @@ - (NSURL*) URLFromDict:(NSDictionary*) params withBaseURL:(NSString*) URLstring
[buildString appendString:URLstring];
}

// loop over params dictionary, appending each key-value pair to url string
// loop over the params dictionary
// appending each key-value pair as we go
NSArray* keys = [params allKeys];
int count = [keys count];
for (int i = 0; i < count; i++) {
Expand All @@ -124,7 +147,7 @@ - (NSURL*) URLFromDict:(NSDictionary*) params withBaseURL:(NSString*) URLstring
}
}

// url is now complete
// at this point, URL is now constructed and ready to be returned

return [[[NSURL alloc] initWithString:buildString] autorelease];
}
Expand Down

0 comments on commit 0df5232

Please sign in to comment.