is it possible to load a link such as
Link("Apple", destination: URL(string: "https://www.apple.com")!)
from a tab bar item? if so how?
struct MainView: View {
@State private var selectedTab = "Three"
var body: some View {
TabView(selection: $selectedTab) {
LiveView()
.tabItem {
Label("LIVE", systemImage: "camera.metering.center.weighted")
.font(Font.title.weight(.ultraLight))
}
.tag("One")
if I add an link or text I get errors. I appreciate any help!
Selecting any option will automatically load the page
Post
Replies
Boosts
Views
Activity
I'm running on fumes but logically this view should be showing the share button on the bottom right, and the Title/Date/PlayButton in the middle? What am I doing wrong? It currently just shows the share button underneath
import SwiftUI
import AVKit
struct ContentView: View {
@State private var items = [Item]()
@State private var currentPage = 0
var body: some View {
TabView(selection: $currentPage) {
ForEach(items, id: \.id) { item in
VideoPlayerView(url: item.interactive)
.aspectRatio(contentMode: .fill)
.tag(item.id)
.onAppear {
let controller = AVPlayerViewController()
controller.player = AVPlayer(url: URL(string: item.interactive)!)
controller.showsPlaybackControls = false
controller.player?.play()
}
.overlay(
VStack() {
Spacer()
Text(item.name)
.font(.title)
Text(item.date)
.font(.caption)
Button(action: {
let player = AVPlayer(url: URL(string: item.videolink)!)
let playerController = AVPlayerViewController()
playerController.player = player
playerController.showsPlaybackControls = true
UIApplication.shared.windows.first?.rootViewController?.present(playerController, animated: true, completion: {
player.play()
})
}) {
HStack(spacing: 5) {
Text("PLAY")
Image(systemName: "play.circle")
}
.padding(.horizontal, 10)
.padding(.vertical, 5)
.background(Color.white)
.foregroundColor(.black)
.cornerRadius(20)
}
Button(action: {
let activityVC = UIActivityViewController(activityItems: [URL(string: item.sharelink)!], applicationActivities: nil)
UIApplication.shared.windows.first?.rootViewController?.present(activityVC, animated: true, completion: nil)
}) {
Image(systemName: "square.and.arrow.up.circle")
.foregroundColor(.black)
}
.padding(.bottom, 50)
}
)
}
}
.tabViewStyle(.page)
.onAppear(perform: loadData)
.gesture(DragGesture()
.onEnded { value in
let offset = value.translation.width / UIScreen.main.bounds.width
let newIndex = (CGFloat(currentPage) - offset).rounded()
currentPage = min(max(Int(newIndex), 0), items.count - 1)
}
)
}
func loadData() {
guard let url = URL(string: "jsonfile") else { return }
URLSession.shared.dataTask(with: url) { data, response, error in
guard let data = data else { return }
do {
let decoder = JSONDecoder()
let items = try decoder.decode([Item].self, from: data)
DispatchQueue.main.async {
self.items = items
}
} catch {
print(error.localizedDescription)
}
}.resume()
}
}
struct VideoPlayerView: UIViewRepresentable {
let url: String
func makeUIView(context: Context) -> AVPlayerView {
let view = AVPlayerView()
view.playerLayer.player = AVPlayer(url: URL(string: url)!)
view.playerLayer.videoGravity = .resizeAspectFill
view.playerLayer.backgroundColor = UIColor.black.cgColor
view.playerLayer.player?.play()
return view
}
func updateUIView(_ uiView: AVPlayerView, context: Context) {
// Nothing to update
}
}
class AVPlayerView: UIView {
override class var layerClass: AnyClass {
return AVPlayerLayer.self
}
var playerLayer: AVPlayerLayer {
return layer as! AVPlayerLayer
}
override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
let hitView = super.hitTest(point, with: event)
return hitView == self ? nil : hitView
}
}
struct Item: Codable, Identifiable {
let id: Int
let name: String
let interactive: String
let thumbnail: String
let date: String
let videolink: String
let sharelink: String
}
I cant get either a custom of basic color to not timeout Xcode
.listRowBackground( Capsule().fill(Color(white: 1, opacity(0.8))) .padding(.vertical, 2) .padding(.horizontal, 10) )
the file does not show a black fill but the app shows a black circle.
since it's to long https://gist.github.com/YutaTheTraveler/0b8c11faf997f2118d19bbd009d490ba.js
I'm using this documentation as my base https://developer.apple.com/tutorials/swiftui/building-lists-and-navigation
I'm failing to understand how I would simply adapt the app to load the json remotely
import Foundation
var landmarks: [Landmark] = load("landmarkData.json")
func load<T: Decodable>(_ filename: String) -> T {
let data: Data
guard let file = Bundle.main.url(forResource: filename, withExtension: nil)
else {
fatalError("Couldn't find \(filename) in main bundle.")
}
do {
data = try Data(contentsOf: file)
} catch {
fatalError("Couldn't load \(filename) from main
snippet from Model data.swift
the only example I've found is included below, however it's using an entirely different setup and does not use a detail view.
import SwiftUI
import Combine
struct ContentView: View {
@ObservedObject var fetcher = MovieFetcher()
var body: some View {
VStack {
List(fetcher.movies) { movie in
VStack (alignment: .leading) {
Text(movie.name)
Image(movie.thumbnail)
Text(movie.released)
.font(.system(size: 11))
.foregroundColor(Color.gray)
}
}
}
}
}
public class MovieFetcher: ObservableObject {
@Published var movies = [Movie]()
init(){
load()
}
func load() {
let url = URL(string: "https://wolvideos.web.app/videos.js")!
URLSession.shared.dataTask(with: url) {(data,response,error) in
do {
if let d = data {
let decodedLists = try JSONDecoder().decode([Movie].self, from: d)
DispatchQueue.main.async {
self.movies = decodedLists
}
}else {
print("No Data")
}
} catch {
print ("Error")
}
}.resume()
}
}
struct Movie: Codable, Identifiable {
public var id: Int
public var name: String
public var thumbnail: String
public var released: String
enum CodingKeys: String, CodingKey {
case id = "id"
case name = "name"
case thumbnail = "thumbnail"
case released = "description"
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
If possible can someone explain how and the best way to load a json file and then let the app format it into detail views?
it's infuriating that it sometimes only takes 2 minuets and then takes hours on other devices. knowing how long it's going to take would actually give us information to make adjustments.
I'm. asking this one to see how many other developers would also like this feature, and to see if anyone knows of an add-on or such.