Encode NSString for XML/HTML

后端 未结 14 1213
误落风尘
误落风尘 2020-11-28 04:25

Is there a way to HTML encode a string (NSString) in Objective-C, something along the lines of Server.HtmlEncode in .NET?

相关标签:
14条回答
  • 2020-11-28 04:52

    I use Google Toolbox for Mac (works on iPhone). In particular, see the additions to NSString in GTMNSString+HTML.h and GTMNSString+XML.h.

    0 讨论(0)
  • 2020-11-28 04:52

    Refer below answer:

    NSString *content = global.strPrivacyPolicy;
    content =  [[[[[content stringByReplacingOccurrencesOfString: @"&" withString: @"&"]
    stringByReplacingOccurrencesOfString:@"""  withString:@"\" "]
    stringByReplacingOccurrencesOfString: @"'"  withString:@"'"]
    stringByReplacingOccurrencesOfString: @">" withString: @">"]
    stringByReplacingOccurrencesOfString:  @"&lt;" withString:@"<"];
    [_webViewPrivacy loadHTMLString:content baseURL:nil];
    
    0 讨论(0)
  • 2020-11-28 04:55

    For URL encoding:

    NSString * encodedString = [originalString
          stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding];
    

    See Apple's NSString documentation for more info.

    For HTML encoding:

    Check out CFXMLCreateStringByEscapingEntities, which is part of the Core Foundation XML library, but should still do the trick.

    0 讨论(0)
  • 2020-11-28 04:56

    This easiest solution is to create a category as below:

    Here’s the category’s header file:

    #import <Foundation/Foundation.h>
    @interface NSString (URLEncoding)
    -(NSString *)urlEncodeUsingEncoding:(NSStringEncoding)encoding;
    @end
    

    And here’s the implementation:

    #import "NSString+URLEncoding.h"
    @implementation NSString (URLEncoding)
    -(NSString *)urlEncodeUsingEncoding:(NSStringEncoding)encoding {
        return (NSString *)CFURLCreateStringByAddingPercentEscapes(NULL,
                   (CFStringRef)self,
                   NULL,
                   (CFStringRef)@"!*'\"();:@&=+$,/?%#[]% ",
                   CFStringConvertNSStringEncodingToEncoding(encoding));
    }
    @end
    

    And now we can simply do this:

    NSString *raw = @"hell & brimstone + earthly/delight";
    NSString *url = [NSString stringWithFormat:@"http://example.com/example?param=%@",
                [raw urlEncodeUsingEncoding:NSUTF8StringEncoding]];
    NSLog(url);
    

    The credits for this answer goes to the website below:-

    http://madebymany.com/blog/url-encoding-an-nsstring-on-ios
    
    0 讨论(0)
  • 2020-11-28 04:56

    I found the only way that uses only built-in functions (not manual parsing) and covers all cases. Requires AppKit/UIKit in addition to Foundation. This is Swift but can easily be Objective-C:

    func encodedForHTML() -> String {
    
        // make a plain attributed string and then use its HTML write functionality
        let attrStr = NSAttributedString(string: self)
    
        // by default, the document outputs a whole HTML element
        // warning: if default apple implementation changes, this may need to be tweaked
        let options: [NSAttributedString.DocumentAttributeKey: Any] = [
                .documentType: NSAttributedString.DocumentType.html,
                .excludedElements: [
                    "html",
                    "head",
                    "meta",
                    "title",
                    "style",
                    "p",
                    "body",
                    "font",
                    "span"
                ]
        ]
    
        // generate data and turn into string
        let data = try! attrStr.data(from: NSRange(location: 0, length: attrStr.length), documentAttributes: options)
        let str = String(data: data, encoding: .utf8)!
    
        // remove <?xml line
        return str.components(separatedBy: .newlines).dropFirst().first!
    }
    
    0 讨论(0)
  • 2020-11-28 05:03

    If you can use NSXMLNode (on OS X) Here is the trick:

    NSString *string = @"test<me>"
    NSXMLNode *textNode = [NSXMLNode textWithStringValue:string];
    NSString *escapedString = [textNode.XMLString];
    
    0 讨论(0)
提交回复
热议问题