URLSession์„ ๋งŒ๋“ค์—ˆ๋‹ค๋ฉด, ๊ทธ ์•ˆ์— ๋“ค์–ด๊ฐˆ URL๊ณผ Request๋ฅผ ์ •์˜ํ•ด์•ผ ํ•œ๋‹ค. ์ด๋ฒˆ์—๋„ ๋ฌธ์„œ๋ฅผ ๋ณด๋ฉฐ ์•Œ์•„๋ณด์ž.

URL

let url = URL(string: "http://www.example.com")

์„ธ์…˜์„ ๋งŒ๋“ค์—ˆ๋‹ค๋ฉด, ์ด์ œ Resource์˜ ์œ„์น˜๋ฅผ ์ ์–ด์ค„ ์ฐจ๋ก€๋‹ค. URL๋กœ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค. Apple์—์„œ ์ œ๊ณตํ•˜๋Š” URL์€ local file, remote resource ๋ชจ๋‘์— ์ ‘๊ทผ๊ฐ€๋Šฅํ•˜๋‹ค. local๋กœ ์ž‘์„ฑํ•œ ๊ฒฝ์šฐ๋„ค๋Š” ํŒŒ์ผ์˜ ๋งˆ์ง€๋ง‰ ์ˆ˜์ • ๋‚ ์งœ ๋ณ€๊ฒฝ๊ณผ ๊ฐ™์ด ํŒŒ์ผ ์†์„ฑ์„ ์ง์ ‘ ์กฐ์ž‘ํ•  ์ˆ˜๋„ ์žˆ๋‹ค.

URL์€ local file์„ ์ฐธ์กฐํ•  ๋•Œ ์„ ํ˜ธ๋˜๋Š” ๋ฐฉ๋ฒ•์ด๋‹ค.ํŒŒ์ผ์—์„œ ๋ฐ์ดํ„ฐ๋ฅผ ์ฝ๊ฑฐ๋‚˜, ์“ฐ๋Š” ๊ฐ์ฒด์—๋Š” ๊ฒฝ๋กœ ์ด๋ฆ„ ๋Œ€์‹  ๋Œ€๋ถ€๋ถ„ URL์„ ๋ฐ›๋Š” method๋“ค์ด ์žˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด String.init(contenstOf:encoding:), Data.init(contentsOf:options:) ๊ฐ€ ์žˆ๋‹ค.

URL Component

URL์€ ์ƒ๊ฐ๋ณด๋‹ค ๋ณต์žกํ•˜๋‹ค. ์ฝ์„ ์ˆ˜๋Š” ์žˆ์ง€๋งŒ ์—ฐ์†๋œ ๋ฌธ์ž์˜ ๋‚˜์—ด๋กœ ์ธํ•ด ํŒŒํŒ!ํ•˜๊ณ  ๋ญ๊ฐ€ ๋ญ”์ง€ ํŒŒ์•…ํ•˜๊ธฐ๋Š” ์–ด๋ ค์šธ ์ˆ˜ ์žˆ๋‹ค. ์ด๋Ÿฌํ•œ ๋ถ€๋ถ„์— ๋Œ€ํ•ด์„œ ์‰ฝ๊ฒŒ ๋‹ค๋ฃฐ ์ˆ˜ ์žˆ๋„๋ก URL์„ ๊ตฌ๋ฌธ๋ถ„์„ํ•˜๋Š” struct๊ฐ€ ์ด๋…€์„์ด๋‹ค. RFC 3986์— ๋”ฐ๋ผ parsing ํ•œ๋‹ค. ๊ธฐ์กด URL๋กœ ๋งŒ๋“ค์–ด์ง„ ๊ฐ์ฒด๋กœ๋ถ€ํ„ฐ Component๋ฅผ ์‰ฝ๊ฒŒ ์–ป์„ ์ˆ˜ ์žˆ๊ณ , ๊ทธ ๋ฐ˜๋Œ€๋„ ๊ฐ€๋Šฅํ•˜๋‹ค.

Generating

var urlComponents = URLComponents(string: "https://itunes.apple.com/search?")!
 
var mediaQuery = URLQueryItem(name: "media", value: "music")
var entityQuery = URLQueryItem(name: "entity", value: "song")
var termQuery = URLQueryItem(name: "term", value: "The Weekend")
 
urlComponents.queryItems?.append(mediaQuery)
urlComponents.queryItems?.append(entityQuery)
urlComponents.queryItems?.append(termQuery)
 
var requestURL = urlComponents.url! // https://itunes.apple.com/search?media=music&entity=song&term=The%20Weekend

๊ทธ๋ƒฅ url ์ž์ฒด๋ฅผ ์ž…๋ ฅํ•ด์„œ ์ฒ˜๋ฆฌํ•  ์ˆ˜๋„ ์žˆ๊ฒ ์ง€๋งŒ, ํ˜น์‹œ ๋ชจ๋ฅผ ์‹ค์ˆ˜๋ฅผ ๋ฐฉ์ง€ํ•˜๋ ค๋ฉด URLComponent๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ๋„ ์ข‹์€ ๋ฐฉ๋ฒ•์ด๋‹ค.

