Angela Yu Course Part 15 (lessons 145 to 153)

So yes we're back! It makes sense at this point to crack on with Angela's course. I think one of the reasons why I wanted to do the challenges from other courses was to protect myself from failing to understand new content. I need to get past that, which is why I'm going back to Angela's course! Last time with her course, I did some stuff which I can't really remember...so I'm going to crack on but may well need to go back over some stuff. Here we go!

Start Time - 11:38

Location Manager

So it's the workspace file I need - the one which has had 'pods' added to it. 

    let WEATHER_URL = "http://api.openweathermap.org/data/2.5/weather"
    let APP_ID = "e72ca729af228beabd5d20e3b7749713"

Here are the two websites which I am using. The App_ID is our own App ID that is required. So the guys at open weather map need to be able to track who is using their API...

API - Application Programming Interface

So all of that data I can basically tap into! Cool! 

Right I had signed up to this before. I'm pausing Angela's video so that I can get my own APP ID...

CoreLocation library - just imported this. This basically has a load of useful and relevant classes and methods. 

We've now created this object:

   let locationManager = CLLocationManager()

Delegates. These basically makes this easier - lots of inbuilt code. So no need to reinvent the wheel when we can use the core library!

Delegates/protocols - going to explore in more detail. For now, all we are doing is setting the weather  view controller as the delegate of the location manager, so that the location manager can deal with the all of the location related functionality. OK, complex but fair enough. 

So you have to choose this and make it explicit. 

 locationManager.delegate = self
 locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters

This is when an app requests you for authorisation etc. This is better than ALWAYS - people don't want it to be so!

 locationManager.requestWhenInUseAuthorization()

Location Permissions

So two items added to the ' p list' - both to do with privacy and locations. So using the p list is one way of changing this notifications part...another way of doing it is with the XML - the latter is much ore complex and more error prone. So stick to p list unless you have to use the XML. 

OK so some source code from the gitHub LondonAppBrewery was needed (can't paste). 

Running app...

*I'm not going to stop this until the Clima app is complete!

Tapping into GPS

 locationManager.startUpdatingLocation()

So this is needed. 

  func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        
    }
    
This bit is needed for when location services are accessed. 

Cool, so I've put in lat and long, then selected London as my location then it's showed these lat and long on the output. That all seems OK but still not at the point of the app showing anything.

*Paused at 12:39

*Continued at 16:01 - lots of things going on today!

Delegation

Before I start this video, I think I know what this is. If you have a class that conforms to a protocol, what takes priority? I think it's to do with that!

So we are using CoreLocation - passing data from the location manager to our own view controller is the problem. 

CoreLocation becomes the delegate for the location manager.

Still not sure what Delegation is. Let's just find out more. 

OK having read up on several things, including this link from Bob:


The best example is the one with the Mum and the baby. The Mom adopts the 'cook' protocol. The baby can't cook, so it is an optional. Then the Mom is declared as the delegate to cook for the baby. That makes more sense in my head now!

Dictionaries

I'm good with these. I know you can mix at match the key/pair as long as it is consistent. 

What are APIs?

Application Programme Interfaces. It's basically a contract with rules that must be followed to get the data from the website server. 

So for our Clima app, we have to provide the latitude, longitude and the app ID as part of the contract. In return we get the weather data. Neat!

APIs are not the only way of getting this. We can look on a website and pick out other info. E.g. on the weather app website, we can pick out the rainfall in London. However, if the website updates, then it would affect the app as something has changed. 

Here is a diagram!










SO on the website it will provide the API details - the API call, with what parameters are needed. In other words, the inputs that are needed (lat, lon). This is really exciting that all these websites have available APIs! I could make my own Bitcoin tracker (Haha, that is coming up from Angela anyway!). 

Alamofire

A HTTP request is needed...

So the Alamofire comes in, first off by importing this as a module. Then on the request bit.

JSON - Javascript object notation - this links in. It's a commonly accepted way of structuring large pieces of data. 

So this is the code provided by Alamo:

Alamofire.request(url, method: .get, parameters: parameters).responseJSON {
            response in
            if response.result .isSuccess {
                
            }
            else {
                
            }
        }
        
    }

Angela has typed it out...not sure if she had to. Anyway....She breaks down the syntax of each of this. The request method opens up all the other parameters. The url is the one I've already got as a constant, the .get is a choice of HTTP options - there are others, the parameters are the ones that I've already got coded - in the dictionary we created (parameters). Alamofire then makes a request with the open weather app. When the response comes back, we need to check if that is successful or not. 

So at this point, we are using a few inputs our url and the parameters from the location manager. We then get a response back from the server. If not successful, then we print to the console that there is a problem and have this on the text label too. 

Networking and REST

Networking - the communication between different computers on a network. 

E.g. when typing in www.google.com, you make the request and the Google servers then return HTTP and CSS files. 

When doing this there are different methods you can use. Here are the three main ones:

Get - fetching data
Post - when you post data and add data
Delete - a request to delete some data in the server

There are lots more!

Stopped at 17:02 (1 hour and 1 minute more; 2 hours 2 minutes total today)

I'm actually stopping there - plenty more lessons to go until the app is complete (8 more lessons and least an hour!) so that's something for Sunday/Monday. I've learned some useful things, however technical some of the information is. Angela has a great way of explaining the concepts and it's a whole new world opened up to think I could get API information from different websites! Just doing a quick check - YouTube, Google, Coinbase, even an F1 one (Ergast), loads of iMDB ones...basically lots out there! Once I've made this one, I MUST try to create something that uses a different API. Rather than just moving on to the next project! I'm not giving the time to consolidate at the moment! 

Comments

Popular posts from this blog

*Xcode Project Entry 2* F1 Quiz - part 1

Angela Yu Course Part 10 (up to lesson 112)

Angela Yu Xcode 12 Course - Part 7 (lectures 74 to 79)