Connect to localhost in SwiftUI

Sometimes Apple's fancy security is really quite the headache....
Anyway, I'm trying to connect a SwiftUI to a simple server (running in Rust + Rocket) on my localhost (using Alamofire), but I can't get it to work with this:

import Foundation
import Alamofire

let evaluators:[String:ServerTrustEvaluating]=[
let manager=ServerTrustManager(evaluators: evaluators)
let session=Session.init(serverTrustManager:manager)

//Responsible for interacting with the Novella Server, and transforming the data into a native swift representation
class NovellaClient{
   static func get_hello() async throws ->String{
        let req = session.request("",method:.get)
        let res = try await req.serializingString().response.result.get()
        return res

test code...

I've enabled NSAllowLocalNetworking in the info.plist and self-signed an SSL certificate, but every time I try to run, it emits this nonsense

2022-06-06 01:40:21.390541-0500 novellex[1030:2703703] [connection] nw_socket_connect [C1:2] connectx(7 (guarded), [srcif=0, srcaddr=<NULL>, dstaddr=], SAE_ASSOCID_ANY, 0, NULL, 0, NULL, SAE_CONNID_ANY) failed: [1: Operation not permitted]
2022-06-06 01:40:21.391165-0500 novellex[1030:2703703] [connection] nw_socket_connect [C1:2] connectx failed (fd 7) [1: Operation not permitted]
2022-06-06 01:40:21.391185-0500 novellex[1030:2703703] [] nw_socket_connect connectx failed [1: Operation not permitted]
2022-06-06 01:40:21.392047-0500 novellex[1030:2703703] Connection 1: received failure notification
2022-06-06 01:40:21.392075-0500 novellex[1030:2703703] Connection 1: failed to connect 1:1, reason -1
2022-06-06 01:40:21.392088-0500 novellex[1030:2703703] Connection 1: encountered error(1:1)
2022-06-06 01:40:21.392968-0500 novellex[1030:2703703] Task <A331604F-AD59-46E3-A4C8-21F1DE59162F>.<1> HTTP load failed, 0/0 bytes (error code: 1 [1:1])
2022-06-06 01:40:21.393358-0500 novellex[1030:2703704] Task <A331604F-AD59-46E3-A4C8-21F1DE59162F>.<1> finished with error [1] Error Domain=NSPOSIXErrorDomain Code=1 "Operation not permitted" UserInfo={_NSURLErrorFailingURLSessionTaskErrorKey=LocalDataTask <A331604F-AD59-46E3-A4C8-21F1DE59162F>.<1>, _kCFStreamErrorDomainKey=1, _kCFStreamErrorCodeKey=1, _NSURLErrorRelatedURLSessionTaskErrorKey=(
    "LocalDataTask <A331604F-AD59-46E3-A4C8-21F1DE59162F>.<1>"
), _NSURLErrorNWPathKey=satisfied (Path is satisfied), interface: lo0}

Anyone have ideas? Help would be most appreciated!

Pardon any glaring errors, I am very new to using swift, mac/ios development in general, and networking, so its quite the ride.

You should add Network related entitlement. See the post above :point_up_2:.

And you can enable it in "Xcode project - Signing & Capabilities" :point_down:

By the way, this issue is irrelevant to swift itself and macOS only. A better website for this is Apple Developer Forums

1 Like

Thank you so much!