Skip to content

Instantly share code, notes, and snippets.

@alloyking
Created December 28, 2013 14:20
Show Gist options
  • Save alloyking/8159982 to your computer and use it in GitHub Desktop.
Save alloyking/8159982 to your computer and use it in GitHub Desktop.
ios post request (likely crappy)
//
// UrlRequest.h
// form
//
// Created by timshultis on 12/26/13.
// Copyright (c) 2013 timshultis. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface UrlRequest : NSObject
-(void) post;
-(void) setBody: (NSString *) b;
-(void) setUrl: (NSString *) u;
-(NSString *) url;
-(NSString *) body;
@end
//
// UrlRequest.m
// form
//
// Created by timshultis on 12/26/13.
// Copyright (c) 2013 timshultis. All rights reserved.
//
#import "UrlRequest.h"
@implementation UrlRequest{
NSString * url;
NSString * body;
NSMutableData * receivedData;
}
-(void)post{
NSMutableURLRequest *postRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:self->url]];
[postRequest setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[postRequest setHTTPMethod:@"POST"];
[postRequest setHTTPBody:[NSData dataWithBytes:[self->body UTF8String] length:strlen([self->body UTF8String])]];
receivedData = [NSMutableData dataWithCapacity: 0];
NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:postRequest delegate:self];
if (!theConnection) {
// Release the receivedData object.
receivedData = nil;
NSLog(@"oh this failed");
// Inform the user that the connection failed.
} else{
NSLog(@"Connection Success");
}
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
// This method is called when the server has determined that it
// has enough information to create the NSURLResponse object.
// It can be called multiple times, for example in the case of a
// redirect, so each time we reset the data.
// receivedData is an instance variable declared elsewhere.
[receivedData setLength:0];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
// Append the new data to receivedData.
// receivedData is an instance variable declared elsewhere.
[receivedData appendData:data];
}
- (void)connection:(NSURLConnection *)connection
didFailWithError:(NSError *)error
{
// Release the connection and the data object
// by setting the properties (declared elsewhere)
// to nil. Note that a real-world app usually
// requires the delegate to manage more than one
// connection at a time, so these lines would
// typically be replaced by code to iterate through
// whatever data structures you are using.
//theConnection = nil;
//receivedData = nil;
// inform the user
NSLog(@"Connection failed! Error - %@ %@",
[error localizedDescription],
[[error userInfo] objectForKey:NSURLErrorFailingURLStringErrorKey]);
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
// do something with the data
// receivedData is declared as a property elsewhere
NSLog(@"Succeeded! Received %d bytes of data",[receivedData length]);
// Release the connection and the data object
// by setting the properties (declared elsewhere)
// to nil. Note that a real-world app usually
// requires the delegate to manage more than one
// connection at a time, so these lines would
// typically be replaced by code to iterate through
// whatever data structures you are using.
//theConnection = nil;
//receivedData = nil;
}
-(void)setBody:(NSString *)b{
body = b;
}
-(void)setUrl:(NSString *)u{
url = u;
}
-(NSString *) url{
return self.url;
}
-(NSString *) body{
return self.body;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment