Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert String To HTML In Swift

Tags:

ios

swift

I am sending a post request which contains an email address for the username and a password. The request works when I hard code in the email address ([email protected]) like this:

test%40test.com

However when I pass it the actual email address, it obviously doesn't work. In swift, how do I convert a string to it's HTML format (or URL format I guess). I found that iOS7 adds NSHTMLTextDocumentType which might be able to do that for me, but I can't find any examples in Swift. Here is one I found in Objective C:

NSURL *htmlString = [[NSBundle mainBundle]
URLForResource: @"helloworld" withExtension:@"html"];
NSAttributedString *stringWithHTMLAttributes = [[NSAttributedString alloc]   initWithFileURL:htmlString options:@{NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType} documentAttributes:nil error:nil];

How would this work in Swift? Or if anyone has a better / easier suggestion that would work with all versions of iOS I would appreciate it. I also don't want to reference third party libraries to make this work.

like image 707
PretzelJesus Avatar asked Jul 12 '26 05:07

PretzelJesus


1 Answers

Thanks Bryan I got the answer from within that thread. Here is what ultimately worked:

var originalString = "[email protected]"
var escapedString = originalString.stringByAddingPercentEncodingWithAllowedCharacters(NSCharacterSet.URLHostAllowedCharacterSet())
println("escapedString: \(escapedString)")

This prints out

test%40test.com
like image 79
PretzelJesus Avatar answered Jul 13 '26 23:07

PretzelJesus



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!