How to show web content as QR code in SwiftUI in watchOS

Issue #449 WatchKit does not have Web component, despite the fact that we can view web content https://www.imore.com/how-view-web-pages-apple-watch-watchos-5 A workaround is to show url as QR code import SwiftUI struct QRCodeView: View { let title: String let url: URL var body: some View { GeometryReader { geometry in VStack { self.makeImage(size: geometry.size) .padding(.top, 10) Text("Scan to open") .font(.system(.footnote)) }.navigationBarTitle(self.title) } } private func makeImage(size: CGSize) -> some View { let value = size....

October 8, 2019 · 1 min · 86 words · Khoa

How to load remote image in SwiftUI

Issue #448 Use ObservableObject and onReceive to receive event. URLSession.dataTask reports in background queue, so need to .receive(on: RunLoop.main) to receive events on main queue. For better dependency injection, need to use ImageLoader from Environment There should be a way to propagate event from Publisher to another Publisher, for now we use sink ImageLoader.swift import Combine import WatchKit class ImageLoader: ObservableObject { private var cancellable: AnyCancellable? let objectWillChange = PassthroughSubject<UIImage?, Never>() func load(url: URL) { self....

October 8, 2019 · 1 min · 184 words · Khoa

How to do navigation in SwiftUI in watchOS

Issue #447 NavigationView is not available on WatchKit, but we can just use NavigationLink List(services.map({ AnyService($0) })) { anyService in NavigationLink(destination: ItemsView(service: anyService.service) .navigationBarTitle(anyService.service.name) .onDisappear(perform: { anyService.service.requestCancellable?.cancel() }) ) { HStack { Image(anyService.service.name) .resizable() .frame(width: 30, height: 30, alignment: .leading) Text(anyService.service.name) } } } Adding NavigationLink to a View adds a round shadow cropping effect, which is usually not want we want. But we shouldn’t wrap Button as Button handles its own touch event, plus it has double shadow effect....

October 8, 2019 · 1 min · 110 words · Khoa

How to use protocol in List in SwiftUI

Issue #446 Suppose we have Service protocol, and want to use in List protocol Service { var name: String { get } } struct MainView: View { let services = [ Car() Plane() ] var body: some View { List(services) { service in HStack { Image(service.name) Text(service.name) } } } } This is not possible because item in List needs to conform to Identifiable Protocol type ‘Service’ cannot conform to ‘Identifiable’ because only concrete types can conform to protocols...

October 7, 2019 · 2 min · 231 words · Khoa