修复CFURLCreateStringByAddingPercentEscapes不推荐使用的方法

时间:2016-09-26 13:45:53

标签: ios objective-c nsstring ios9 urlencode

在DOWNVOTING或标记DUPLICATE之前,请记住我想要更新的代码是@implement,因此其他Stackoverflow帖子不会有帮助。

我正在调整旧应用程序的代码以将其更新为iOS9和10.我不熟悉ObjectiveC并且有一个我无法清除错误的片段。

据我所知,代码"重载"具有Url编码功能的NSString类。但是,我无法理解。以下是带有一些注释的代码作为问题的描述:

#import "NSString+URLEncoding.h"
@implementation NSString (OAURLEncodingAdditions)

- (NSString *)encodedURLString {
   //this line raises a "requires a bridge cast" error, when I click on "fix it", another yellow "first deprecated in iOS 9.0" warning appears
   NSString *result = (NSString *)CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault,
                                                                           (CFStringRef)self,
                                                                           NULL,                   // characters to leave unescaped (NULL = all escaped sequences are replaced)
                                                                           CFSTR("?=&+"),          // legal URL characters to be escaped (NULL = all legal characters are replaced)
                                                                           kCFStringEncodingUTF8); // encoding*/

   /*  this is the closest solution I've got, but it does not work, because I cannot anticipate the value of result
   NSString *result = @"<url>";
   result = [result stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
   */

    return [result autorelease]; //will remove this autorelease because of ARC
}
@end

0 个答案:

没有答案