iosswiftmongodbmongokitten

Can't connect with Mongokitten from iOS device


I setup a connection to a MongoDB database with Mongokitten. This is the code I do to manage the connection :

class MongoManager {
static let shared = MongoManager()

private var collIndex: MongoCollection?
private var collCourses: MongoCollection?
let connectionString = "mongodb+srv://user:password@myCluster.vd2s3.mongodb.net/mongolf?retryWrites=true&w=majority&appName=myCluster"
var db: MongoDatabase?

var isConnected: Bool {
    return db != nil
}

func connect() async {
    if isConnected {
        return
    }
    do {
        db = try await MongoDatabase.connect(to: connectionString)
        if isConnected {
            collIndex = db!["index"]
            collCourses = db!["courses"]
            print("Connected to MongoDB")
        }
    } catch {
        print("Error Init MongoDB : \(error)")
    }
}

}

It works perfectly from the simulator, but from the real iOS Device I get "Error Init MongoDB : UnableToParseConfig()"

Any idea what I could check ?


Solution

  • The problem occurred on DNS, and the solution is to add a personalized DNS server in the ConnectionString.

    let connectionString = "mongodb+srv://user:password@myCluster.vd2s3.mongodb.net/mongolf?retryWrites=true&w=majority&appName=myCluster&dnsServer=1.1.1.1"