Objective-C 和 Swift URL 编码
URL 编码是 iOS 应用开发中必不可少的一部分。很多时候,您需要对 URL 进行编码才能与服务器进行通信。让我们看看如何在 Swift 和 Objective-C 中对 URL 进行编码的一些示例。
Swift 中的 URL 编码
在 Swift 中,您可以使用 addingPercentEncoding(withAllowedCharacters:) 方法进行 URL 编码。
Objective-C 中的 URL 编码
在 Objective-C 中,您可以使用 stringByAddingPercentEncodingWithAllowedCharacters 方法进行 URL 编码。
在 Objective-C 中编码 URL
在 Objective-C 中,您可以使用 NSString 类的 stringByAddingPercentEncodingWithAllowedCharacters: 方法对 URL 字符串进行编码。这是一个示例
NSString *stringToEncode = @"https://www.exampleserver.com/complete path with spaces"; NSCharacterSet *allowedCharacters = [NSCharacterSet URLQueryAllowedCharacterSet]; NSString *encodedString = [stringToEncode stringByAddingPercentEncodingWithAllowedCharacters:allowedCharacters]; NSLog(@"%@", encodedString); // Output: https://www.exampleserver.com/complete%20path%20with%20spaces
在 Swift 中编码 URL
在 Swift 中,您可以使用 String 类的 addingPercentEncoding(withAllowedCharacters:) 方法对 URL 字符串进行编码。这是一个示例 -
示例
import Foundation
let stringToEncode = "https://www.exampleserver.com/complete path with spaces"
let allowedCharacters = CharacterSet.urlQueryAllowed
if let encodedString = stringToEncode.addingPercentEncoding(withAllowedCharacters: allowedCharacters) {
print("Original URL: \(stringToEncode)")
print("Encoded URL: \(encodedString)")
}
输出
Original URL: https://www.exampleserver.com/complete path with spaces Encoded URL: https://www.exampleserver.com/complete%20path%20with%20spaces
在这两种情况下,allowedCharacters 变量都指定了允许出现在编码后的 URL 中的字符集。URLQueryAllowedCharacterSet 是一个常用的用于编码 URL 的字符集。
这是一个在 Swift 中使用 URL 编码进行网络请求的示例
URL 编码是网络通信中必要的一步,以确保 URL 和数据得到正确处理和传输,而不会导致错误或歧义。您可以按照以下步骤在网络中使用 URL 编码 -
在将数据作为请求的一部分发送时,对包含在 URL 或请求正文中的任何查询参数、表单数据或其他数据进行编码。您可以使用适用于您的编程语言的适当编码方法,如前面在本对话中所述。
服务器在接收数据作为响应时也可能提供编码数据,例如 URL 中的查询参数或特殊字符。必须使用适用于您的编程语言的正确解码技术(例如 Java 中的 URLDecoder 或 Swift 中的 URLComponents)来解码这些数据。
在创建动态 URL 时,请确保使用正确的编码方法对任何不寻常的字符、空格或查询参数进行编码。通过这样做,您可以验证您的 URL 是否格式正确,并有助于防止出现问题。
示例
import Foundation
import UIKit
func sendSearchRequest() {
// creating a URL and URLComponents object
let urlString = "https://api.example.com/search"
var urlComponents = URLComponents(string: urlString)
// encode query parameters
let queryItems = [
URLQueryItem(name: "query", value: "tutorials point"),
URLQueryItem(name: "limit", value: "20")
]
urlComponents?.queryItems = queryItems.map { item in
let encodedValue = item.value?.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)
return URLQueryItem(name: item.name, value: encodedValue)
}
// creating a URL request from the URLComponents object
guard let url = urlComponents?.url else { return }
var request = URLRequest(url: url)
request.httpMethod = "GET"
// URL: https://api.example.com/search?query=tutorials%2520point&limit=20
// sending the request and handling the response
let task = URLSession.shared.dataTask(with: request) { data, response, error in
// Decode response data as needed
}
// resuming the task
task.resume()
}
在此示例中,使用基本 URL 和查询参数集合来构造 URLComponents 对象。然后,使用 addingPercentEncoding 方法对查询参数值进行编码,并将其作为查询项放入 URLComponents 对象中。然后,在从 URL 创建的 URLRequest 中将 HTTP 方法设置为“GET”,并将请求通过 URLSession 发送。在收到响应后,我们可以根据需要解码信息。
以下是 URL 编码的一些常见用例
当用户在网站上提交表单时,表单数据通常作为 URL 中的查询参数发送。由于查询参数可能包含特殊字符,因此必须在提交之前对其进行编码。
对 URL 中的特殊字符进行编码:为了避免与 URL 的其他元素产生混淆,必须对诸如 &、?和 = 等特殊字符进行编码。
URL 中的空格应谨慎编码,因为它们可能会导致问题或被视为独立的 URL 组件。为了避免这种情况,空格通常被编码为“%20”。
结论
在 Objective-C 和 Swift 中,我们可以使用 stringByAddingPercentEncodingWithAllowedCharacters: 和 addingPercentEncoding(withAllowedCharacters:) 等方法对 URL 字符串进行编码。然后,我们可以使用编码后的字符串发送请求和接收响应,并根据需要解码数据。
数据结构
网络
关系数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP