1

I am using NSURLconnection to connect to a webservice and receive data. Since I connect to different webservices, I set the connection accessibilityLabel to a string describing the webservice.

In the delegate method

-(void) connectionDidFinishLoading:(NSURLConnection *) connection;

I check connection.accessibilityLabel and run the appropriate code.

This works perfectly in the simulator.

However, on the iPhone itself, connection.accessibilityLabel is always null.

Does anyone know why?

(the connection still works properly and I get my data on the iPhone, I just dont know what to do with it since the accessibility label seems not to get set properly or is erased or something.)

Thanks in advance.

EDIT: here is the code I use to make the connection and assign the label

// create the connection with the request
// and start loading the data
ConnectionDelegate *connDel = [[ConnectionDelegate alloc] init];
connDel.delegate = self;
conn = [[NSURLConnection alloc] initWithRequest:theRequest delegate:connDel];
conn.accessibilityLabel = label;

if (conn) {
    // Create the NSMutableData to hold the received data.
    // receivedData is an instance variable declared elsewhere
    connDel.webData = [[NSMutableData data] retain];
} else {
    // Inform the user that the connection failed.
    NSLog(@"Connection Failed");
}
rustybeanstalk
  • 2,722
  • 9
  • 37
  • 57
  • Be aware that the iPhone Simulator is more "robust" than the iPhone itself. Critical functions are more likely to fail on the iPhone than on the iPhone Simulator. – Paul Apr 07 '11 at 16:05
  • I have a similar problem. I need your help. See [For standard UIKit controls and views, the value of it's accessibilityLabel property on real device can't be got, while the simulator can](http://stackoverflow.com/questions/43671423/for-standard-uikit-controls-and-views-the-value-of-its-accessibilitylabel-prop) – 刘佳杰 Apr 28 '17 at 06:41

2 Answers2

1

Not sure why your accessibilityLabel is only working on the simulator, but a more extendable approach can be found in the accepted answer for this question: Managing multiple asynchronous NSURLConnection connections

Basically you can create a mutable CFDictionary with your connection as a key and whatever you want as the value (in the linked answer he uses the receivedData object, but you could use whatever you wanted).

So to adapt his solution for your situation you could do something like this:

Before creating your requests:

connectionToInfoMapping =
    CFDictionaryCreateMutable(
        kCFAllocatorDefault,
        0,
        &kCFTypeDictionaryKeyCallBacks,
        &kCFTypeDictionaryValueCallBacks);

After each connection is created:

CFDictionaryAddValue(
    connectionToInfoMapping,
    connection,
    [NSMutableDictionary
        dictionaryWithObject:@"someName"
        forKey:@"connectionName"]);

Then in your connectionDidFinishLoading: method:

-(void) connectionDidFinishLoading:(NSURLConnection *) connection
{
    NSMutableDictionary *connectionInfo = CFDictionaryGetValue(connectionToInfoMapping, connection);
    NSString *connectionName = (NSString *)[connectionInfo objectForKey:@"connectionName"];
    if ([connectionName isEqualToString:@"connectionA"]) {
        //Do something
    }
}

The above can easily be extended to have multiple properties mapped by connection (Just add more keys and values to the NSMutableDictionary you add to the CFDictionary).

Community
  • 1
  • 1
theChrisKent
  • 15,029
  • 3
  • 61
  • 62
  • I edited my post to show my code, perhaps there is something wrong with my declarations. As for your solution, does connectionToInfoMapping need to be a global variable? or how else can you access is both in the connectionDidFinishLoading: method and elsewhere. Thanks! – rustybeanstalk Apr 07 '11 at 13:48
  • `connectionToInfoMapping` should be a member of the calling class (`self` in your code above). This makes it "global" to the methods of `self`. So just add it to your .h file. – theChrisKent Apr 07 '11 at 14:14
0

I believe the problem is with the use of accessibilityLabel. Looking at the docs for it, it's intended to be used on UIKit controls to display the correct label on an accessibility element. Its behavior on NSURLConnection objects is undefined. I think you need to find another way of identifying the web service to your delegate.

Using an NSDictionary in ConnectionDelegate to associate the web service with active connections is as @theChrisKent recommended would work.

You could also create a subclass of NSURLConnection and add a webServiceLabel iVar & property to that.

XJones
  • 21,959
  • 10
  • 67
  • 82