10

I'm creating a custom keyboard for iOS8, and I'm having a tough time figuring out some issues with NSURLConnection.

I'm using the sendAsynchronousRequest: method like this:

[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
        NSLog(@"recieved asynchronous response");

        if (!connectionError) {
            [self connection:nil didReceiveData:data];
            [self connectionDidFinishLoading:nil];
        }
        else{
            NSLog(@"connection failed - %@", connectionError);
        }
    }];

This all seems to work correctly for a period of time (1-2 days), and I can correctly receive data. After this time I start seeing the following error message:

 ***cachedResponseForRequest:key:handler failed: 

Error Domain=NSCocoaErrorDomain Code=4099 "The operation couldn’t be completed. (Cocoa error 4099.)" (The connection to service named com.apple.nsurlstorage-cache was invalidated.) UserInfo=0x17579030 {NSDebugDescription=The connection to service named com.apple.nsurlstorage-cache was invalidated.}; { NSDebugDescription = "The connection to service named com.apple.nsurlstorage-cache was invalidated.";

The only way I then seem to be able to make things work correctly again is to reset the content and setting on the phone. If anyone could point me in the correct direction as to why this is happening I would be very grateful. Thanks in advance.

SteveB
  • 351
  • 1
  • 4
  • 15

4 Answers4

7

This occures because the device didn't allow your keyboard to use internet.

To fix this, first check that your keyboard extention's Info.plist has the value "YES" for the key "RequestsOpenAccess".

Then, after installing your app on a device, go to Settings -> General -> Keyboard -> Keyboards -> , and turn on the "Allow Full Access" button.

Drico
  • 1,284
  • 15
  • 33
  • It's not the case, cause he mentioned that "This all seems to work correctly for a period of time (1-2 days), and I can correctly receive data.", so he already gave this permission. – Alexey Kozhevnikov Oct 03 '14 at 09:22
  • Then maybe he deleted the app in the simulator and thus had to give this permission again :s – Drico Oct 03 '14 at 12:38
  • 1
    This helped me. Note that the `RequestsOpenAccess` is nested within the `NSExtensionAttributes` dictionary (which itself is withing `NSExtension`) – Max Chuquimia Nov 25 '14 at 20:30
6

I also had this problem. When I write a widget for my app, the widget can use network. Then I found how to allow to use network.

Just choose your Target(keyboard) -> Capabilities -> Switch on App Sandbox -> check Network. Incoming Connections(Server) Outgoing Coonections(Client)

Flexo
  • 87,323
  • 22
  • 191
  • 272
DianQK
  • 61
  • 1
  • 3
0

I also had this problem. When I write a widget for my app, the widget can use network. Then I found how to allow to use network.

Just choose your Target(keyboard) -> Capabilities -> Switch on App Sandbox -> check Network. Incoming Connections(Server) Outgoing Coonections(Client)

Sorry ^_^, now I can't upload image(because I sign up just now).

0

I've found the way to avoid the issue.

To solve the issue, add an environment variable before launching tests as follows:

XPC_SIMULATOR_LAUNCHD_NAME="com.apple.CoreSimulator.SimDevice.04042E78-E743-4376-94E9-31842D4770B6.launchd_sim"

However, the 04042E78-E743-4376-94E9-31842D4770B6 is depends on each environment. So it can not be hard-coded. I'm looking for a way to read the value from project settings.

Rahul Sharma
  • 2,867
  • 2
  • 27
  • 40
Qamar
  • 9
  • 4
  • Interesting, can you offer any more details on what this does? I haven't been able to find much documentation for XPC_SIMULATOR_LAUNCHD_NAME yet. – natevw Feb 08 '18 at 17:54