i this post there is answer for base 64 encode a data
for NSData from string
NSData* data = [str dataUsingEncoding:NSUTF8StringEncoding];
and NSString from data
NSString* str =[NSString stringWithUTF8String:[data bytes]];
this snippet directly from my code does post connection easily
-(BOOL) doPostConnection:(ServiceProps*) props delegate:(id<URLConnectionDelegate>) delegate
{
NSString *post = props.contentString;
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:NO];
NSString *postLength = [NSString stringWithFormat:@"%d",[postData length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:props.connectionURL];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Data-Type"];
[request setHTTPBody:postData];
NSURLConnection *conn = [[NSURLConnection alloc]initWithRequest:request delegate:delegate];
return conn != nil;
}
props -> basic class for wrap content string and url
@interface ServiceProps : NSObject
@property () NSString* contentString;
@property () NSURL* connectionURL;
@end
delegate -> An interface class which contains your post service logic it inside depends on your service
@protocol URLConnectionDelegate <NSObject>
- (id) initWithConnectionDelegate:(id<ConnectionDelegate>)delegate;
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData*)data;
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error;
- (void)connectionDidFinishLoading:(NSURLConnection *)connection;
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response;
@end