Skip to content

Instantly share code, notes, and snippets.

View ejknapp's full-sized avatar

Eric Knapp ejknapp

View GitHub Profile
@ejknapp
ejknapp / AsyncStart.m
Created November 24, 2009 00:48
Start async call to Flak server
- (void)getNextMessages:(NSNumber *)maxMessageId {
if (!self.flakManager.isLoggedIn) {
[self createSession];
}
// NSLog(@"In getNextMessages: %@", maxMessageId);
NSString *urlString = [NSString
stringWithFormat:@"%@/messages.json?kind=message&after_id=%@",
#pragma mark NSURLConnection Delagate Callback Methods
- (void)connection:(NSURLConnection *)connection
didReceiveResponse:(NSURLResponse *)response {
NSLog(@"In didReceiveResponse");
self.responseString = @"";
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
// Deserialize json and add to Core Data
- (void)processNewMessagesData:(NSString *)jsonMessages {
NSLog(@"In processNewMessagesData");
NSData *messagesData = [jsonMessages dataUsingEncoding:NSUTF8StringEncoding];
NSError *error = nil;
NSArray *messageArray = [[CJSONDeserializer deserializer]
deserialize:messagesData error:&error];
// How to poll
// Property
NSTimer *keepLoggedInTimer;
// Methods
- (void)keepLoginAlive:(NSTimer *)timer;
- (void)startTimerForKeepAlive;
/Users/user/Library/Application\ Support/iPhone\ Simulator/User/Applications
//Add a message to Core Data
- (void)addMessage:(NSDictionary *)newMessageDictionary {
NSDictionary *messageDictionary = [newMessageDictionary objectForKey:@"message"];
// NSLog(@"In addMessage");
// NSLog(@"new message dictionary:\n%@", newMessageDictionary);
NSEntityDescription *entity =
[self.messageFetchedResultsController.fetchRequest entity];
//Application Data Initialization
- (void)awakeFromNib {
// NSLog(@"waking up!");
[self managedObjectContext];
self.hostURL = @"http://flak.heroku.com";
NSHTTPCookieStorage *cookieStorage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
[cookieStorage setCookieAcceptPolicy:NSHTTPCookieAcceptPolicyAlways];
// Escape double quotes in new messages
-(NSString *)escapeString:(NSString *)string {
NSString *cleanString = [string stringByReplacingOccurrencesOfString:@"\""
withString:@"\\\""];
return cleanString;
}
//NSUserDefaults usage for user and app settings
- (void)createUserDefaultsFromPlist {
NSString *settingsPlistPath = [[NSBundle mainBundle]
pathForResource:@"FlakSettings" ofType:@"plist"];
NSDictionary *dictionaryFromDefaultPlist;
if (settingsPlistPath) {
dictionaryFromDefaultPlist = [NSMutableDictionary
//POSTing a message to the server and checking the response.
//POST the message to the server
NSData *responseData = [NSURLConnection sendSynchronousRequest:request
returningResponse:&response error:&error];
//Extract the JSON string from the response
NSString *jsonString = [[NSString alloc] initWithData:responseData
encoding:NSUTF8StringEncoding];