Skip to content

Instantly share code, notes, and snippets.

@sabapathyk7
Created December 29, 2023 17:58
Show Gist options
  • Save sabapathyk7/1e552c303896c9f49263b64dd30015a8 to your computer and use it in GitHub Desktop.
Save sabapathyk7/1e552c303896c9f49263b64dd30015a8 to your computer and use it in GitHub Desktop.
import Foundation
let sampleURL = URL(string: "https://example.com/auth-callback#access_token=mytoken&expires_in=36000&scope=zap+profile")
var sampleComponents = URLComponents()
sampleComponents.query = sampleURL?.fragment
for item in sampleComponents.queryItems! {
print("\(item.name): \(item.value)")
}
//host - The host subcomponent.
//var host: String? { get set }
var url = URL(string: "https://sabapathy7.medium.com")
url = url?.appending(component: "users")
print(url)
var components = URLComponents()
components.scheme = "https"
components.host = "sabapathy7.medium.com"
print(components.url)
let queryItemToken = URLQueryItem(name: "token", value: "12345")
let queryItemQuery = URLQueryItem(name: "query", value: "swift ios")
components.queryItems = [queryItemToken, queryItemQuery]
print(components.url)
components.fragment = "five"
components.user = "saba"
components.password = "password"
print(components.url)
let decodeURL = URL(string: "https://saba:password@sabapathy7.medium.com?token=12345&query=swift%20ios#five")
let decodeComponents = URLComponents(url: decodeURL!, resolvingAgainstBaseURL: false)
if let components = decodeComponents {
print(components.host)
print(components.query)
print(components.percentEncodedQuery)
if let queryItems = components.queryItems {
for queryItem in queryItems {
print("\(queryItem.name): \(queryItem.value)")
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment