Hello i am new to iOS and need to submit an image to php server. For this i have converted image into base64 format. and need to send this base64 string to PHP server. Code i am using fot this is, Please help i am completely new in ios
// Create your request string with parameter name as defined in PHP file
NSString *myRequestString = [NSString stringWithFormat:@"comment=%@",@"test data"];
// Create Data from request
NSData *myRequestData = [NSData dataWithBytes: [myRequestString UTF8String] length: [myRequestString length]];
request = [[NSMutableURLRequest alloc] initWithURL: [NSURL URLWithString: @"http://www.sdevices.ru/flashrecorder/speechtotext.php"]];
// set Request Type
[request setHTTPMethod:@"POST"];
// Set content-type
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"content-type"];
// Set Request Body
[request setHTTPBody:myRequestData];
// Now send a request and get Response
NSData *returnData = [NSURLConnection sendSynchronousRequest: request returningResponse: nil error: nil];
// Log Response
NSString *response = [[NSString alloc] initWithBytes:[returnData bytes] length:[returnData length] encoding:NSUTF8StringEncoding];
NSLog(@"%@",response); // here you get reasponse string
if ([response isEqualToString:@"Speech text!"]) {
UIAlertView *alrt = [[UIAlertView alloc] initWithTitle:@"Flash Recorder" message:@"Text is submitted!" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil];
[alrt show];
}
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