Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSURLRequest creation using query string in ios

I have created one user enquiry form in ios. I'm using php as a server side. I constructed query string like bellow in IOS

http://www.mydomain.in/androidmail_enquiry.php?name=Vinoth Kumar&phone=04259280244&[email protected]&address=Coimbatore&comments=Sample Enquiry&mobile=xxxxxxxx

Code:

-(void)sendEnquiryDetails
{
    cmttextview.text = @"Sample Enquiry";
    NSString *siteurl = @"http://www.mydomain.in/androidmail_enquiry.php?";
    NSString *name = txtName.text;
    NSString *phone = txtPhone.text;
    NSString *email = txtEmail.text;
    NSString *address = txtAddress.text;
    NSString *comments = cmttextview.text;
    NSString *mobile = txtMobile.text;
    NSString *enquiryurl = [NSString stringWithFormat:@"%@name=%@&phone=%@&email=%@&address=%@&comments=%@&mobile=%@",siteurl,name,phone,email,address,comments,mobile];

    NSLog(enquiryurl);

   NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:enquiryurl]];
    NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request  delegate:self];
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    //NSLog(@"didReceiveResponse");
    [self.responseData setLength:0];
}

-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    [self.responseData appendData:data];
}

-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
    NSLog(@"%@",[NSString stringWithFormat :@"didfailwitherror: %@", [error description]]);
}

-(void)connectionDidFinishLoading: (NSURLConnection *)connection{
    NSLog(@"Success Code:%@",self.responseData);
}

But when i submit this by using NSURLRequest getting error like invalid url(URL Forming error) in didFailWithError method.

like image 284
Vinoth Kumar Avatar asked Oct 09 '13 04:10

Vinoth Kumar


1 Answers

Make sure You have added stringByAddingPercentEscapesUsingEncoding , because in your url there is space in name , so in browser it is replaced with %20 you have to add programmatically in code,

Try this

-(void)sendEnquiryDetails
{
    cmttextview.text = @"Sample Enquiry";
    NSString *siteurl = @"http://www.mydomain.in/androidmail_enquiry.php?";
    NSString *name = txtName.text;
    NSString *phone = txtPhone.text;
    NSString *email = txtEmail.text;
    NSString *address = txtAddress.text;
    NSString *comments = cmttextview.text;
    NSString *mobile = txtMobile.text;
    NSString *enquiryurl = [NSString stringWithFormat:@"%@name=%@&phone=%@&email=%@&address=%@&comments=%@&mobile=%@",siteurl,name,phone,email,address,comments,mobile];

    NSLog(enquiryurl);

 NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:[enquiryurl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]];
    NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request  delegate:self];
}
like image 141
Toseef Khilji Avatar answered Oct 18 '22 10:10

Toseef Khilji