Kap*_*isa 9 iphone objective-c nsurl
我有一个我的网址字符串..我想从该网址获取数据.为此我需要创建对象NSURL.问题是当我尝试URLWithStringNSURL类的方法时,它返回nil.但是当我在My Mac的Safari中打开此链接时,它会向我显示图像.
我在用 ..
NSString *str = @"http://chart.apis.google.com/chart?chs=150x150&cht=qr&chld=L|0&chl=http://MyServer.com/shareMyCard.php?value=1304057103";
NSURL *url = [NSURL URLWithString:str];
NSData *data = [NSData dataWithContentsOfURL:url];
UIImage *img = [UIImage imageWithData:data];
Run Code Online (Sandbox Code Playgroud)
我不知道为什么URL是Nil.我想因为我的字符串中的查询字符串.
任何想法如何从这个Url字符串中获取数据.
谢谢
Sau*_*abh 32
您的URL为nil,因为它包含特殊字符,在URL中使用之前使用以下函数对您的url参数进行编码 -
-(NSString *) URLEncodeString:(NSString *) str
{
NSMutableString *tempStr = [NSMutableString stringWithString:str];
[tempStr replaceOccurrencesOfString:@" " withString:@"+" options:NSCaseInsensitiveSearch range:NSMakeRange(0, [tempStr length])];
return [[NSString stringWithFormat:@"%@",tempStr] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
10615 次 |
| 最近记录: |