0

I created a project with "watch-only App" template in XCode, running on Apple Watch Series 6, the console output network error: ErrorDomain =NSURLErrorDomain Code=-1009 "The Internet connection appears to be offline." However, It works fine on the Apple watch Ultra.

I try the following:

  1. I added the following code, but doesn't work.
configuration.allowsCellularAccess = true
configuration.waitsForConnectivity = true
  1. Tried using the Alamofire framework, but it didn't work.
import Alamofire
var body: some View {
        VStack {
            Text("hello world")
                .task {
                    URLCache.shared.removeAllCachedResponses()
                    AF.request("https://api.thecatapi.com/v1/images/search?limit=1").response { response in print( response)
                    }
                }
        }
}
  1. Turn off the Wi-Fi and Bluetooth of the mobile phone. The Watch Series 6 can successfully request through Wi-Fi. But that's not an option. I can't ask my users to turn off Wi-Fi and Bluetooth before using my app.

Here is my code:

VStack {
      Text("hello world") 
}
.onAppear {
            guard let url = URL(string: "https://api.thecatapi.com/v1/images/search?limit=1") else {
                print("Invalid URL")
                self.error = "Invalid URL"
                return
            }
            let dataTask = URLSession.shared.dataTask(with: url) {(data, response, error) in

                guard error == nil else {
                    print(error)
                    return
                }

                print(data)
            }
            dataTask.resume()
}

What can I do? My Apple Watch Series 6 version is watchOS 9.0. I noticed when lauching my app first time on the Apple Watch Series 6. It doesn't ask for 'allow "Settings" to use wireless data’.But I was asked about it on the Apple Watch Ultra.

  • Where is your Ultra Watch OS version? It seems to be HardWare/OS issue. – Neklas May 16 '23 at 17:24
  • Ultra Watch OS version 9.4. Probably not a version issue, I have another Watch, Series 5 OS version also 9.4, but it has the same network errors as Watch Series 6 watchOS 9.0 @Neklas – jiaoxiaker abby May 17 '23 at 06:12
  • I think it could be device setting issue. Did u check with other apps yet? To test if other apps can use internet via Cellular network. – Neklas May 17 '23 at 14:35
  • I checked the other apps on the watch, and they all worked with normal Internet connections. But these apps are "dependent apps".@neklas Neklas – jiaoxiaker abby May 18 '23 at 08:16
  • I added my answer, it may help you. – Neklas May 18 '23 at 13:41

1 Answers1

0

Here is my current HTTP request handler:

class BuildInHttpClient: NSObject {
    
    override init() {
        let config = URLSessionConfiguration.default
        config.requestCachePolicy = .reloadIgnoringLocalCacheData
        config.urlCache = nil
        let session = URLSession(configuration: config)
        self.urlSession = session
    }
    public static let current = BuildInHttpClient()
    
    fileprivate var urlSession: URLSession!
    
}

So I think you can add some more configurations to this, like below:

class BuildInHttpClient: NSObject {
    
    override init() {
        let config = URLSessionConfiguration.default
        
        // Here for disabling request caching, and reduce app data size
        config.requestCachePolicy = .reloadIgnoringLocalCacheData
        config.urlCache = nil
        
        // This is true as default, don't need to set it
        config.allowsCellularAccess = true
        
        // Allow when Low Data Mode enabled
        config.allowsConstrainedNetworkAccess = true
        //Some Celluar Networks are expensive cuz it uses your Mobile Network Data, you may need to pay money as a fee
        config.allowsExpensiveNetworkAccess = true
        
        let session = URLSession(configuration: config)
        self.urlSession = session
    }
    public static let current = BuildInHttpClient()
    
    fileprivate var urlSession: URLSession!
}
Neklas
  • 504
  • 1
  • 9
  • I tried your code and it didn't work. **Proxy through iPhone:** Request failure "ErrorDomain =NSURLErrorDomain Code=-1009 "The Internet connection appears to be offline." I guess "Watch Only App" does not support URL session requests through iPhone Proxy – jiaoxiaker abby May 23 '23 at 03:56