Skip to main content

The challenge I was facing, I had written a script to scan barcodes and use Google book API to view the contents.  However, a snippet of the JSON response

{
  
  "contentVersion": "0.2.0.0.preview.0",
  "panelizationSummary": {
    "containsEpubBubbles": false,
    "containsImageBubbles": false
  },
  "imageLinks": {
    "smallThumbnail": "http://books.google.com/books/content?id=VktTDwAAQBAJ&printsec=frontcover&img=1&zoom=5&source=gbs_api",
    "thumbnail": "http://books.google.com/books/content?id=VktTDwAAQBAJ&printsec=frontcover&img=1&zoom=1&source=gbs_api"
  },
  "language": "en",
  
}

was showing the path without SSL.  How to alter?

 

URLComponents

Basic URL Operations

Breaking down a URL with SwiftUI URLComponents.  With basic operations to create and work with links. For example, you can convert a String value into a URL as follows:

let url = URL(string: "http://books.google.com/books/content?id=VktTDwAAQBAJ&printsec=frontcover&img=1&zoom=1&source=gbs_api")!

This default initializer has a negative where it's forcing an unwrap that we'll have to copy over throughout our codebase.  Push it through an extension helps us to prevent this by using a custom initializer:

extension URL {
    init(_ string: StaticString) {
        self.init(string: "\(string)")!
    }
}
var unwrappedURL = URL("http://books.google.com/books/content?id=VktTDwAAQBAJ&printsec=frontcover&img=1&zoom=1&source=gbs_api")

Looking better and allows us to keep our implementation code clean from unwraps!

 

Converting a URL into a String

Using the absoluteString property a URL can be converted to a string:

print(unwrappedURL.absoluteString)

 

URL Components

A link is basically built up out of several components.  Lets's step through these components:

let imageURL = URL(string: "http://books.google.com/books/content?id=VktTDwAAQBAJ&printsec=frontcover&img=1&zoom=1&source=gbs_api")!
print(imageURL.host!) // Prints: books.google.com
print(imageURL.scheme!) // Prints: https

 

Extending the URL components

Continuing on from the example above

print(imageURL.path) // Prints: /books/content
print(imageURL.pathComponents) // Prints: ["/", "books", "content"]
print(imageURL.lastPathComponent) // Print: content
print(imageURL.query!) // Prints: id=VktTDwAAQBAJ&printsec=frontcover&img=1&zoom=1&source=gbs_api

 

Putting it together

Using the information above

// Check if the URL scheme is SSL or not
func checkHTTP(url: URL) {
    // build the new URL
    var components = URLComponents()
    components.scheme = "https"
    components.host = url.host
    components.path = url.path
    components.query = url.query
    
    self.url = URL(string: components.string ?? "image")!
}

 

 

Related articles

Andrew Fletcher19 Jul 2022
Xcode NullInjectorError
In Xcode and executing a run command, the response error was: { "name": "NullInjectorError", "ngTempTokenPath": null, "ngTokenPath": ["Ee", "fe", "Ne", "Ne", "Ne"] } This was a result of me attempting to incorrectly import the AppComponent from inside a service. import {...