有时需求会是从后端获取url然后拼接字符再请求,这里会需要对未知NSString进行去除空格换行等操作,否则NSUrl会是nil!
NSString *url = @"www.baidu.co m";//后端获取得到(里面带有空格换行等)
NSString *name = @"小明";
//中文转编码
NSString *nameCode = [name stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
//拼接
NSString *urlStr = [NSString stringWithFormat:@"%@&name=%@",url,nameCode];
//去除空格和换行
urlStr = [urlStr stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
urlStr = [urlStr stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
urlStr = [urlStr stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
urlStr = [urlStr stringByReplacingOccurrencesOfString:@"\r" withString:@""];
urlStr = [urlStr stringByReplacingOccurrencesOfString:@"\n" withString:@""];
NSURL *urlU = [NSURL URLWithString:urlStr];
NSURLRequest *request = [NSURLRequest requestWithURL:urlU];
[self.currentWebView loadRequest:request];
网友评论