3

我正在尝试将字符串转换为 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.000000or2his.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";

然后一切正常...帮助

4

1 回答 1

8
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
于 2013-01-14T07:49:54.597 回答