iphone - 将文本/数据以及特殊字符作为参数添加到 iphone 中的 api(url)
<p><p>我是 Obj-c 的新手。我正在向 url 添加像文本这样的参数(文本也可能有特殊字符)。但是 url 显示为零,它没有从字符串中获取值。</p>
<p>例如:</p>
<pre><code>NSString*strUrl=;
NSString *strMainUrl=;
NSString *encodeStr = ;
NSURL *url=;
NSLog(@" url is =%@",url);
</code></pre>
<p>但 url 显示 nil 值。它没有采用“encodeStr”值。我该如何解决这个问题。请帮助我。</p>
<p>我试过了..</p>
<pre><code>NSURLRequest *urlRequest = cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:30.0];
</code></pre>
<p>还有</p>
<p><code>strEncode=;</code></p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p><a href="http://madebymany.com/blog/url-encoding-an-nsstring-on-ios" rel="noreferrer noopener nofollow">here</a> 中的修改示例:</p>
<pre><code>#import <Foundation/Foundation.h>
// In case you're unfamiliar, this is a category, which allows us to add methods
// to an existing class, even if we didn't create it. It's a nice alternative
// to subclassing.
//
// In this case, we're extending NSString
@interface NSString (URLEncoding)
-(NSString *)urlEncodeUsingEncoding:(NSStringEncoding)encoding;
@end
@implementation NSString (URLEncoding)
-(NSString *)urlEncodeUsingEncoding:(NSStringEncoding)encoding {
return (NSString *)CFURLCreateStringByAddingPercentEscapes(NULL,
(CFStringRef)self,
NULL,
(CFStringRef)@"!*'\"();:@&=+$,/?%#[]% ",
CFStringConvertNSStringEncodingToEncoding(encoding));
}
@end
int main(int argc, char *argv[]) {
@autoreleasepool
{
NSString *raw = @"hi how@!#$%^^&*()_=+ r u <>,./ where r u";
// note also, that your string omits the '?' in the URL
NSString *url = [NSString stringWithFormat:@"http://google.com/API/index.php?action=listIt&data=%@",
];
NSURL *finalUrl = ;
NSLog(@"%@", finalUrl);
}
}
</code></pre>
<p>输出:</p>
<pre><code>http://google.com/API/index.php?action=listIt&data=hi%20how%40%21%23%24%25%5E%5E%26%2A%28%29_%3D%2B%20%20%20r%20u%20%3C%3E%2C.%2F%20where%20r%20u
</code></pre></p>
<p style="font-size: 20px;">关于iphone - 将文本/数据以及特殊字符作为参数添加到 iphone 中的 api(url),我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/12219547/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/12219547/
</a>
</p>
页:
[1]