#import <Foundation/Foundation.h>
int main(int argc, const char * argv[]) {
@autoreleasepool {
// insert code here...
//https://www.baidu.com/img/bd_logo1.png
// NSURL *url = [NSURL URLWithString:@"https://www.baidu.com/img/bd_logo1.png"];
//http://www.xinhuanet.com/photo/2018-03/13/1122528717_15209051820841n.jpg
NSURL *url = [NSURL URLWithString:@"http://www.xinhuanet.com/photo/2018-03/13/1122528717_15209051820841n.jpg"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
NSError *err;
// 'sendSynchronousRequest:returningResponse:error:' is deprecated: first deprecated in macOS 10.11 - Use [NSURLSession dataTaskWithRequest:completionHandler:] (see NSURLSession.h
NSData *data = [NSURLConnection sendSynchronousRequest:request
returningResponse:NULL
error:&err];
if(!data) {
NSLog(@"Fetch failed : %@", [err localizedDescription]);
}
NSLog(@"The file is %lu bytes", (unsigned long)[data length]);
BOOL written = [data writeToFile:@"/Users/demi/Ming/test/renda.jpg"
options:NSDataWritingAtomic
error:&err];
if(!written) {
NSLog(@"Write failed : %@", [err localizedDescription]);
}
NSLog(@"Success");
NSData *readData = [NSData dataWithContentsOfFile:@"/Users/xxx/xxxx/test/renda.jpg"];
NSLog(@"The file size is %lu bytes", (unsigned long)[readData length]);
//speical directories
NSArray *desktops = NSSearchPathForDirectoriesInDomains(NSDesktopDirectory, NSUserDomainMask, YES);
NSString *desktoppath = desktops[0];
NSLog(@"desktops are: %@", desktops);
NSLog(@"desktop path is %@", desktoppath);
NSArray *mydirs = NSSearchPathForDirectoriesInDomains(NSUserDirectory, NSLocalDomainMask, YES);
NSString *mydir = mydirs[0];
NSString *savedir = [mydir stringByAppendingString:@"/test/folder"];
NSLog(@"My dictory is %@", savedir);
}
return 0;
}
2018-03-13 21:50:30.661413+0800 TOCSaveNSDataa[65809:4002612] The file is 137361 bytes
2018-03-13 21:50:30.671947+0800 TOCSaveNSDataa[65809:4002612] Success
2018-03-13 21:50:30.672301+0800 TOCSaveNSDataa[65809:4002612] The file size is 137361 bytes
2018-03-13 21:50:30.672707+0800 TOCSaveNSDataa[65809:4002612] desktops are: (
"/Users/xxxx/Desktop"
)
2018-03-13 21:50:30.672879+0800 TOCSaveNSDataa[65809:4002612] desktop path is /Users/xxxx/Desktop
2018-03-13 21:50:30.673022+0800 TOCSaveNSDataa[65809:4002612] My dictory is /Users/test/folder
Program ended with exit code: 0