Download image file to your hard drive using Cocoa

I am creating a program, and I am confident using Objective-C, but I do not know how to programmatically download a file from the Internet and copy it to my hard drive. I started with:

NSString url = @"http://spiritofpolo.com/images/logo.png"; NSData* data = [NSData dataWithContentsOfURL:[NSURL URLWithString:url]]; 

But then I don’t know what to do with the data ... it sucks, no;) Can anyone help?

+4
source share
2 answers

Although this approach, with the last step provided by fbrereto , will work, it does not handle the failure gracefully (indeed, it does not handle any failure at all) and will block your application for the duration of the download.

Use NSURLDownload instead . This requires more code, but broken network connections, boot files, and inaccessible destination paths will not necessarily (necessarily) silently disturb your application.

+5
source

You are close; the last thing you need is a call -[NSData writeToFile:atomically:] .

+9
source

Source: https://habr.com/ru/post/1311105/


All Articles