Open menu with table of contents iOS Development - Framework Basics 4
Logo of Stuttgart Media University for light theme Logo of Stuttgart Media University for dark theme
Mobile Application Development 2

iOS Development - Framework Basics 4

Stuttgart Media University

1 Agenda

  • WebKit
    • WKWebView
  • MediaPlayer
    • MPMediaQuery
  • Assignment Artist Explorer

2 WKWebView successor of UIWebView

  • WKWebView (part of WebKit) / UIWebView (part of UIKit) components allow to open HTML content in your app
    • Works similar to the Android WebView component
    • With new apps use WKWebView as UIWebView is deprecated since iOS 9.0
    • WKWebView is part of the WebKit framework, so you need to import WebKit in your class and also link to the WebKit.framework (in Build Phases).

50%

3 To load a Website in a WKWebView:

  1. Create a URL? object (since Swift 3.0, formerly NSURL), please be aware that this is an Optional, so you need to unwrap is (e.g. using guard):

guard let url = URL(string: "https://www.hdm-stuttgart.de") else { return }

  1. Create a URLRequest object:

let urlRequest = URLRequest(url: url)

  1. Call load Request on WKWebView:
//WKWebView add to StoryBoard and linked to you code like here:
@IBOutlet weak var webView: WKWebView!

// Load the URL
webView.load(urlRequest)

4 App Transport Security

  • Since version 9 of iOS it is required for any app to use only encrypted connections when accessing a web service
  • Thus you should always use https://
  • If this is not possible you can allow arbitrary loads in the apps plist file as shown here:

center 70%

5 WKNavigationDelegate

  • WKWebView will load URLs asynchronously
  • In order to know, when the loading of a webpage started, finished and if a error occurred you can implement the WKNavigationDelegate protocol and register it with the WKWebView by e.g.: webView.navigationDelegate = self
  • Implement the following methods in your view:
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
    //Called when the navigation is complete.
}

func webView(_ webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!) {
    //Called when web content begins to load in a web view.
}

func webView(_ webView: WKWebView, didFail navigation: WKNavigation!, withError error: Error) {
    //Called when an error occurs during navigation.
}

6 Network Access Indicator UI

7 MPMediaQuery

  • The MPMediaQuery class is part of the MediaPlayer Framework and allows you to query your iPod Library on the device - similar to Androids MediaStore (Content Provider)
  • It is very simple to use, e.g. to retrieve all artists from your device:
let query = MPMediaQuery()

guard let items = query.items else {
    debugPrint("Error: query has no items object")
    return
}

if items.count == 0 {
    debugPrint("Error: no items found")
    return
}

for item in query.items! {
    if let artist = item.value(forProperty: MPMediaItemPropertyArtist)  {
        artists.append(artist as! String)
    }
}

8 MPMediaItem

  • A MediaItem represents a single piece of media (song, video) in the iPod Library
  • It has an ID and different properties that can be queried using the value(forProperty: ) method (or since Swift 3.0) directly accessed, e.g.:
if let artist = item.artist {
    print("Artist: \(artist)")
}
  • To use it, import MediaPlayer in you ViewController class
import MediaPlayer

9 Medialibrary request authorisation

  • Since iOS 9.3 it is required to request access to the MediaLibrary in order to read data. Do this by calling the requestAuthorization() method, e.g.
MPMediaLibrary.requestAuthorization { (status) in
    if status == .authorized {
        self.readMediaLibrary()
    } else {
        self.showError()
    }
}

10 Medialibrary Authorisation Status

  • To check the authorisation status use the authorizationStatus() method, e.g.:
var error: String
switch MPMediaLibrary.authorizationStatus() {
    case .restricted:
        error = "Media library access restricted by corporate or parental settings"
    case .denied:
        error = "Media library access denied by user"
    default:
        error = "Unkown error"
}

11 Medialibrary User Information

  • When access to the MediaLibrary is requested, the user will be prompted by iOS with a message. It is required that the developer adds an information text to the Info.plist, that explains the user why it should authorise access either your app could be rejected by Apple.
  • Add the key NSAppleMusicUsageDescription to your info.plist

center 70%

  • Unfortunately it is not possible to access the Media Library in the simulator, it is not supported. You need a real device for this

12 Artist Explorer Assignment

13 References