Giter Site home page Giter Site logo

uikit-in-swiftui's Introduction

UIKit in SwiftUI

This is a simple project that shows how UIKit can be integrated into SwiftUI.
You can find my full article about the integration of both frameworks here.

giffu

Table of Contents

General Information

I started this project in order to understand how SwiftUI and UIKit can be integrated together to get the best out of both of them. In this case, I decided to use a WKWebView, which is part of the WebKit framework and is used to incorporate web content into an app. This is actually just one of two projects that I worked on, you can find the "SwiftUI in UIKit" part here.

Technologies Usedimage

  • macOS - 12.1
  • Xcode - 13.3

Create a WebView

Create a SwiftUIView that will need to conform to the UIVIewRepresentable protocol.

struct WebView: UIViewRepresentable { }

Add The typealias (the kind of View we are creating), in this case a WKWebView.

typealias UIViewType = WKWebView

And then the make and the update methods. The first one is called only once and creates the view, while the second one updates it.

func makeUIView(context: Context) -> WKWebView { }
func updateUIView(_ uiView: WKWebView, context: Context) { }

We will also store the WKWebView into a constant and create a variable of type URL, that we'll use to assign any url we want to the WebView.

let webView = WKWebView()
var url : URL

In order to create the View, we just have to return the view from the makeUIView() function.

return webView

Instead for the updateUIView() we need to load the url that we'll assign to it when we'll call the WebView.

let request = URLRequest(url: url)
webView.load(request)

Usage

In order to use this view, you just have to import the file in your project and then call it where you need it. Now you just have to pass the url of your desired web page as parameter.

WebView(url: URL(string: "https://tenor.com/bQ6Vf.gif")!)

Contact

Created by Barbara Beatrice

uikit-in-swiftui's People

Contributors

barbarabeatrice avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.