URL Encode only Unicode Characters

Make your URLs readable by the Framework SDK

When dealing with Unicode Characters in URL strings, we need to encode Unicode characters to percentages for them to be readable by URL. We do so using the following code:

// https://hashaam.com/2017/11/13/url-encode-only-unicode-characters/
let originalURLString = "https://www.exampleserver.com/articles/latest/لن-تصدق-ما-يمكن-أن-تجده-في-مطبخك-فاحذر-غسل-هذه-الأداة.html"
let allowedCharacterSet = CharacterSet.urlFragmentAllowed
let urlString = originalURLString.addingPercentEncoding(withAllowedCharacters: allowedCharacterSet)!
let url = URL(string: urlString)!

Get Specific URLQueryItem from URLComponents

Consider the following URL:

http://www.example.com/?utm_source=adsite&utm_campaign=adcampaign&utm_term=adkeyword

We are interested in the query parameter value of utm_term. We can parse the URL using the following code:

// https://hashaam.com/2017/09/02/get-specific-urlqueryitem-from-urlcomponents
func queryParam(urlString: String?, param: String) -> String? {
guard let urlString = urlString else { return nil }
guard let urlComponents = URLComponents(string: urlString) else { return nil }
guard let queryItems = urlComponents.queryItems else { return nil }
return queryItems.filter { queryItem in
return queryItem.name == param
}.first?.value
}

And call the function using: