[Solved] iOS 17 – URL encoding and not working with server API – Ios

by
Ali Hasan
axios baseurl swift-concurrency

The Problem:

In an iOS app, a developer is experiencing issues with calling a server API using a URL that contains special characters. After updating to iOS 17, the URL encoding behavior has changed, and the app is now using %5B and %5D to encode square brackets ([ and ]), which is causing the server to return an error. The developer needs to find a way to keep the original characters in the URL to maintain compatibility with the server API.

The Solutions:

Solution 1:Implement the custom protocol by hand

The server implemented protocol is similar to HTTP but not actually HTTP. Apple frameworks support specific protocols but not this non-standard protocol. So, expressing an invalid URL as `URL` is impossible.

The best solution is to correct the server to decode HTTP messages properly, including handling percent-encoded URIs. However, implementing the custom protocol by hand is necessary if you cannot or do not wish to change the server.

Here’s an example of how you might implement the custom protocol:

class Client {
    // ... // Define the necessary variables (connection, host, port, path)
    
    // This method constructs an HTTP request by hand
    func sendRequest() {
        let data = Data("""
        GET \(path) HTTP/1.1\r
        Host: \(host)\r
        \r
        \r
        """.utf8)
        connection?.send(content: data, isComplete: true, completion: .idempotent)
    }

    // This method handles the returned data
    func handle(data: Data) {
        print(String(decoding: data, as: UTF8.self))
    }

    // ... // Define methods for handling the connection and receiving data (These are based on NWConnection)
    
    // The start method initiates the connection process
    func start() {
        let connection = NWConnection(host: host, port: port, using: .tcp)
        connection.stateUpdateHandler = self.stateDidChange(to:)
        self.setupReceive(on: connection)
        self.connection = connection
        connection.start(queue: .main)
    }
}

// Initialize and start the client
let client = Client()
client.start()

This custom implementation closely resembles HTTP. By constructing the request manually, you can ensure that it’s sent as required by the server.

Q&A

Why is my url encoding not working with my server API?

Your server is not implementing HTTP properly.

How can I implement a custom protocol by hand?

You can use the NWConnection class to send HTTP requests manually.

What are the key steps in implementing a custom protocol?

Split the host, port, and query, construct an HTTP request, handle the returned data, and set up a NWConnection.

Video Explanation:

The following video, titled "How to fix the Google Authenticator Wrong Code Try Again error on ...", provides additional insights and in-depth exploration related to the topics discussed in this post.

Play video

How to fix the Google Authenticator Wrong Code Try Again error on iPhone | Problem Solved · Comments291.