Houman
Houman

Reputation: 66320

Why does NSData remain nil after loading from a valid URL?

@interface ViewController : UIViewController
{   
    __strong IBOutlet UIImageView *testImageView;
}

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    NSData *imageData = [[NSData alloc] initWithContentsOfURL: [NSURL URLWithString: @"http://news.bbcimg.co.uk/media/images/79790000/jpg/_79790768_hi024951574.jpg"]];
    // imageData => nil ???
    testImageView = [[UIImageView alloc] initWithImage:[UIImage imageWithData: imageData]];

}

@end

I don't understand why imageData remains nil, even though the url exists. Is it because I am doing this from the simulator? Thanks

UPDATE:

enter image description here Not entirely sure why I am getting a NSFileReadUnknownError = 256

Upvotes: 3

Views: 346

Answers (2)

Paul.V
Paul.V

Reputation: 416

When you're dealing with the web services you need permissions for the transporation of your arbitrary load. JUST follow the steps below: 1. GO to p.list>rightClick>OpenAs>SourceCode 2. paste the below lines into our p.list source code: (Find in attached image)enter image description here 3. Run your program & its done.

Upvotes: 0

siejkowski
siejkowski

Reputation: 1681

Trying the code you provided, I've got no problem with showing the image.

Looks like the issue lays in your local network environment.

I know it might sound dumb, but I encourage you to reset and restart iOS simulator. I've experienced a bug that prevents simulator from properly connecting after sleeping and waking up Mac. Xcode 6.1, Yosemite.

Also, please check if you're connecting through proxy or if your /etc/hosts file is clear regarding bbcimg.co.uk domain. Just the usual, sanity check.

Upvotes: 2

Related Questions