7

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>}
featherless
  • 2,118
  • 19
  • 19
  • What sort of network connections are you making? URLSession or similar? The watch will access the network via the paired iPhone. If the paired iPhone is not in Bluetooth range and it isn't connected directly to wifi and doesn't have cellular data then it will not be able to access the network. – Paulw11 Jan 06 '23 at 06:39
  • Added an example code snippet. This happens with all forms of URLSession. – featherless Jan 06 '23 at 06:48
  • In terms of hardware/environment, my phone is next to the watch and the watch is on my wrist. The watch does not have cellular access, and the phone is connected to both 5G and Wifi. I'm not able to connect the watch to wifi because I'm at a public hotspot that requires authentication. – featherless Jan 06 '23 at 06:49
  • fwiw using AlamoFire succeeds pretty much every time. Seems to be something AlamoFire is doing to work around this bug. – featherless Jan 06 '23 at 07:43
  • I had no problem with your code. I turned off wifi and cellular data on my watch and tried with and without wifi enabled on my phone. – Paulw11 Jan 06 '23 at 07:46
  • Does it work consistently for you? I find it does work sometimes, but more often than not it fails. – featherless Jan 06 '23 at 07:53
  • btw this works fine: import Alamofire var body: some Scene { WindowGroup { Text("Test") .task { URLCache.shared.removeAllCachedResponses() AF.request("https://google.com").debugLog().response { response in print(response) } } } } – featherless Jan 06 '23 at 07:54
  • It worked for me 100% – Paulw11 Jan 06 '23 at 08:17
  • Sigh. Will keep poking at this then; thank you for trying it out! – featherless Jan 06 '23 at 08:19
  • 1
    @featherless I'm running into this problem with a newly created standalone watchOS app, too, and have yet to find the source or a solution. Did you ever solve this? – bmt22033 May 04 '23 at 13:36

1 Answers1

0

In Target->WatchProject->General check the Supports Running Without iOS App Installation

enter image description here

Qazi Ammar
  • 953
  • 1
  • 8
  • 23