Trying to create an app in SwiftUI that uses HTTP for "gets", "posts", "forms", et al.
I cannot assign URLSessionDataDelegate,URLSessionTaskDelegate, URLSessionDelegate to a SwiftUI view. So my assumption is that I need to create a UIViewControllerRepresentable for a regular UIViewController and assign the delegates to that UIViewController
I just need to know if this is the correct path to proceed with? Or is there some sort ion tie in that I cannot find using Google?
Thank you
Selecting any option will automatically load the page
Post
Replies
Boosts
Views
Activity
How can I keep a class declaration within SwiftUI view persistent through any of the view's updates?
I have a class, MyURL, from a UIKit app that I wrote that handles all my URL needs, uploads, downloads, GETS, POSTS, etc.
I would like to use that class from within a SwiftUI view now. So the SwiftUI view that creates calls MyURL methods. And I would pass binding vars that would cause my SwiftUI view to update things such as statuses : percentage done, etc.
My issue is, how can I properly declare that class, myURL, from within the SwiftUI view, so that it doesn't get redeclared every time the view updates.
Should I declare the MyURL class in a view above it and pass it into the view that calls the MyUrl class? Would just like to do it the proper way.
I wish I was better with terminology.
Thank you
struct ContentView: View {
**var myURL = MyURL()**
@State var proceed = false
var body: some View {
Button {
myURL.pressed(proceed: $proceed)
}
label: {
Text(proceed ? "pressed" : "not pressed")
}
}
}
class MyURL: NSObject, URLSessionDataDelegate,URLSessionTaskDelegate, URLSessionDelegate, URLSessionDownloadDelegate{
func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didFinishDownloadingTo location: URL) {
}
func pressed(proceed: Binding<Bool>) {
print("\(proceed)")
proceed.wrappedValue.toggle()
}
// Error received
func urlSession(_ session: URLSession, task: URLSessionTask, didCompleteWithError error: Error?) {
if let err = error {
DispatchQueue.main.async { [self] in
//code
}
}
}
// Response received
func urlSession(_ session: URLSession, dataTask: URLSessionDataTask, didReceive response: URLResponse, completionHandler: (URLSession.ResponseDisposition) -> Void) {
completionHandler(URLSession.ResponseDisposition.allow)
if let httpResponse = response as? HTTPURLResponse {
xFile?.httpResponse = httpResponse.statusCode
DispatchQueue.main.async { [self] in
if httpResponse.statusCode != 200 {
//code
}
}
}
}
// Data received
func urlSession(_ session: URLSession, dataTask: URLSessionDataTask, didReceive data: Data) {
if xFile?.httpResponse == 200 {
DispatchQueue.main.async { [self] in
//code
}
} //DispatchQueue.main.async
}
}
func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didWriteData bytesWritten: Int64, totalBytesWritten: Int64, totalBytesExpectedToWrite: Int64) {
let num : Float = Float(totalBytesWritten * 100)
let den : Float = Float(totalBytesExpectedToWrite * 100)
let percentDownloaded : Int = Int(num/den * 100)
DispatchQueue.main.async { [self] in
//code
}
}
}
I hope this isn't too off-topic, but I'm in a bind
My HD is so full that I can't download any new iOS' in Xcode. Actually, there's a lot I can't do. According to the storage part of System Details, Developer is over 100 GB.
I have a huge external HD and would like to completely remove and clean Xcode, and the reinstall it on the external HD.
1 - Is this allowed? Some people say that all apps must be installed in the Applications folder. Checking the web I get both answers.
2 - If it is allowed, is there a way to properly clean out? Because every website I found that describes this procedure is touting their own cleanup app.
Thank you
Am trying to figure out how to dismiss the iOS keyboard if my TextFields do not have focus. Obviously when clicking a button I can call a dismiss keyboard function.
But what I wanted to learn was how to dismiss the keyboard of if the user hits return OR clicks off of the TextFields.
Before I could figure out the "user hit return part" I got stuck at the user being able to click away from the TextFields.
While I can add a onTapGesture to the text fields, I wonder if I can do something like detecting a tapGesture to the entire screen, so that if the user taps on any blank space I could call the dismiss keyboard function.
import SwiftUI
struct ContentView: View {
@State var textField1 = ""
@State var textField2 = ""
@State var hasFocus = "No text field has focus"
@FocusState var leftTyping : Bool
@FocusState var rightTyping : Bool
var body: some View {
VStack {
Text(hasFocus)
.font(.largeTitle)
HStack {
TextField("left" , text: $textField1)
.focused($leftTyping)
.onChange(of: leftTyping) {
if leftTyping == false, rightTyping == false {
hideKeyboard()
hasFocus = "No text field has focus"
}
else if leftTyping {
hasFocus = "focus on left field"
}
}
TextField("right", text: $textField2)
.focused($rightTyping)
.onChange(of: rightTyping) {
if leftTyping == false, rightTyping == false {
hideKeyboard()
hasFocus = "No text field has focus"
}
else if rightTyping {
hasFocus = "focus on right field"
}
}
}
Button ("steal focus"){
hideKeyboard()
hasFocus = "No text field has focus"
}
.buttonStyle(.borderedProminent)
.tint(.brown)
.font(.largeTitle)
.padding(10)
.foregroundStyle(.white)
}
.padding()
}
func hideKeyboard() {
UIApplication.shared.sendAction(#selector(UIResponder.resignFirstResponder), to: nil, from: nil, for: nil)
}
}
#Preview {
ContentView()
}
Is there a way to do this?
Someone showed me the code below, when I was trying to figure out how to detect which views were being intercepted by one continuous DragGesture.
It works, but I would like to find a tutorial whre I can learn what's going on here.
From what I can tell, the background modifier is a closure, that calls a function which has a GeometryReader which returns a view.
I've Googled .background as a closure in swiftui and still can't find any form of tutorial that discuss what is going on here. Not looking for the answers, looking to learn.
Thank you
struct ContentView: View {
@State private var dragLocation = CGPoint.zero
@State private var dragInfo = " "
private func dragDetector(for name: String) -> some View {
GeometryReader { proxy in
let frame = proxy.frame(in: .global)
let isDragLocationInsideFrame = frame.contains(dragLocation)
let isDragLocationInsideCircle = isDragLocationInsideFrame &&
Circle().path(in: frame).contains(dragLocation)
Color.clear
.onChange(of: isDragLocationInsideCircle) { oldVal, newVal in
if dragLocation != .zero {
dragInfo = "\(newVal ? "entering" : "leaving") \(name)..."
}
}
}
}
var body: some View {
ZStack {
Color(white: 0.2)
VStack(spacing: 50) {
Text(dragInfo)
.foregroundStyle(.white)
HStack {
Circle()
.fill(.red)
.frame(width: 100, height: 100)
.background { dragDetector(for: "red") }
Circle()
.fill(.white)
.frame(width: 100, height: 100)
.background { dragDetector(for: "white") }
Circle()
.fill(.blue)
.frame(width: 100, height: 100)
.background { dragDetector(for: "blue") }
}
}
}
.gesture(
DragGesture(coordinateSpace: .global)
.onChanged { val in
dragLocation = val.location
}
.onEnded { val in
dragLocation = .zero
dragInfo = " "
}
)
}
}
I am trying to watch an @EnvironmentObject in the Xcode debugger and it comes up as an Invalid Expression in the View pane. If I want to view it, I need to declare it as a local variable and then assign it the value of the passed in @EnvironmentObject.
While not impossible, it's a lot of work. Or maybe I am doing something incorrectly and this is a symptom of that? Would like to resolve this.
encl: Example code & Screenshot, where choice is the passed/unviewable EnviornmentObject and ch is the local variable I use to view it in the debugger
Project "TestingApp"
File: TestingApp
import SwiftUI
@main
struct TestingApp: App {
@StateObject private var choice = Choices()
var body: some Scene {
WindowGroup {
ContentView()
.environmentObject(choice)
}
}
}
File: Choices
import Foundation
@MainActor
class Choices: ObservableObject {
@Published var aChoice = 1
@Published var bChoice = 2
}
File: ContentView
import SwiftUI
struct ContentView: View {
@EnvironmentObject private var choice: Choices
var body: some View {
VStack {
let ch = choice
Text("\(choice.aChoice)")
.font(.largeTitle)
.padding(.bottom)
Text("2")
.font(.largeTitle)
}
.padding()
}
}
#Preview {
ContentView()
}
Would like to be able to bring up the iOS keyboard in a SwiftUI view without having to use a TextField? The goal would be to capture each keyup, or keydown, using .onKeyPress
While I thought I could create a TextField not visible to the user, was hoping there was a cleaner way.
Is there any app out there that lets you browse through a CoreData database? When I first started to learn Swift, an app called Liya seemed to work. But alas, no longer.
it would just make it easier if there was anything out there that let you browse the data directly.
Thanks
Can I open two separate Xcode windows with the same project ?
I have multiple monitors, so would love to be able use them to view different files of the same project.
Is there a way?
Before Xcode 15, when I could access the info.plist file, I was able to add exceptions to the App Transport Security Settings so I could connect with my home server, which has no HTTPS, just HTTP.
But in Xcode 15 I have no idea, not can I buy a clue with google, on how to do this.
Please help!
Thanks
p.s. I should probably add that one site mentioned going to the Target section of your project allows easy access to info.plist. Yet for some strange reason, there is no item in Targets, which is odd, as I can debug my. project.
Topic:
Developer Tools & Services
SubTopic:
Xcode
Before Xcode 15, when I could access the info.plist file, I was able to add exceptions to the App Transport Security Settings so I could connect with my home server, which has no HTTPS, just HTTP.
But in Xcode 15 I have no idea, not can I buy a clue with google, on how to do this.
Please help!
Thanks
p.s. I should probably add that one site mentioned going to the Target section of your project allows easy access to info.plist. Yet for some strange reason, there is no item in Targets, which is odd, as I can debug my. project.
Is there a Finder type app that will read through my iPhone files?
I’m working on a app that records audio files to my iPhone, and it would be much easier if I could find an app where I could scroll through the files on my iPhone from my desktop as opposed to doingit on the iPhone itself.
Am new enough to SwiftUI that I that are still some concepts that confuse me. Case in point: .background
The code below is meant to detect when the user drags their finger over different areas, in this case three different size circles placed over each other.
The code works, but I get lost trying to figure out how the logic works.
.background calls a function that's a view builder, yet doesn't an actual view? Unless Color.clear is the view it's returning?
I have more questions, but might as well start with .background since it comes first? I think?
Thanks
import SwiftUI
struct ContentView: View {
@State private var dragLocation = CGPoint.zero
@State private var dragInfo = " "
@State private var secondText = "..."
private func dragDetector(for name: String) -> some View {
GeometryReader { proxy in
let frame = proxy.frame(in: .global)
let isDragLocationInsideFrame = frame.contains(dragLocation)
let isDragLocationInsideCircle = isDragLocationInsideFrame &&
Circle().path(in: frame).contains(dragLocation)
Color.clear
.onChange(of: isDragLocationInsideCircle) { oldVal, newVal in
if dragLocation != .zero {
dragInfo = "\(newVal ? "entering" : "leaving") \(name)..."
}
}
}
}
var body: some View {
ZStack {
Color(white: 0.2)
VStack(spacing: 50) {
Text(dragInfo)
.padding(.top, 60)
.foregroundStyle(.white)
Text(secondText)
.foregroundStyle(.white)
Spacer()
ZStack {
Circle()
.fill(.red)
.frame(width: 200, height: 200)
.background { dragDetector(for: "red") }
Circle()
.fill(.white)
.frame(width: 120, height: 120)
.background { dragDetector(for: "white") }
Circle()
.fill(.blue)
.frame(width: 50, height: 50)
.background { dragDetector(for: "blue") }
}
.padding(.bottom, 30)
}
}
.ignoresSafeArea()
.gesture(
DragGesture(coordinateSpace: .global)
.onChanged { val in
dragLocation = val.location
secondText = "\(Int(dragLocation.x)) ... \(Int(dragLocation.y))"
}
.onEnded { val in
dragLocation = .zero
dragInfo = " "
}
)
}
}
#Preview {
ContentView()
}
While running Swift's SpeechRecognition capabilities I get the error below. However, the app successfully transcribes the audio file.
So am not sure how worried I have to be, as well, would like to know that if when that error occurred, did that mean that the app went to the internet to transcribe that file? Yes, requiresOnDeviceRecognition is set to false.
Would like to know what that error meant, and how much I need to worry about it?
Received an error while accessing com.apple.speech.localspeechrecognition service: Error Domain=kAFAssistantErrorDomain Code=1101 "(null)"
I find Xcode's Debugging Variable View to be to cluttered, even when just watching "local" variables.
Is there anyway to set it so that it only shows the variables I want to watch? Would make debugging so much easier.