Is Apple Watch not able to make network calls when the app is a standalone app, even though the watch is connected to an iPhone? I'm using the new standalone app target, which does not come with a paired iOS app to which WatchConnectivity requests could be made.
I'm getting "Bad file descriptor" 9 times out of 10, and this is making me think watch apps basically need an iPhone to guarantee some amount of connectivity (my watch doesn't have cellular and I can't connect to public wifi hotspots).
Am I holding this wrong?
Example code snippet to reproduce this:
import Combine
import SwiftUI
var cancellables = Set<AnyCancellable>()
@main
struct NetworkTest_Watch_AppApp: App {
var body: some Scene {
WindowGroup {
Text("Test")
.task {
let url = URL(string: "https://google.com")!
let dataTaskPublisher = URLSession.shared.dataTaskPublisher(for: url)
dataTaskPublisher
.retry(3)
.sink(receiveCompletion: { completion in
print(completion)
}, receiveValue: { response in
print(response)
})
.store(in: &cancellables)
}
}
}
}
Example log failure:
2023-01-05 22:48:54.472214-0800 NetworkTest Watch App[809:862165] [scenes] unable to send desiredFidelity:Never response to desiredFidelityAction:<BLSDesiredFidelityAction: 0x16ed9090; info: 0x0; responder: <_BSActionResponder: 0x16ed93e0; active: YES; waiting: NO> clientInvalidated = NO;
clientEncoded = NO;
clientResponded = NO;
reply = <BSMachPortSendOnceRight: 0x16ed9a90; usable: NO; (809:0:send-once xpcCode) from (779:0:send-once take)>;
annulled = YES;>
2023-01-05 22:50:14.457958-0800 NetworkTest Watch App[809:862418] PDTask <42094D6F-56E0-45A9-9F47-53DB66453C9D>.<1> finished with error [9] Error Domain=NSPOSIXErrorDomain Code=9 "Bad file descriptor" UserInfo={_kCFStreamErrorCodeKey=9, NSErrorPeerAddressKey={length = 28, bytes = 0x1c1ef516 00000000 fd746572 6d6e7573 ... cbc268fa 00000000 }, _kCFStreamErrorDomainKey=1, _NSURLErrorRelatedURLSessionTaskErrorKey=(
"LocalDataPDTask <42094D6F-56E0-45A9-9F47-53DB66453C9D>.<1>",
"LocalDataTask <42094D6F-56E0-45A9-9F47-53DB66453C9D>.<1>"
), _NSURLErrorFailingURLSessionTaskErrorKey=LocalDataPDTask <42094D6F-56E0-45A9-9F47-53DB66453C9D>.<1>}
2023-01-05 22:50:14.459136-0800 NetworkTest Watch App[809:862418] Task <42094D6F-56E0-45A9-9F47-53DB66453C9D>.<1> finished with error [9] Error Domain=NSPOSIXErrorDomain Code=9 "Bad file descriptor" UserInfo={_kCFStreamErrorCodeKey=9, NSErrorPeerAddressKey={length = 28, bytes = 0x1c1ef516 00000000 fd746572 6d6e7573 ... cbc268fa 00000000 }, _kCFStreamErrorDomainKey=1, _NSURLErrorRelatedURLSessionTaskErrorKey=(
"LocalDataTask <42094D6F-56E0-45A9-9F47-53DB66453C9D>.<1>",
"LocalDataPDTask <42094D6F-56E0-45A9-9F47-53DB66453C9D>.<1>",
"LocalDataTask <42094D6F-56E0-45A9-9F47-53DB66453C9D>.<1>"
), _NSURLErrorFailingURLSessionTaskErrorKey=LocalDataTask <42094D6F-56E0-45A9-9F47-53DB66453C9D>.<1>}