3.2 C
New York
Saturday, November 30, 2024

ios – Swift URLSession closes connection instantly, whereas Postman retains it open (Sony TV PIN request)


I am making an attempt to implement a PIN request characteristic for a Sony TV in my iOS app. The aim is to maintain the PIN entry window open on the TV till the consumer enters the PIN. Nonetheless, I am encountering a problem the place the connection is closed instantly when utilizing Swift’s URLSession, whereas the identical request works as anticipated in Postman.

This is my Swift code:

let parameters = """
        {
            "technique": "actRegister",
            "params": [
                {
                    "clientid": "MyDevice:1",
                    "nickname": "My Device",
                    "level": "private"
                },
                [
                    {
                        "value": "yes",
                        "function": "WOL"
                    }
                ]
            ],
            "id": 1,
            "model": "1.0"
        }
        """
        
        guard let postData = parameters.knowledge(utilizing: .utf8) else {
            completion(.failure(NSError(area: "Invalid knowledge", code: 0, userInfo: nil)))
            return
        }
        
        var request = URLRequest(url: URL(string: "http://(ipAddress)/sony/accessControl")!,timeoutInterval: 30)
        request.addValue("utility/json", forHTTPHeaderField: "Content material-Sort")
        request.httpMethod = "POST"
        request.httpBody = postData
        
        let activity = URLSession.shared.dataTask(with: request) { knowledge, response, error in
            if let error = error {
                completion(.failure(error))
                return
            }
            
            guard let knowledge = knowledge else {
                completion(.failure(NSError(area: "No knowledge acquired", code: 0, userInfo: nil)))
                return
            }
            
            if let responseString = String(knowledge: knowledge, encoding: .utf8) {
                print("Response: (responseString)")
            }
        }
    
        activity.resume()

After I ship this request utilizing Postman, the PIN window on the TV stays open as anticipated. I obtain a 401 response, which is regular for the sort of request. In Postman, I can simulate the undesirable conduct by sending the request twice in fast succession, which closes the PIN window.

Nonetheless, once I run this code within the iPhone Simulator, the PIN window on the TV closes instantly after showing.

What I’ve tried:

  • Rising the timeoutInterval
  • Utilizing URLSession.shared.dataTask and URLSession(configuration:delegate:delegateQueue:)
  • Implementing URLSessionDataDelegate strategies

Anticipated conduct:
The PIN window ought to keep open on the TV till the consumer enters the PIN or a timeout happens.

Precise conduct:
The PIN window seems briefly on the TV after which closes instantly.

Questions:

  1. Why does the conduct differ between Postman and my Swift code?
  2. How can I modify my Swift code to maintain the connection open and the PIN window displayed on the TV?
  3. Is there a option to forestall URLSession from routinely closing the connection after receiving the 401 response?

Any insights or recommendations can be vastly appreciated. Thanks!

Setting:

  • iOS 15+
  • Swift 5
  • Xcode 13+

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles