Im making a standalone app for the Apple Watch and Im trying to play sound there, after trying all I could think of I only get errors with it. I wonder if I should import the sound file in a specific way or folder
I made a simple test with the code Im running in app, the test sound is calle 5.wav
The message is not being printed and get some error related to sound not being loaded
"Watch App[53825:678174] void * _Nullable NSMapGet(NSMapTable * _Nonnull, const void * _Nullable): map table argument is NULL
"
import SwiftUI
import AVFoundation
struct ContentView: View {
#State var avp:AVAudioPlayer?
var body: some View {
VStack {
Button("PLAY")
{
guard let url = Bundle.main.url(forResource: "5", withExtension: "wav")else
{
return
}
try? avp=AVAudioPlayer(contentsOf: url)
avp?.play()
print("printing")
}
}
.padding()
}
}
Related
I have an Xcode 14.1 project that has a target with two destinations: "iPad" and "Mac (Designed for iPad)".
The code shown in this question is simplified from the actual project to illustrate the problem.
Since SwiftUI does not directly support PDFView directly the app has a PDFView wrapped in UIViewRepresentable:
import PDFKit
import SwiftUI
struct MyPDFView: UIViewRepresentable {
func makeUIView(context: Context) -> UIView {
let path = Bundle.main.path(forResource: "basic-link-1", ofType: "pdf")!
let pdfDocument = PDFDocument(url: URL(filePath: path))
let pdfView = PDFView()
pdfView.document = pdfDocument
pdfView.autoScales = true
return pdfView
}
func updateUIView(_ uiView: UIView, context: Context) {
}
}
("basic-link-1.pdf" is a simple PDF file that I got from an Adobe tutorial. It is a one-page document with various Link annotations.)
The PDFView is shown in the app's main view:
import SwiftUI
struct ContentView: View {
var body: some View {
MyPDFView()
}
}
When I run the app on an iPad or iPad simulator, the Link annotations in "basic-link-1.pdf" work. Tapping on one of them takes the user to another page in the document or opens a web page (depending on the exact type of link in the Link annotation).
When I run the same app directly on Mac in "Mac (Designed for iPad)" mode, nothing happens when I click on the links in the PDF view. Does anyone know what is different about this mode or what I have to do differently to get the links to react to clicks?
Additional info: I don't show it in my sample code here, but if I register a PDFViewDelegate for MyPDFView, its pdfViewWillClick(onLink:sender:url) callback gets called if the app is running on an iPad, but does not get called if the app is running in "Mac (Designed for iPad)" mode.
Also, all development and testing above was done on a MacBook Air M2.
Why is my SwiftUI Swipe Action behaving like this?
I don't now how to add a GIF in stack overflow so here is a imagur link https://imgur.com/a/9MqjIgX.
If you don't want to click on external links here is a image from the GIF:
My View:
struct MyView: View {
#State var shops = [Shop.empty(), Shop.empty(), Shop.empty(), Shop.empty(), Shop.empty()]
var body: some View {
NavigationView {
List($shops) { $shop in
Text(shop.name)
.swipeActions {
Button {
shop.toggleFavourite()
} label: {
Image(systemName: "star")
}
}
}
}
}
}
the shop struct:
struct Shop: Hashable, Identifiable {
var id: UUID
var favourite: Bool
init(id: UUID){
self.id = id
self.favourite = UserDefaults.standard.bool(forKey: id.uuidString)
}
mutating func toggleFavourite() {
favourite.toggle()
UserDefaults.standard.set(favourite, forKey: id.uuidString)
}
static func empty() -> Shop{
Shop(id: UUID())
}
}
But I can't sadly I can't give you a working example, because I tried to run this code in a fresh app and it worked, without the Bug. On the same device. And I don't understand why, because I also put this view in the root of my old project, just for testing, and the bug stayed there.
I was able to figure out, that if I commented out this line:
UserDefaults.standard.set(favourite, forKey: id.uuidString)
my code would work. But unfortunately I can't just leave out this line of code.
I tried several things, including wrapping this line into DispatchQueue.main.async {} and DispatchQueue.main.sync {}, same with the DispatchQueue.global(). I also added delays. Short delays wouldn't work at all (under .5 seconds) and longer delays would just delay the view bug.
Of course I also tried wrapping this line into a separate function, and so on.
There are two mayor points, why I'am so confused:
Why is the line, that sets this to the Userdefaults even influencing the view? I mean I checked with a print statement, that the initializer, which is the only section in my code that checks this Userdefaultvalue, only gets called when the view gets initialized.
Why does the code work in a different project?
I know since I can't provide a working example of my bug it's hard for you to figure out whats wrong. If you have any ideas, I would be very happy!
I'm developing an Ipad app that contains a MapKit Map using SwiftUI and I get the following error message in the XCode console everytime when the view with the map disappears:
[Memory] Resetting zone allocator with 24 allocations still alive.
Thankful for any help on how to solve it. :)
Related threads
First, before posting some code, let me say that there are a few threads related to this error. I've looked at this SO thread, Resetting zone allocator with allocations still alive, but it relates to UIKit and not to SwiftUI and either way I get the error message without adding annotations or zooming.
Another related thread here on SO is SwiftUI - EnvironmentObject - Strange Memory Usage but there the OP uses MKMapView instead of SwifUI's Map. Preferably, I'd like to use Map, but if nothing else MKMapView might be a way forward.
There is also some threads on the Apple developer forum, such as this one, but I've found no complete matches (in the linked thread, the OP only gets the error once, while it reoccurs for me every time the view with the map disappears).
Minimal reproducible example
The error occurs each time the view button that removes the Map is pressed.
View creating the error
import MapKit
import SwiftUI
struct ContentView: View {
#State private var region = MKCoordinateRegion(center: CLLocationCoordinate2D(latitude: 51.507222, longitude: -0.1275), span: MKCoordinateSpan(latitudeDelta: 10, longitudeDelta: 10))
#State private var showMap = true
var body: some View {
if showMap {
VStack {
Map(coordinateRegion: $region)
Button("Hide map") {
showMap = false
}
.font(.title)
}
} else {
Button("Show map") {
showMap = true
}
.font(.title)
}
}
}
Set-up view
import SwiftUI
#main
struct SwiftUIByExampleApp: App {
var body: some Scene {
WindowGroup {
ContentView()
}
}
}
first question on StackOverflow (and relatively new to native app dev targeting macOS).
I'm currently trying to build a simple SwiftUI view, that leverages WKWebView's loadHTMLString function, to display hardcoded HTML string on the screen.
AFAIK webkit does not support SwiftUI as of the moment, so I need to embed AppKit UI in my SwiftUI app using NSViewRepresentable. This what I got so far following the docs and XCode's autocomplete:
import SwiftUI
import WebKit
struct HTMLView: NSViewRepresentable {
typealias NSViewType = WKWebView
let html = "<h1>Hello wordl</h1>"
func makeNSView(context: Context) -> WKWebView {
let webview = WKWebView()
return webview
}
func updateNSView(_ nsView: WKWebView, context: Context) {
nsView.loadHTMLString(html, baseURL: nil)
}
}
struct HTMLView_Previews: PreviewProvider {
static var previews: some View {
HTMLView()
}
}
Should be noted that preview canvas does not load the HTML (displays empty window).
I then replace the default Text() view in ContentView.Swift with HTMLView(), and run my application.
The application compiles, but the WebView fails to load the HTML (I get an empty window). I get the following errors in console:
WebPageProxy::processDidTerminate: (pid 0), reason 3
WebPageProxy::dispatchProcessDidTerminate: reason = 3
WebPageProxy::processDidTerminate: (pid 0), reason 3
WebPageProxy::dispatchProcessDidTerminate: reason = 3
WebPageProxy::tryReloadAfterProcessTermination: process crashed and the client did not handle it, not reloading the page because we reached the maximum number of attempts
Any help with the above would be highly appreciated!
On macOS, although it doesn't seem like this should be necessary, you need to set "Outgoing Connections (Client)" to true in your "Signing and Capabilities" on your target in order for WKWebView to load, even though you're loading from a string and not from an external page.
As soon as I changed this, your example worked fine.
The way to achieve Master and Detail structure, as of beta 4, it should be by using .navigationViewStyle(.doubleColumn). Works perfectly on iOS / iPadOS / macOS but not in tvOS... it's a bug or I'm missing something?
import SwiftUI
struct ContentView: View {
var arra = ["Margherita","Marinara","Calzone"]
var body: some View {
NavigationView {
List(arra, id: \.self) { pizza in
NavigationLink(destination: SecondView(pizza: pizza)) {
Text(pizza)
}
}.navigationBarTitle("MASTER")
SecondView(pizza: arra[0])
}
.navigationViewStyle(.doubleColumn)
}
}
struct SecondView: View {
var pizza : String
var body: some View {
Text(pizza)
.navigationBarTitle("DETAIL")
}
}
(as of GM release) The official doc tells something like: double column style will be stacked on tvOS, like it's an iPhone in portrait mode. So it's impossible to automatically achieve the "master and detail" look like it's an iPad in landscape, you have to build it for yourself. Even if you use one UISplitViewController in UIKit! With this behavior I think it's good to go only if the master it's a fullscreen CollectionView, sorta like Netflix App.