AppClip URLSession network request problem

Hi, I have pretty trivial problem. So, I am trying to use URLSession network request but I am getting errors.

This code is in the shared file that works when I run it as app, but when I run it as AppClip it is not working. Running on the simulator is working even as AppClip.


Here is the code (pretty trivial):

Code Block
    let session = URLSession.shared
    let url = URL(string: "https://learnappmaking.com/ex/users.json")!
    let task = session.dataTask(with: url, completionHandler: { _, response, error in
      print(error)
      print(response)
    })
    task.resume()


I am getting this error:

Code Block
2020-11-20 19:35:05.189273+0100 ExampleAppClip[14703:4633304] [Client] Updating selectors after delegate removal failed with: Error Domain=NSCocoaErrorDomain Code=4099 "The connection to service on pid 92 named com.apple.commcenter.coretelephony.xpc was invalidated from this process." UserInfo={NSDebugDescription=The connection to service on pid 92 named com.apple.commcenter.coretelephony.xpc was invalidated from this process.}
2020-11-20 19:35:05.363730+0100 ExampleAppClip[14703:4633299] [connection] nw_socket_connect [C1.1:3] connectx(8 (guarded), [srcif=0, srcaddr=<NULL>, dstaddr=104.27.132.57:443], SAE_ASSOCID_ANY, 0, NULL, 0, NULL, SAE_CONNID_ANY) failed: [65: No route to host]
2020-11-20 19:35:05.364100+0100 ExampleAppClip[14703:4633299] [connection] nw_socket_connect [C1.1:3] connectx failed (fd 8) [65: No route to host]
2020-11-20 19:35:05.364195+0100 ExampleAppClip[14703:4633299] [] nw_socket_connect connectx failed [65: No route to host]
2020-11-20 19:35:05.368229+0100 ExampleAppClip[14703:4633299] [connection] nw_socket_connect [C1.2:3] connectx(8 (guarded), [srcif=0, srcaddr=<NULL>, dstaddr=104.27.133.57:443], SAE_ASSOCID_ANY, 0, NULL, 0, NULL, SAE_CONNID_ANY) failed: [65: No route to host]
2020-11-20 19:35:05.368424+0100 ExampleAppClip[14703:4633299] [connection] nw_socket_connect [C1.2:3] connectx failed (fd 8) [65: No route to host]
2020-11-20 19:35:05.368484+0100 ExampleAppClip[14703:4633299] [] nw_socket_connect connectx failed [65: No route to host]
2020-11-20 19:35:05.370781+0100 ExampleAppClip[14703:4633299] [connection] nw_socket_connect [C1.3:3] connectx(8 (guarded), [srcif=0, srcaddr=<NULL>, dstaddr=172.67.210.249:443], SAE_ASSOCID_ANY, 0, NULL, 0, NULL, SAE_CONNID_ANY) failed: [65: No route to host]
2020-11-20 19:35:05.370989+0100 ExampleAppClip[14703:4633299] [connection] nw_socket_connect [C1.3:3] connectx failed (fd 8) [65: No route to host]
2020-11-20 19:35:05.371054+0100 ExampleAppClip[14703:4633299] [] nw_socket_connect connectx failed [65: No route to host]
2020-11-20 19:35:05.372142+0100 ExampleAppClip[14703:4633299] Connection 1: received failure notification
2020-11-20 19:35:05.372241+0100 ExampleAppClip[14703:4633299] Connection 1: failed to connect 1:65, reason -1
2020-11-20 19:35:05.372291+0100 ExampleAppClip[14703:4633299] Connection 1: encountered error(1:65)
2020-11-20 19:35:05.374764+0100 ExampleAppClip[14703:4633300] Task <3C72DFED-F839-4BD2-9FE2-CC0B8BB7090F>.<1> HTTP load failed, 0/0 bytes (error code: -1004 [1:65])
2020-11-20 19:35:05.383016+0100 ExampleAppClip[14703:4633299] Task <3C72DFED-F839-4BD2-9FE2-CC0B8BB7090F>.<1> finished with error [-1004] Error Domain=NSURLErrorDomain Code=-1004 "Could not connect to the server." UserInfo={_kCFStreamErrorCodeKey=65, NSUnderlyingError=0x282ecc5a0 {Error Domain=kCFErrorDomainCFNetwork Code=-1004 "(null)" UserInfo={_kCFStreamErrorCodeKey=65, _kCFStreamErrorDomainKey=1}}, _NSURLErrorFailingURLSessionTaskErrorKey=LocalDataTask <3C72DFED-F839-4BD2-9FE2-CC0B8BB7090F>.<1>, _NSURLErrorRelatedURLSessionTaskErrorKey=(
"LocalDataTask <3C72DFED-F839-4BD2-9FE2-CC0B8BB7090F>.<1>"
), NSLocalizedDescription=Could not connect to the server., NSErrorFailingURLStringKey=https://learnappmaking.com/ex/users.json, NSErrorFailingURLKey=https://learnappmaking.com/ex/users.json, _kCFStreamErrorDomainKey=1}
Optional(Error Domain=NSURLErrorDomain Code=-1004 "Could not connect to the server." UserInfo={_kCFStreamErrorCodeKey=65, NSUnderlyingError=0x282ecc5a0 {Error Domain=kCFErrorDomainCFNetwork Code=-1004 "(null)" UserInfo={_kCFStreamErrorCodeKey=65, _kCFStreamErrorDomainKey=1}}, _NSURLErrorFailingURLSessionTaskErrorKey=LocalDataTask <3C72DFED-F839-4BD2-9FE2-CC0B8BB7090F>.<1>, _NSURLErrorRelatedURLSessionTaskErrorKey=(
"LocalDataTask <3C72DFED-F839-4BD2-9FE2-CC0B8BB7090F>.<1>"
), NSLocalizedDescription=Could not connect to the server., NSErrorFailingURLStringKey=https://learnappmaking.com/ex/users.json, NSErrorFailingURLKey=https://learnappmaking.com/ex/users.json, _kCFStreamErrorDomainKey=1})
nil

URLSession with background activity cannot be performed in App Clip, as described in this doc. I quote:


To preserve resources and help to protect user privacy, App Clips can’t perform background activity. For example, App Clip’s can’t make use of the following:

  • Background networking with URLSession


Further it implies that you can perform network calls, but without background activity:

App Clips provide an in-the-moment experience and focus on offering the quickest possible solution to an everyday task, so some functionality works best in your full app. Reserve the following functionality for the full app:

  • Advanced networking features like Bonjour and low-level networking APIs like CFSocket, or POSIX functions; instead, use URLSession or the Network framework.


I hope this helps






@liazkam

I see, but from my code I don't think that I am using background activity. As I said this is fairly simple request.

Code Block
 let session = URLSession.shared
 let url = URL(string: "https://learnappmaking.com/ex/users.json")!
let task = session.dataTask(with: url, completionHandler: { _, response, error in
      print(error) 
      print(response)
 })    
task.resume()


And this is working on simulator too but on device not.
I have faced with the same issue. Just reboot your device. It helped to me

I have faced similar errors when using NetworkLinkConditioner with AppClips. I think NetworkLinkConditioner is broken in latest iOS versions because these errors appears as soon as NLC is turned on. And after turned off it continue appears until restart the device. Code=-1004 "Could not connect to the server." _kCFStreamErrorCodeKey=65

AppClip URLSession network request problem
 
 
Q