Demo for including third party libraries in IOS

1. Install cocoapods, these two commands are only needed if cocoapods are not yet installed.

sudo gem install cocoapods
pod setup --verbose

2. cd into your ios project root directory and run this command to initialize the cocoapods.

pod init

3. Open the PodFile created by the above init command.

open -a Xcode Podfile

4. Replace the content in the PodFile with the following. This includes the libraries for this demo, SDWebImage for image downloading and Alamofire for network requests.

platform :ios, '9.0'

target 'ios_include_library_demo' do
    use_frameworks!
    pod 'SDWebImage', '~> 4.0'
    pod 'Alamofire', '4.4.0'
end

5. Run this command to install the libraries specified in the PodFile.

pod install

6. Since this demo project is named ios_include_library_demo, the above pod install command will install the libraries and create a new project file, ios_include_library_demo.xcworkspace. this file need to be used to open the project from now on. Let’s quit xcode and open this project again from this file.

7. Add an UIImageView and an UITextView in the storyboard and add outlets for UIImageView and UITextView in the ViewController. Then add the code for image loading and api get request in the ViewController. The ViewController should look like this.

import UIKit
import SDWebImage // import the image library so it can be used in this class
import Alamofire  // import the network library so it can be used in this class

class ViewController: UIViewController {
    
    // The outlet that connects to the ImageView on the storyboard
    @IBOutlet weak var imageView: UIImageView!
    
    // The outlet that connects to the TextField on the storyboard
    @IBOutlet weak var textView: UITextView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // load the image from an image url
        imageView.sd_setImage(with: URL(string: "https://images.freeimages.com/images/large-previews/e71/frog-1371919.jpg"), placeholderImage: UIImage(named: "placeholder.png"))
        
        makeGetRequestWithAlamofire()
    }
    
    
    func makeGetRequestWithAlamofire() {
        Alamofire.request("https://api.github.com/users/google")
            .responseJSON { response in
                guard response.result.error == nil else {
                    print("error calling GET")
                    print(response.result.error!)
                    return
                }
                
                guard let json = response.result.value as? [String: Any] else {
                    print("Expected JSON data, but non JSON data returned from API")
                    if let error = response.result.error {
                        print("Error: \(error)")
                    }
                    return
                }
                
                print(json)
                
                guard let name = json["name"] as? String else {
                    print("Could not get name from JSON")
                    return
                }
                
                guard let email = json["email"] as? String else {
                    print("Could not get email from JSON")
                    return
                }

                print("The name is: \(name), email is: \(email)")
                self.textView.text = name + "\r" + email //"\(name)\n\(email)"
        }
    }
    
}

Complete example in Github

Search within Codexpedia

Custom Search

Search the entire web

Custom Search