반응형

#import <Foundation/Foundation.h>


int main(int argc, const char * argv[])

{


    @autoreleasepool {

        

        NSURL *url = [NSURL URLWithString:@"http://www.google.co.kr/intl/ko_ALL/images/logos/images_logo_lg.gif"];

        NSURLRequest *request = [NSURLRequest requestWithURL:url];

        NSError *error = nil;

        NSData *data = [NSURLConnection sendSynchronousRequest:request

                                             returningResponse:NULL error:&error];

        if(!data)

        {

            NSLog(@"fetch failed: %@", [error localizedDescription]);

            

            return 1;

        }

        

        NSLog(@"The file is %lu bytes", [data length]);

        

        BOOL written = [data writeToFile:@"/Users/Mac/Pictures/google.png" options:NSDataWritingAtomic error:&error];

        

        if(!written)

        {

            NSLog(@"write failed: %@", [error localizedDescription]);

            

            return 1;

        }

        

        NSLog(@"Success!");


// 파일에서 읽기.

     NSData *readData = [NSData dataWithContentsOfFile:@"/Users/Mac/Pictures/google.png"];

     NSLog(@"The file read from the disk has %lu bytes", [readData length]);

        

    }

    return 0;

}



반응형
Posted by 컴스터
,