Home>

Thank you for viewing.
I am currently developing an iOS app with Swift.
We plan to run Django's API server on the back end.
However, an error will occur even if a request is made from iOS to the API.
I can't find a solution and I'm asking questions.

Purpose

A request is made from the iOS application to the API, and a response in json format is received from the back end.
iOS request implementation uses Ishikawa/APIKit.

Challenge

Communication cannot be performed normally.

Confirmation event

What the error is

The operation could n’t be completed. (APIKit.SessionTaskError error 0.)


It is.
In addition, since the access log is not output to the log of the Django server (on Mac), it is considered that the request has not reached the server.
Access to the server API from the browser can be done normally.

Environment Local server (running on Mac)

Language: Python3
FW: Django, Django-Rest-Framework
IP address: 192.168.2.100:8000

iOS app

Language: Swift4
Library: APIKit, Himotoki

Tried

Sample code that communicates with GithubAPI was created and executed. As a result, the response was accepted normally.
API client base URL was changed as follows and executed.

  1. http://192.168.2.100:8000
  2. http://127.0.0.1:8000
Code API client
import APIKit
import Himotoki
protocol APIRequestProtocol: Request {
}
extension APIRequestProtocol {
    var baseURL: URL {
        return URL (string: "http: // localhost: 8000")!
    }
}
final class API {
    // Sample to get user ID: 4
    struct GetUser: APIRequestProtocol {
        typealias Response = User
        let method: HTTPMethod = .get
        let path: String = "/ api/users/4 /"
        func response (from object: Any, urlResponse: HTTPURLResponse) throws->SnuffleUser {
            return try decodeValue (object)
        }
    }
}
Implementation using API
func getProfile () {
        let request = API.GetUser ()

        Session.send (request) {response in
            switch response {
            case .success (let user):
                self.user = user
            case .failure (let error):
                print (error.localizedDescription)
                print ("Request faild")
            }
        }
    }