NEW: My new book Pro SwiftUI is out now – level up your SwiftUI skills today! >>

How to Implement SwiftUI Media Controls into the Touch Bar

Forums > macOS

Hi,

I'm pretty new to SwiftUI and Swift and I'm trying to create a macOS app that uses WKWebView to display Netflix. I want it to be similar to the Clicker for Netflix app. So far, I’ve been successful in displaying Netflix. Now I want to add media controls to play or pause the Netflix movie from the Touch Bar in addition to skipping to the next episode of a TV show. I'm not sure how to start implementing these features. I’ve looked at using AVKit but haven’t gotten much far. I would really appreciate it if you could help me. The following is my ContentView.swift file:

import SwiftUI
import WebKit

struct ContentView: View {
    var body: some View {
            Webview(url: "https://www.netflix.com")
                .scaledToFill()
        }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

And this is the WebView.swift file:

import Foundation
import SwiftUI
import WebKit

struct Webview: NSViewRepresentable {

    var url: String

    func makeNSView(context: Context) -> WKWebView {
        guard let url = URL(string: self.url) else {
            return WKWebView()
        }

        let request = URLRequest(url: url)
        let wkWebview = WKWebView()
        wkWebview.load(request)
        return wkWebview
    }

    func updateNSView(_ nsView: WKWebView, context: Context) {

    }
}

Thanks,

1      

Hacking with Swift is sponsored by MadMachine

SPONSORED Want to explore your Swift skill outside of the Apple world? Join the MadMachine community and start to program microcontrollers in Swift.

Get it now

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.