WKInterfaceImage from URL in Watch OS 2

I was using the code below in Watch OS 1 and it displayed image thumbnails from URLs nicely in table rows.


Then in Watch OS 2, it works on the Simularor but not on the Wacth. I have made sure that the call is https to be App Transport Security compliant. I have looked as other suggestions to use NSURLSession (since the Extension is now in the Watch?) but I am not sure how to do that in code that loops through an object to get the data the cells in a table. Any help would be greatly appreciated.


for (NSInteger i = 0; i < self.libraryTable.numberOfRows; i++)

{

NSObject *row = [libraryTable rowControllerAtIndex:i];

NSString *title = self.titles[i];

NSString *description = self.descriptions[i];


NSString *urlString = self.thumbnails[i];

urlString = [urlString stringByReplacingOccurrencesOfString:@"http" withString:@"https"];

NSURL *url = [NSURL URLWithString:urlString];

NSData *data = [NSData dataWithContentsOfURL:url];

UIImage *image = [UIImage imageWithData:data];

MyRowController *ordinaryRow = (MyRowController *) row;

[ordinaryRow.titleLabel setText:title];

[ordinaryRow.descriptionLabel setText:description];

[ordinaryRow.videoThumbnail setImage:image];

}

Answered by tbneary in 63348022

I figured it out using NSURLSession. Here is the working code for Watch OS 2.0


        for (NSInteger i = 0; i < self.meTable.numberOfRows; i++)
        {
            NSObject *row = [meTable rowControllerAtIndex:i];
            NSString *title = self.titles[i];
            NSLog(@"Row X Title: %@", title);
            NSString *description = self.descriptions[i];
            NSString *urlString = self.thumbnails[i];
            urlString = [urlString stringByReplacingOccurrencesOfString:@"http" withString:@"https"];
           
            NSURLSessionConfiguration *conf = [NSURLSessionConfiguration defaultSessionConfiguration];
            NSURLSession *session = [NSURLSession sessionWithConfiguration:conf];
            [[session dataTaskWithURL:[NSURL URLWithString:urlString]
                    completionHandler:^(NSData *data,
                                        NSURLResponse *response,
                                        NSError *error) {
                        MyRowController *ordinaryRow = (MyRowController *) row;
                        [ordinaryRow.titleLabel setText:title];
                        [ordinaryRow.descriptionLabel setText:description];
                        [ordinaryRow.videoThumbnail setImage:[UIImage imageWithData:data]];
                       
                    }] resume];
       
        }
Accepted Answer

I figured it out using NSURLSession. Here is the working code for Watch OS 2.0


        for (NSInteger i = 0; i < self.meTable.numberOfRows; i++)
        {
            NSObject *row = [meTable rowControllerAtIndex:i];
            NSString *title = self.titles[i];
            NSLog(@"Row X Title: %@", title);
            NSString *description = self.descriptions[i];
            NSString *urlString = self.thumbnails[i];
            urlString = [urlString stringByReplacingOccurrencesOfString:@"http" withString:@"https"];
           
            NSURLSessionConfiguration *conf = [NSURLSessionConfiguration defaultSessionConfiguration];
            NSURLSession *session = [NSURLSession sessionWithConfiguration:conf];
            [[session dataTaskWithURL:[NSURL URLWithString:urlString]
                    completionHandler:^(NSData *data,
                                        NSURLResponse *response,
                                        NSError *error) {
                        MyRowController *ordinaryRow = (MyRowController *) row;
                        [ordinaryRow.titleLabel setText:title];
                        [ordinaryRow.descriptionLabel setText:description];
                        [ordinaryRow.videoThumbnail setImage:[UIImage imageWithData:data]];
                       
                    }] resume];
       
        }
WKInterfaceImage from URL in Watch OS 2
 
 
Q