Parsing

let url = "https://itunes.apple.com/search?media=music&entity=song&term=The%20Weekend"
        
let urlComponents = URLComponents(string: url)
 
print(urlComponents?.scheme)
print(urlComponents?.host)
print(urlComponents?.path)
print(urlComponents?.query)
 
let items = urlComponents?.queryItems ?? []
for item in items {
    print("name : \(item.name), value : \(item.value)")
}
 
// Optional("https")
// Optional("itunes.apple.com")
// Optional("/search")
// Optional("media=music&entity=song&term=The Weekend")
// name : media, value : Optional("music")
// name : entity, value : Optional("song")
// name : term, value : Optional("The Weekend")

url ์ •๋ณด๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ parsing๋„ ๊ฐ€๋Šฅํ•œ๋ฐ, ์œ„์™€ ๊ฐ™์ด ๋‹ค์–‘ํ•œ ์ •๋ณด๋ฅผ ๋ฐ”๋กœ๋ฐ”๋กœ ๊ฐ€์ ธ๋‹ค๊ฐ€ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. parsing ์ •๋ณด๋ฅผ ํ™•์ธํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด, URLComponents๋ฅผ ์ฐธ๊ณ ํ•˜์ž.

URLRequest

URL์„ ๋งŒ๋“ค์—ˆ๋‹ค๋ฉด ์ด์ œ ์ด๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ Request๋ฅผ ๋งŒ๋“ค์–ด์•ผ ํ•œ๋‹ค. URLRequest์—์„œ๋Š” request์— ๋Œ€ํ•œ ์ •๋ณด๋งŒ์„ ๊ธฐ์ˆ ํ•œ๋‹ค. ์‹ค์ œ๋กœ ์ด ์ •๋ณด๋ฅผ ๊ฐ€์ง€๊ณ  ์š”์ฒญ์˜ ๊ฒฐ๊ณผ๋ฅผ ์–ป๊ธฐ ์œ„ํ•ด์„œ๋Š” URLSession์„ ์‚ฌ์šฉํ•ด์•ผ ํ•œ๋‹ค. ๊ธฐ๋ณธ์ ์œผ๋กœ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์ƒ์„ฑ์ž๋ฅผ ๊ฐ€์ง„๋‹ค.

init(url: URL, cachePolicy: URLRequest.CachePolicy = .useProtocolCachePolicy, timeoutInterval: TimeInterval = 60.0)

์—ฌ๊ธฐ์„œ Cache ์ •์ฑ…์„ ์ •ํ•  ์ˆ˜ ์žˆ๋Š”๋ฐ, ๊ธฐ๋ณธ ๊ฐ’์€ NSURLRequest.CachePolicy.useProtocolCachePolicy์ด๋‹ค. ๋™์ž‘์„ ํ™•์ธํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด NSURLRequest.CachePolicy๋ฅผ ์ฐธ๊ณ ํ•˜์ž.

var request = URLRequest(url: url)
request.httpMethod = "POST"
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
request.addValue("application/json", forHTTPHeaderField: "Accept")
request.httpBody = try JSONSerialization.data(withJSONObject: body, options: JSONSerialization.WritingOptions.prettyPrinted)
request.allowsCellularAccess = true

์œ„์™€ ๊ฐ™์ด header ์ถ”๊ฐ€, http body ์ถ”๊ฐ€์™€ ๊ฐ™์€ ๊ฒƒ๋“ค์„ ํ•  ์ˆ˜ ์žˆ๋‹ค. ์ด์ „์— configuration์—์„œ cellular ์ ‘๊ทผ ๊ถŒํ•œ๋“ฑ์„ ์„ค์ •ํ•  ์ˆ˜ ์žˆ๋‹ค๊ณ  ํ–ˆ๋Š”๋ฐ, URLRequest์—์„œ๋„ ํ•ด๋‹น Request์— ๋Œ€ํ•ด์„œ๋งŒ ๋”ฐ๋กœ ์„ค์ •ํ•  ์ˆ˜๋„ ์žˆ๋‹ค.

๋งˆ๋ฌด๋ฆฌ

URL๊ณผ Request๋ฅผ ๋งŒ๋“œ๋Š” ๋ฐฉ๋ฒ•์„ ์•Œ์•„๋ณด์•˜๋‹ค. ๊ทธ๋Ÿผ ๋‹ค์Œ๊ธ€์—์„œ๋Š” ์‹ค์ œ๋กœ Task๋กœ ๋„คํŠธ์›Œํฌ ํ†ต์‹ ์„ ํ•ด๋ณด์ž. ๋!

Reference