Search code examples
iosnsstringnsurl

convert string into url to send data to mysql objective c iOS


I am trying to convert a string into url but not getting success. any better way to do, or improve the below code. thanks

NSString *strURL = @"http://domain.com/This Is My, Code for This. app";
NSString *escapedUrlString = [strURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSLog(escapedUrlString);

It is showing this log

http://domain.com/This2s2y,芙ode
0.000000or2his.20x1.55e800047f4p-1029pp

Here is my actual code sending and save data to mysql

NSString *data = @"This Is My, Code for This. app";
NSString *strURL = [NSString stringWithFormat:@"http://domain.com/connection.php?data=%@", data];   

NSData *dataURL = [NSData dataWithContentsOfURL:[NSURL URLWithString:strURL]];


NSString *strResult = [[NSString alloc] initWithData:dataURL encoding:NSUTF8StringEncoding];  

NSURL *urll = [NSURL URLWithString:strResult];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:urll];

[request setHTTPMethod:@"GET"];


NSURLResponse *response;
NSError *err;
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&err];
NSLog(@"responseData: %@", responseData);

No success with this, but when I use

data = @"ThisIsMyCode";

Then everything works fine... Help


Solution

  • NSString *data = @"This Is My, Code for This. app";
    NSString *escapedDataString = [data stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    
    NSString *strURL = [NSString stringWithFormat:@"http://domain.com/connection.php?data=%@", escapedDataString];
        NSLog(@"Encoded:%@",strURL);
    

    This works fine, this is the log:

    Encoded:http://domain.com/connection.php?data=This%20Is%20My,%20Code%20for%20This.%20app