Hello everyone!
I have a WKWebView in my swiftui app and would like to enable to "pop root view when selected tab is tapped again" feature, but I have been unable to figure out how to implement this.
Here's the basic code:
class TabIdentifierModel:ObservableObject {
@Published var tabSelection:TabIdentifier {
willSet {
if newValue == tabSelection {
NotificationCenter.default.post(name: .popRootView, object: nil, userInfo: ["tab": newValue.rawValue])
}
}
}
init() {
tabSelection = .home
}
}
struct ContentView: View {
@AppStorage(AppStorageKeys.enableShorts) var enableShorts = true
@StateObject var storeVM = StoreVM()
@StateObject var downloadURLManager = DownloadURLManager.shared
@State var downloadViewIsOpen = false
// ......
@State var tabSelection = TabIdentifierModel()
var body: some View {
TabView(selection: $tabSelection.tabSelection) {
// ......
WebViewWrapper(url: $libraryTabURL)
.tabItem {
Label {
Text("Library")
} icon: {
Image(systemName: "folder.fill")
}
}.tag(TabIdentifier.library)
// ......
}
.environmentObject(tabSelection)
}
}
Tapping on the tab again doesn't seem to set the value again thus the NotificationCenter.default.post is not sent and the web view is not reloaded.
Help would be much appreciated! Thanks in advance!
Post
Replies
Boosts
Views
Activity
I am working on a native visionOS app.
I'm running into a problem when websites request fullscreen.
Sample code:
import SwiftUI
import WebKit
struct WebViewWrapper: UIViewRepresentable {
let url: String
func makeUIView(context: Context) -> WKWebView {
let webConfiguration = WKWebViewConfiguration()
webConfiguration.allowsInlineMediaPlayback = false
webConfiguration.mediaPlaybackRequiresUserAction = false
let webView = WKWebView(frame: .zero, configuration: webConfiguration)
webView.navigationDelegate = context.coordinator
webView.uiDelegate = context.coordinator
return webView
}
func updateUIView(_ uiView: WKWebView, context: Context) {
if let url = URL(string: url) {
let request = URLRequest(url: url)
uiView.load(request)
}
}
func makeCoordinator() -> Coordinator {
Coordinator(self)
}
class Coordinator: NSObject, WKNavigationDelegate, WKUIDelegate {
var parent: WebViewWrapper
init(_ parent: WebViewWrapper) {
self.parent = parent
}
func webView(_ webView: WKWebView, createWebViewWith configuration: WKWebViewConfiguration, for navigationAction: WKNavigationAction, windowFeatures: WKWindowFeatures) -> WKWebView? {
if navigationAction.targetFrame == nil {
webView.load(navigationAction.request)
}
return nil
}
}
}
struct ContentView: View {
var body: some View {
GeometryReader { geo in
VStack {
WebViewWrapper(url: "https://youtube.com")
.clipShape(RoundedRectangle(cornerSize: CGSize(width: geo.size.width/40, height: geo.size.height/40)))
.padding()
}
}
}
}
#Preview(windowStyle: .automatic) {
ContentView()
}
The error I'm getting: -[AVPlayerViewController enterFullScreenAnimated:completionHandler:] failed with error Invalid call of -[AVPlayerViewController (0x153087600) _transitionToFullScreenAnimated:interactive:completionHandler:]!
Any help would be greatly appreciated.
Hello,
I am working on a Mac app and I would like to be able to get notifications (NotificationCenter.default.addObserver.add... MPMusicPlayerControllerPlaybackStateDidChange) when media starts to play in other apps. Is this possible on Mac?
Thanks in advance!
Hi all,
I've seen a lot of documentation of SharePlay for AVFoundation, but I was wondering if it is possible to integrate SharePlay with MusicKit. Can it be done?
Hello everyone!
I am wondering how to make one of those cards that sticks to the bottom of my view in SwiftUI. An example of this is in Apple Music where the now playing item and controls stay at the bottom throughout the navigation movements.
Thank you and hope you having a great WWDC!
I have the same question as here.
https://developer.apple.com/forums/thread/110001
Thanks!
Hello,
I've recently added a widget extension to my app. But I've been having trouble sharing data between the two using UserDefaults.
Code
Shared Code
var AppGroup: String {
"group.someGroup"
}
Main App
UserDefaults(suiteName: AppGroup)!.setValue(data, forKey: "TimeLine")
// sets the value for main app.
Widget Extension
let data = UserDefaults(suiteName: AppGroup)!.data(forKey: "TimeLine")
// can't get the value in extension.
App groups are turned on in both targets, but the data is not shared.
Any ideas as to why this is happening would be appreciated.
Thank you.
Hello,
I'm wondering if the content of the MediaPlayer framework, songs and music videos, counts as third-party content for the app review process.
Thank you!
I have a 3d camera app that I'm working on and I am wondering how to put the two videos side-by-side to save to Photos as one video using this delegate method: func fileOutput(_ output: AVCaptureFileOutput,
didFinishRecordingTo outputFileURL: URL,
from connections: [AVCaptureConnection],
error: Error?) {
Thank You!