MasterJ93 / ATProtoKit

A straightforward solution for using AT Protocol and Bluesky, written in Swift.
https://atprotokit.cjrriley.com/documentation/atprotokit/
MIT License
39 stars 8 forks source link

[Bug]: ATProtoKit claims the hostname doesn't exist #34

Closed MasterJ93 closed 1 month ago

MasterJ93 commented 1 month ago

Summary

When trying to authenticate into Bluesky, an error message appears that states the hostname couldn't be found.

Reproduction Steps

  1. Create a new project in Xcode.
  2. Add ATProtoKit as a package via "File > Package Dependencies...".
  3. Copy and paste the following code:
    
    import SwiftUI
    import SwiftData
    import ATProtoKit

struct ModalView: View { @State private var text: String = "" @State private var progress: Double = 0.0 @State private var session: UserSession? @State private var errorMessage: String?

let config = ATProtocolConfiguration(handle: [username], appPassword: [appPassword])

var body: some View {
    VStack {
        ZStack {
            Circle()
                .stroke(Color.gray.opacity(0.3), lineWidth: 4)
                .frame(width: 20, height: 20)

            Circle()
                .trim(from: 0.0, to: CGFloat(progress))
                .stroke(Color.purple, lineWidth: 4)
                .rotationEffect(.degrees(-90))
                .frame(width: 20, height: 20)
        }
        Button("Post") {
            Task {
                do {
                    guard let session = session else {
                        print("Error: No session available")
                        return
                    }
                    let atProto = ATProtoKit(session: session)
                    let atProtoBluesky = ATProtoBluesky(atProtoKitInstance: atProto)
                    let postResult = try await atProtoBluesky.createPostRecord(text: text)
                    print(postResult)
                } catch {
                    print("Error: \(error)")
                }
            }
        }
        .padding()

        Divider()
            .background(Color.gray)
            .frame(height: 0.5)

        TextEditor(text: $text)

        Spacer()
    }
    .padding()
    .onAppear {
        Task {
            do {
                let session = try await config.authenticate()
                self.session = session
            } catch {
                self.errorMessage = "Error: \(error)"
                print(errorMessage ?? "Unknown error")
            }
        }
    }
}

}

Preview {

ModalView()
    .modelContainer(for: Item.self, inMemory: true)

}

Be sure to replace `[username]` and `[appPassword]` with your username and App Password.
4. In the AppDelegate, change `ContentView` with `ModelView`.
5. Build and run the app.
6. Observe the console.

### Expected Results

A successful authentication so that the user can start posting. In the Bluesky account, you should see "Hello Bluesky!" at the top of your profile's posts feed.

### Actual Results

An error code appears in the console:

Error: Error Domain=NSURLErrorDomain Code=-1003 "A server with the specified hostname could not be found." UserInfo={_kCFStreamErrorCodeKey=-72000, NSUnderlyingError=0x600003c3b030 {Error Domain=kCFErrorDomainCFNetwork Code=-1003 "(null)" UserInfo={_kCFStreamErrorDomainKey=10, _kCFStreamErrorCodeKey=-72000, _NSURLErrorNWResolutionReportKey=Resolved 0 endpoints in 1ms using unknown from query, _NSURLErrorNWPathKey=satisfied (Path is satisfied), interface: en1[802.11], ipv4, dns, uses wifi}}, _NSURLErrorFailingURLSessionTaskErrorKey=LocalDataTask <59E0A79C-4E40-4B19-A8CF-1989FEB83D16>.<1>, _NSURLErrorRelatedURLSessionTaskErrorKey=( "LocalDataTask <59E0A79C-4E40-4B19-A8CF-1989FEB83D16>.<1>" ), NSLocalizedDescription=A server with the specified hostname could not be found., NSErrorFailingURLStringKey=https://bsky.social/xrpc/com.atproto.server.createSession, NSErrorFailingURLKey=https://bsky.social/xrpc/com.atproto.server.createSession, _kCFStreamErrorDomainKey=10}



### What operating systems did you experience this bug? (We'll count Docker as an operating system.)

macOS

### Operating System Version

This was tested with macOS Sequoia 15.0 and Xcode 16.0.

### ATProtoKit Version

0.17.5

### Additional Context

This seems to only happen when using SwiftUI; when creating a CLI application with the code used in the Getting Started guide, it works without question. Perhaps the way SwiftUI works is incompatible with how ATProtoKit is recommended?

`APIClientService` should probably be an actor, given that it's dealing with networking requests and only one instance of it should be available at a time. It appears to make the most sense. This may help to solve the issue.
bienemann commented 1 month ago

I got this and it was caused by not having enabled outgoing connections in the sandbox environment. This can be fixed by, after step 2 of your Reproduction Steps, doing the following:

Build and run you project and the problem should be gone.

MasterJ93 commented 1 month ago

I got this and it was caused by not having enabled outgoing connections in the sandbox environment. This can be fixed by, after step 2 of your Reproduction Steps, doing the following:

  • In XCode, click on your project in the project navigator
  • Select your target
  • Navigate to the 'Signing & Capabilities' tab
  • Navigate to the 'App Sandbox' section
  • Check the box 'Outgoing Connections (Client)'

Build and run you project and the problem should be gone.

Had I seen this comment earlier, I would have closed the issue hours ago. I was able to reach the same conclusion myself. That was a huge blunder on my part, but thank you for that.

In a future update, I'll be sure to have this mentioned somewhere in the documentation in a future update.