我正在尝试将字符串转换为url但没有成功。任何更好的方法,或改进以下代码。谢谢

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

它显示此日志

http://domain.com/This2s2y,福罗德
0.000000或2个20x1.55e800047f4p-1029pp

这是我实际的代码发送并将数据保存到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);

这样做没有成功,但是当我使用
data = @"ThisIsMyCode";

然后一切正常...
救命

最佳答案

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);

正常工作,这是日志:
Encoded:http://domain.com/connection.php?data=This%20Is%20My,%20Code%20for%20This.%20app

关于ios - 将字符串转换为url以将数据发送到mysql objective-c iOS,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/14314508/

10-09 02:41