Post

Replies

Boosts

Views

Activity

Getting error "Cannot preview in this file"
Hello, I'm new to SwiftUI and right now I'm trying to create one simple Application by using @State, @ObservedObject, and @Environment. For some reason, I got error "Cannot preview in this file" after I tried to add Environment object. The canvas is not working, but I can actually run the application in a simulator and actual device without any problem. Here is how I added my environment object. SceneDelegate:func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) { // Use this method to optionally configure and attach the UIWindow `window` to the provided UIWindowScene `scene`. // If using a storyboard, the `window` property will automatically be initialized and attached to the scene. // This delegate does not imply the connecting scene or session are new (see `application:configurationForConnectingSceneSession` instead). let channelData = ChannelData() // Create the SwiftUI view that provides the window contents. let contentView = ContentView().environmentObject(channelData) // Use a UIHostingController as window root view controller. if let windowScene = scene as? UIWindowScene { let window = UIWindow(windowScene: windowScene) window.rootViewController = UIHostingController(rootView: contentView) self.window = window window.makeKeyAndVisible( } }ChannelData class:import Combine final class ChannelData: ObservableObject{ @Published var channelName = "YouTube Channel" }ContentView:import SwiftUI struct ContentView: View { @State private var showingSecondVC = false @ObservedObject var videoIdea = VideoIdea() @EnvironmentObject var channelData: ChannelData var body: some View { NavigationView{ VStack(alignment: .leading){ Text(videoIdea.title) .font(.title) Text(videoIdea.contentIdea) .font(.subheadline) Divider() NavigationLink(destination: ChannelView()){ Text("Add Channel") } Button(action: { self.showingSecondVC.toggle() }){ Text("Add New Idea") }.sheet(isPresented: $showingSecondVC){ SecondView(videoTitle: self.$videoIdea.title, videoContent: self.$videoIdea.contentIdea).environmentObject(self.channelData) } Spacer() }.padding() .navigationBarTitle(channelData.channelName) } } } struct ContentView_Previews: PreviewProvider { static var previews: some View { ContentView() } }SecondView:import SwiftUI struct SecondView: View { @Environment(\.presentationMode) var presentationMode: Binding<PresentationMode> @Binding var videoTitle: String @Binding var videoContent: String @EnvironmentObject var channelData:ChannelData var body: some View { NavigationView { VStack(alignment: .leading){ TextField("Video title", text: $videoTitle) TextField("Video Content", text: $videoContent) Divider() Button(action: { self.presentationMode.wrappedValue.dismiss() }){ Text("Dismiss this VC") } Spacer() }.padding() .navigationBarTitle(channelData.channelName) } } }ChannelView:import SwiftUI struct ChannelView: View { @Environment(\.presentationMode) var presentationMode: Binding<PresentationMode> @State private var channelName = "My Channel" @EnvironmentObject var channelData: ChannelData var body: some View { NavigationView{ VStack(alignment: .leading){ TextField("Channel Name", text: $channelName) Divider() Button(action: { self.presentationMode.wrappedValue.dismiss() }){ Text("Dismiss this VS") } Spacer() }.padding() .navigationBarTitle(channelData.channelName) } } }
0
0
700
Jan ’20
Some redirect url links are not working when loaded by WKWebView
I'm trying to display one embedded content by using the WKWebView and I can not click some of these links. Is there anything I did wrong?Here is the Link which I get the embedded content from:https://publish.twitter.com Here is the embedded content:<a class="twitter-timeline" href="https://twitter.com/Google?ref_src=twsrc%5Etfw">Tweets by Google</a><script async src="https://platform.twitter.com/widgets.js" charset="utf-8"></script>Here is my code:import UIKit import WebKit class ViewController: UIViewController { private var mWebView: WKWebView! let embeddedTwittContent = "<a class='twitter-timeline' href='https://twitter.com/Google?ref_src=twsrc%5Etfw'>Tweets by Google</a>" // let embeddedTwittContent = "<a class=\"twitter-timeline\" href=\"https://twitter.com/Smaforetagarna?ref_src=twsrc%5Etfw\">Tweets by Smaforetagarna</a>" let scriptValue = "<script async src=\"https://platform.twitter.com/widgets.js\" charset=\"utf-8\"></script>" let redirectLink = "https://twitter.com/Smaforetagarna/status/" override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view. let preferences = WKPreferences() preferences.javaScriptEnabled = true let configuration = WKWebViewConfiguration() configuration.websiteDataStore = WKWebsiteDataStore.nonPersistent() configuration.preferences = preferences self.mWebView = WKWebView(frame: CGRect.zero, configuration: configuration) self.mWebView.translatesAutoresizingMaskIntoConstraints = false self.mWebView.allowsLinkPreview = true self.mWebView.allowsBackForwardNavigationGestures = true self.mWebView.navigationDelegate = self self.view.addSubview(self.mWebView) // Add Constraints self.mWebView.topAnchor.constraint(equalTo: self.view.topAnchor, constant: 100).isActive = true self.mWebView.bottomAnchor.constraint(equalTo: self.view.bottomAnchor, constant: 0).isActive = true self.mWebView.rightAnchor.constraint(equalTo: self.view.rightAnchor, constant: 0).isActive = true self.mWebView.leftAnchor.constraint(equalTo: self.view.leftAnchor, constant: 0).isActive = true loadTweetNews() } func loadTweetNews(){ let htmlHeader = "<!DOCTYPE html> <html><meta name=\'viewport\' content=\'initial-scale=1.0\'/> <head> \(scriptValue) </head> <body>" let htmlFooter = "</body> </html>" let orderHtml = htmlHeader + embeddedTwittContent + htmlFooter let url: URL = URL(string: "https:")! self.mWebView.loadHTMLString(orderHtml, baseURL: url) } } extension ViewController: WKNavigationDelegate{ func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) { print("page finished load") } func webView(_ webView: WKWebView, didReceiveServerRedirectForProvisionalNavigation navigation: WKNavigation!) { print("didReceiveServerRedirectForProvisionalNavigation: \(navigation.debugDescription)") } func webView(_ webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!) { print("didStartProvisionalNavigation") } func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) { if navigationAction.navigationType == .linkActivated { if let url = navigationAction.request.url, UIApplication.shared.canOpenURL(url) { UIApplication.shared.open(url) print(url) decisionHandler(.cancel) } else { decisionHandler(.allow) } } else { decisionHandler(.allow) } }
2
0
15k
May ’19