NASA posts amazing images of the far out distance from the rest of the earth: http://apod.nasa.gov/apod/astropix.html. How do I just get one image to show up on my simulator as a newbie for iOS development?
YES! It is less than 15 lines of code.
api: mentioned often, and seems important. Sign up so I can have access to NASA’s api in order to get the image of the day! Go to this site https://api.nasa.gov/index.html#live_example and sign up. You’ll receive an email with a key to open the door of NASA’s images of the space! Maybe that is what api is about.
@implementation ViewController
-(void)viewDidLoad {
[super viewDidLoad];NSString * urlS = @”https://api.nasa.gov/planetary/apod?api_key= (PASTE YOUR KEYS THAT NASA SENT YOU AFTER ABOVE SIGN UP)"; // It looks similar like the below
//NSString * urlS = @"https://api.nasa.gov/planetary/apod?concept_tags=True&api_key=NNKOjkoul8n1CH18TWA9gwngW1s1SmjESPjNoUFo";
...
}
Try to open the link in web browser. You get something like this.
Wow! Try to copy and paste the fist line http://apod.nasa.gov/apod/image/1511/monumentvalley_pacholka_1080.jpg . You might get this! ✌︎
With the key I got from NASA’s email, I was able to get the link of the apod (picture of the day), and some info about that picture! Lets pack that data into objective C recognizable/friendly boxes and use them in the iOS land.
-(void)viewDidLoad {
[super viewDidLoad];
NSString * urlS = @"https://api.nasa.gov/planetary/apod?api_key= (☞☛your-NASA-api-key☞❉✲✺❋✮✣✢⦿)";NSURL * url = [NSURL URLWithString:urlS];
NSData * apiData = [NSData dataWithContentsOfURL:url];
NSDictionary * apiResult = [NSJSONSerialization JSONObjectWithData:apiData options:0 error:nil];
...
}
Put break point at line NSURL * url = [ …. ] NSURL turns a NSString of a URL weblink to something like an informational set of data. Step to next line NSData * apiData =[ … ] apiData looks like this:
NOT really readable. Step to the next line? NSDictionary * apiResult = […]
♡ ! A dictionary of 5 elements. [0]explanation, [1]title, [2]url, [3]media type, [4]concepts. ︎☞get the title from the dictionary apiResult.
NSString * imageURLString = [apiResult objectForKey:@”url”];
NSString * title = [apiResult objectForKey:@"title"];
self.apodTitle.text = title;
☛get the image from the dictionary apiResult. However, The image URL string just as the big NASA link https://api.nasa.gov/planetary/apo…., that we have to make it go through NSData unpacking process for Xcode to get the full information about it. It is not really a string after all.
NSURL * imageURL =[NSURL URLWithString:imageURLString];
NSData *imageData2 = [NSData dataWithContentsOfURL:imageURL];
UIImage * testImage = [UIImage imageWithData:imageData2];
self.test.image = testImage;
Things I learned:
☛api is hard to understand fully, but it has to do with this motion of getting the data from NASA open api services by a quick registration, delivering it to objective C! Like visiting friend’s place to pick up something but let them know first.
☛Different ways to unpack the api for Xcode to get the full content of this NASA image-of- the-day api, one of them is
[NSJSONSerialization JSONObjectWithData:apiData options:0 error:nil];
It returns a NSDictionary with readable information about what exactly is packed in NSData.
.🎈✨. Github example
FYI: you might see the following error : App Transport Security has blocked a cleartext HTTP (http://) resource load since it is insecure. Temporary exceptions can be configured via your app’s Info.plist file. It has to do with iOS 9 update. Google has an explanation.
http://googleadsdeveloper.blogspot.com/2015/08/handling-app-transport-security-in-ios-9.html
.