capacitor-community / http

Community plugin for native HTTP
MIT License
208 stars 136 forks source link

catch disabled Local Network case on iOS #241

Open sultanmyrza opened 2 years ago

sultanmyrza commented 2 years ago

In the iOS app, I use @capacitor-community/http to connect to an IoT device (in my case it's GoPro9). When I first connect to GoPro's WiFi Access Point and try to do get a request it was failing silently then I found out the problem was Local Network was disabled for my app (Settings -> MyApp -> Local Network).

When I do http request to GoPro and disable Local Network for my app, the native call ends up here

let task = urlSession.dataTask(with: urlRequest) { (data, response, error) in
  urlSession.invalidateAndCancel()
  if error != nil { <--- My call ends up here 
    return <--- But because of this return I can't catch on ionic side
  }

  let type = ResponseType(rawValue: responseType) ?? .default
  call.resolve(self.buildResponse(data, response as! HTTPURLResponse, responseType: type))
}

the error value is this

2022-02-28 16:10:54.342760+0800 App[1496:224988] Task <A4C6F12F-13CF-458C-9A21-D69F8DF46E36>.<2> finished with error [-1020] Error Domain=NSURLErrorDomain Code=-1020 "A data connection is not currently allowed." UserInfo={_kCFStreamErrorCodeKey=50, NSUnderlyingError=0x2819fe6a0 {Error Domain=kCFErrorDomainCFNetwork Code=-1020 "(null)" UserInfo={_NSURLErrorNWPathKey=unsatisfied (Local network prohibited), interface: en0, ipv4, _kCFStreamErrorCodeKey=50, _kCFStreamErrorDomainKey=1}}, _NSURLErrorFailingURLSessionTaskErrorKey=LocalDataTask <A4C6F12F-13CF-458C-9A21-D69F8DF46E36>.<2>, _NSURLErrorRelatedURLSessionTaskErrorKey=(
    "LocalDataTask <A4C6F12F-13CF-458C-9A21-D69F8DF46E36>.<2>"
), NSLocalizedDescription=A data connection is not currently allowed., NSErrorFailingURLStringKey=http://10.5.5.9:8080/gopro/media/list, NSErrorFailingURLKey=http://10.5.5.9:8080/gopro/media/list, _kCFStreamErrorDomainKey=1}
2022-02-28 16:10:54.342898+0800 App[1496:224988] [NSURLSession sharedSession] may not be invalidated
ERROR MESSAGE:  {"message":"Error","code":"REQUEST","errorMessage":"Error"}
sultanmyrza commented 2 years ago

I solved my issue with by call.reject()

if error != nil {
    call.reject("Error", "REQUEST", error, [:]) <--- just added this line
    return
}
mrahn24 commented 2 years ago

Hi @sultanmyrza,

just want to let you know, that this is a duplicate of #224 and already resolved with #225. 😄

Have a great day!

sultanmyrza commented 2 years ago

Hello @mrahn24 Oh, I see https://github.com/mrahn24/http/commit/da0742fde037e34008a5c86a176a1c33b888bc02 thank you for pointing that out. Then it can be closed now. Hope they will approve you PR soon 🤗

Thank you 🤗