ios unlabe 显示html,iphone - Objective C HTML escape/unescape - Stack Overflow

This is an old answer that I posted some years ago. My intention was

not to provide a "good" and "respectable" solution, but a "hacky" one

that might be useful under some circunstances. Please, don't use this solution unless nothing else works.

Actually, it works perfectly fine in many situations that other

answers don't because the UIWebView is doing all the work. And you can

even inject some javascript (which can be dangerous and/or useful). The performance should be horrible, but actually is not that bad.

There is another solution that has to be mentioned. Just create a UIWebView, load the encoded string and get the text back. It escapes tags "<>", and also decodes all html entities (e.g. ">") and it might work where other's don't (e.g. using cyrillics). I don't think it's the best solution, but it can be useful if the above solutions doesn't work.

Here is a small example using ARC:

@interface YourClass()

@property UIWebView *webView;

@end

@implementation YourClass

- (void)someMethodWhereYouGetTheHtmlString:(NSString *)htmlString {

self.webView = [[UIWebView alloc] init];

NSString *htmlString = [NSString stringWithFormat:@"

%@", self.description];

[self.webView loadHTMLString:htmlString baseURL:nil];

self.webView.delegate = self;

}

- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error {

self.webView = nil;

}

- (void)webViewDidFinishLoad:(UIWebView *)webView {

self.webView = nil;

NSString *escapedString = [self.webView stringByEvaluatingJavaScriptFromString:@"document.body.textContent;"];

}

- (void)webViewDidStartLoad:(UIWebView *)webView {

// Do Nothing

}

@end

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值