Skip to content

Instantly share code, notes, and snippets.

@pilky
Created November 23, 2011 13:14
Show Gist options
  • Save pilky/1388633 to your computer and use it in GitHub Desktop.
Save pilky/1388633 to your computer and use it in GitHub Desktop.
- (void)submitDetails {
//If the user said not to send, don't
if (![[NSUserDefaults standardUserDefaults] boolForKey:TWSSendSystemProfile])
return;
//Generate our last sent check and see if it is different to the stored one
NSString *lastSent = [[NSDate date] descriptionWithCalendarFormat:@"%Y%m" timeZone:[NSTimeZone timeZoneForSecondsFromGMT:0] locale:nil];
if ([lastSent isEqualToString:[[NSUserDefaults standardUserDefaults] objectForKey:TWSLastSent]])
return;
//Submit the string and the new last sent value
[self _submitJSONString:[TWSJSONGenerator JSONStringWithSystemProfiler:profiler] newLastSentValue:lastSent];
}
- (void)_submitJSONString:(NSString *)aJSONString newLastSentValue:(NSString *)aLastSent {
//Get the profile URL, warning if it is invalid
NSURL *profileURL = [NSURL URLWithString:[[NSBundle mainBundle] objectForInfoDictionaryKey:TWSProfileURL]];
if (!profileURL) {
NSLog(@"Invalid System Profiler URL - Check the TWSProfileURL key in your Info.plist");
return;
}
//Generate our request and send it
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:profileURL];
[request setHTTPBody:[aJSONString dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPMethod:@"POST"];
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *aResponse, NSData *aData, NSError *aError) {
//Only update the last sent if we got a successful response back
if ([(NSHTTPURLResponse *)aResponse statusCode] == 200) {
[[NSUserDefaults standardUserDefaults] setObject:aLastSent forKey:TWSLastSent];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment