UTF8 Encoding in iOS / swift for email containing + - ios

How to encode URL parameters with UTF8 encoding in Query string format URL/param1/param2/param3
I have even tried URLHostAllowedCharacterSet, which encodes special characters but not '+' character.
How to encode email containing + character using almofire request?

Try to add an extension to handle the encoding and then just call the extension when you want to encode.
extension String {
func stringByAddingPercentEncodingForRFC3986() -> String? {
let unreserved = "-._~/?:"
let allowed = NSMutableCharacterSet.alphanumericCharacterSet()
allowed.addCharactersInString(unreserved)
return stringByAddingPercentEncodingWithAllowedCharacters(allowed)
}
}
And then to use it:
let query = "http://test.com/param1& param2+ param3"
let encoded = query.stringByAddingPercentEncodingForRFC3986()!

extension String {
func stringByAddingPercentEncodingForRFC3986() -> String? {
let unreserved = "-._~/?:"
let allowedCharacterSet = NSMutableCharacterSet.alphanumeric()
allowedCharacterSet.addCharacters(in: unreserved)
return self.addingPercentEncoding(withAllowedCharacters: allowedCharacterSet as CharacterSet)
}
}
let encodedVal = usernameTextField.text.stringByAddingPercentEncodingForRFC3986()
--------- OR ----------
extension String {
var urlEncoded: String? {
let allowedCharacterSet = CharacterSet.alphanumerics.union(CharacterSet(charactersIn: "-._~/?:"))
return self.addingPercentEncoding(withAllowedCharacters: allowedCharacterSet)
}
}
let encodedVal = usernameTextField.text.urlEncoded

Related

Substitute for CFURLCreateStringByAddingPercentEscapes in Swift3 iOS10?

I know the method CFURLCreateStringByAddingPercentEscapes is deprecated in iOS10, it suggests me using stringByAddingPercentEncodingWithAllowedCharacters, but I do not using UTF-8, how can I use stringByAddingPercentEncodingWithAllowedCharacters to do the same thing as following:
extension String {
var gbkEncoded: String {
let cfEnc = CFStringEncodings.GB_18030_2000
return CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault, self as CFString!, nil, nil, CFStringEncoding(cfEnc.rawValue)) as String
}
}
I'm afraid Apple would not provide us a replacement for CFURLCreateStringByAddingPercentEscapes as non-UTF-8 is not recommended in the modern web standards.
You may need to do it yourself. An example:
extension CharacterSet {
static let rfc3986Unreserved = CharacterSet(charactersIn: "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-._~")
}
extension String.Encoding {
static let gb_18030_2000 = String.Encoding(rawValue: CFStringConvertEncodingToNSStringEncoding(CFStringEncoding(CFStringEncodings.GB_18030_2000.rawValue)))
}
extension String {
func addingPercentEncoding(withAllowedCharacters characterSet: CharacterSet, using encoding: String.Encoding) -> String {
let stringData = self.data(using: encoding, allowLossyConversion: true) ?? Data()
let percentEscaped = stringData.map {byte->String in
if characterSet.contains(UnicodeScalar(byte)) {
return String(UnicodeScalar(byte))
} else if byte == UInt8(ascii: " ") {
return "+"
} else {
return String(format: "%%%02X", byte)
}
}.joined()
return percentEscaped
}
var gbkEncoded: String {
return self.addingPercentEncoding(withAllowedCharacters: .rfc3986Unreserved, using: .gb_18030_2000)
}
}
print("互联网".gbkEncoded) //->%BB%A5%C1%AA%CD%F8

Encode / Decode string to UTF-8 with Emojis

I'm developping a messaging app between Android an iOS (Swift 2.2).
Users must be able to send and receive Emojis : Hello πŸ˜€
I need to encode emojis to UTF-8 instead of Unicode.
Here my code to encode / decode from Unicode :
static func decodeUnicode(input: String) -> String {
let ns = NSString(string: input)
let data:NSData = ns.dataUsingEncoding(NSUTF8StringEncoding)!
if let decoded = NSString(data:data,encoding:NSUTF8StringEncoding) as? String {
return decoded
}
return ""
}
static func encodeUnicode(input: String) -> String {
let data:NSData = input.dataUsingEncoding(NSNonLossyASCIIStringEncoding)!
if let decoded = NSString(data:data,encoding:NSUTF8StringEncoding) as? String {
return decoded
}
return ""
}
Encoding have to be compatible with Android decoding.

How to delete last path component of a String in Swift?

I have a String 11/Passion/01PassionAwakening.mp3 and I need to delete the last path component 01PassionAwakening.mp3 in order to get 11/Passion.
How can I do this while saving both components?
You can separate your url into two parts like so:
let str : NSString = "www.music.com/Passion/PassionAwakening.mp3"
let path : NSString = str.stringByDeletingLastPathComponent
let ext : NSString = str.lastPathComponent
print(path)
print(ext)
Output
www.music.com/Passion
PassionAwakening.mp3
For more info please have a look at this link.
You should really do away with legacy NS Objective-C classes and manual path string splitting where possible. Use URL instead:
let url = URL(fileURLWithPath: "a/b/c.dat", isDirectory: false)
let path = url.deletingLastPathComponent().relativePath // 'a/b'
let file = url.lastPathComponent // 'c.dat'
That being said, Apple has an explicit FilePath type starting with macOS 11, but with no path manipulation methods. For those you'd have to include the external system package
If you are on macOS 12, the methods from the external package are now also available on the system.
Swift 4+:
let components = path.split(separator: "/")
let directory = components.dropLast(1).map(String.init).joined(separator: "/")
Swift 3:
let str = "11/Passion/01PassionAwakening.mp3"
if !str.isEmpty {
let components = str.characters.split("/")
let head = components.dropLast(1).map(String.init).joinWithSeparator("/")
let tail = components.dropFirst(components.count-1).map(String.init)[0]
print("head:",head,"tail:", tail) // head: 11/Passion tail: 01PassionAwakening.mp3
} else {
print("path should not be an empty string!")
}
This works for Swift 3.0 as well:
let fileName = NSString(string: "11/Passion/01PassionAwakening.mp3").lastPathComponent
Swift 3.0 version
if !str.isEmpty {
let components = str.characters.split(separator: "/")
let head = components.dropLast(1).map(String.init).joined(separator: "/")
let words = components.count-1
let tail = components.dropFirst(words).map(String.init)[0]
print("head:",head,"tail:", tail) // head: 11/Passion tail: 01PassionAwakening.mp3
} else {
print("path should not be an empty string!")
}
rolling back to NSString:
extension String {
var ns: NSString {
return self as NSString
}
var pathExtension: String {
return ns.pathExtension
}
var lastPathComponent: String {
return ns.lastPathComponent
}
var stringByDeletingLastPathComponent: String {
return ns.deletingLastPathComponent
}
}
so you can do:
let folderPath = pathString.stringByDeletingLastPathComponent
Just improvised the solution for URL String. Thank you so much ingconti
extension String {
var ns: URL? {
return URL.init(string: self)
}
var pathExtension: String {
return ns?.pathExtension ?? ""
}
var lastPathComponent: String {
return ns?.lastPathComponent ?? ""
}
var stringByDeletingLastPathComponent: String {
return ns?.deletingLastPathComponent().absoluteString ?? ""
}
}

URL decode in iOS

I am using Swift 1.2 to develop my iPhone application and I am communicating with a http web service.
The response I am getting is in query string format (key-value pairs) and URL encoded in .Net.
I can get the response, but looking the proper way to decode using Swift.
Sample response is as follows
status=1&message=The+transaction+for+GBP+12.50+was+successful
Tried following way to decode and get the server response
// This provides encoded response String
var responseString = NSString(data: data, encoding: NSUTF8StringEncoding) as! String
var decodedResponse = responseString.stringByReplacingEscapesUsingEncoding(NSUTF8StringEncoding)!
How can I replace all URL escaped characters in the string?
To encode and decode urls create this extention somewhere in the project:
Swift 2.0
extension String
{
func encodeUrl() -> String
{
return self.stringByAddingPercentEncodingWithAllowedCharacters( NSCharacterSet.URLQueryAllowedCharacterSet())
}
func decodeUrl() -> String
{
return self.stringByRemovingPercentEncoding
}
}
Swift 3.0
extension String
{
func encodeUrl() -> String
{
return self.addingPercentEncoding( withAllowedCharacters: NSCharacterSet.urlQueryAllowed())
}
func decodeUrl() -> String
{
return self.removingPercentEncoding
}
}
Swift 4.1
extension String
{
func encodeUrl() -> String?
{
return self.addingPercentEncoding( withAllowedCharacters: NSCharacterSet.urlQueryAllowed)
}
func decodeUrl() -> String?
{
return self.removingPercentEncoding
}
}
Swift 2 and later (Xcode 7)
var s = "aa bb -[:/?&=;+!##$()',*]";
let sEncode = s.stringByAddingPercentEncodingWithAllowedCharacters(NSCharacterSet.URLQueryAllowedCharacterSet())
let sDecode = sEncode?.stringByRemovingPercentEncoding
You only need:
print("Decode: ", yourUrlAsString.removingPercentEncoding)
The stringByReplacingEscapesUsingEncoding method is behaving correctly. The "+" character is not part of percent-encoding. This server is using it incorrectly; it should be using a percent-escaped space here (%20). If, for a particular response, you want spaces where you see "+" characters, you just have to work around the server behavior by performing the substitution yourself, as you are already doing.
It's better to use built-in URLComponents struct, since it follows proper guidelines.
extension URL
{
var parameters: [String: String?]?
{
if let components = URLComponents(url: self, resolvingAgainstBaseURL: false),
let queryItems = components.queryItems
{
var parameters = [String: String?]()
for item in queryItems {
parameters[item.name] = item.value
}
return parameters
} else {
return nil
}
}
}
In my case, I NEED a plus ("+") signal in a phone number in parameters of a query string, like "+55 11 99999-5555". After I discovered that the swift3 (xcode 8.2) encoder don't encode "+" as plus signal, but space, I had to appeal to a workaround after the encode:
Swift 3.0
_strURL = _strURL.replacingOccurrences(of: "+", with: "%2B")
In Swift 3
extension URL {
var parseQueryString: [String: String] {
var results = [String: String]()
if let pairs = self.query?.components(separatedBy: "&"), pairs.count > 0 {
for pair: String in pairs {
if let keyValue = pair.components(separatedBy: "=") as [String]? {
results.updateValue(keyValue[1], forKey: keyValue[0])
}
}
}
return results
}
}
in your code to access below
let parse = url.parseQueryString
print("parse \(parse)" )

How to remove characters from a String in swift

I am looking for a simple way to remove the 4 characters in the tilesColored String "ment" from the shuffledWord1.
var word1: String = "employment"
var shuffledWord1: String = "melpyoemtn"
var tilesColored: String = "ment"
var characters = Array(tilesColored) // gives ["m","e","n","t"]
let newWord1 = word1.StringByReplacingOccurencesOfString("\(characters[0])", withString:"") // gives "elpyoetn"
stringByReplacingOccurencesOfString only allows 1 character to be checked and removes BOTH m's, so how can I check against all 4 and only remove ONE instance of each to return "melpyo"?
Thanks in advance for any help possible
Swift 3+ version with better performance than the previous top answers. (Because we don't separate into arrays with substrings, which all would need seperate allocations.)
This here just works on the unicode scalar level. You can paste it right into a playground.
import Foundation
extension String {
func removeCharacters(from forbiddenChars: CharacterSet) -> String {
let passed = self.unicodeScalars.filter { !forbiddenChars.contains($0) }
return String(String.UnicodeScalarView(passed))
}
func removeCharacters(from: String) -> String {
return removeCharacters(from: CharacterSet(charactersIn: from))
}
}
let str = "n1o d2i3g4i5t6s!!!789"
let t1 = str.removeCharacters(from: CharacterSet.decimalDigits.inverted)
print(t1) // will print: 123456789
let t2 = str.removeCharacters(from: "0123456789")
print(t2) // will print: no digits!!!
Swift 3 version of Macondo2Seattle's answer, which, I think, is the most elegant solution.
extension String {
func removing(charactersOf string: String) -> String {
let characterSet = CharacterSet(charactersIn: string)
let components = self.components(separatedBy: characterSet)
return components.joined(separator: "")
}
}
Swift 5:
var phrase = "The rain in Spain stays mainly in the plain."
let vowels: Set<Character> = ["a", "e", "i", "o", "u"]
phrase.removeAll(where: { vowels.contains($0) })
// phrase == "Th rn n Spn stys mnly n th pln."
extension String {
func removeCharacters(characters: String) -> String {
let characterSet = NSCharacterSet(charactersInString: characters)
let components = self.componentsSeparatedByCharactersInSet(characterSet)
let result = components.joinWithSeparator("")
return result
}
}
Swift 2.0:
extension String {
func stringByRemovingOnce(chars: String) -> String {
var cs = Set(chars.characters)
let fd = characters.filter { c in
cs.remove(c).map { _ in false } ?? true
}
return String(fd)
}
}
"melpyoemtn".stringByRemovingOnce("ment") // "lpyoem"
Swift 1.2:
extension String {
func stringByRemovingOnce(chars: String) -> String {
var cs = Set(chars)
let fd = filter(self) { c in
cs.remove(c).map { _ in false } ?? true
}
return String(fd)
}
}
"melpyoemtn".stringByRemovingOnce("ment") // "lpyoem"

Resources