Xcode and Reading documents from a URL connection.

I have an Xcode app where currently txt files in the project display text data as a list. I can search through the lists and have buttons that will swap between different lists of information that you can look through.

The next task is I have URL connections to docx files on a SharePoint site. I am trying to use an URLsession function to connect to the URL links to download the documents to the document directory then have the application read the doc information to then be displayed as the txt info would.

The idea is that the docx files are a type of online update version of the data. So when the app is used and on wifi, the app can update the list data with the docx files.

I have code set up that should access the URL files but I am struggling to figure out how to read the data and access from this Documents directory. I have been looking online and so far I am at a loss on where to go here.

If anyone can help or provide some insight I would greatly appreciate it.

I can try and provide code samples to help explain things if that is needed.

Answered by DTS Engineer in 866764022
I do use URLsession.

OK. Then let’s use this thread to focus on the networking side of things. Once you’ve got that working, you can start a new thread with an appropriate subtopic and tags for your next step.

URLSession is a very flexible API, so there are numerous ways you might use it to download a file. However, let’s explore a simple form. Imagine you have a button like this:

Button("Download") {
    Task {
        await runDownload()
    }
}

That calls the runDownload() function, which looks like this:

func runDownload() async {
    do {
        print("will run download")
        let url = URL(string: "https://example.com")!
        let request = URLRequest(url: url, cachePolicy: .reloadIgnoringLocalCacheData, timeoutInterval: 60.0)
        let (downloadURL, response) = try await URLSession.shared.download(for: request)
        let httpResponse = response as! HTTPURLResponse
        print("did run download, status: \(httpResponse.statusCode), downloadURL: \(downloadURL)")
    } catch let error as NSError {
        print("did not run download, error: \(error.domain) / \(error.code)")
    }
}

That calls the download(for:delegate:) method, which downloads the file to a temporary location and returns you the HTTP response (response) and the file URL (downloadURL). You would normally look at the response to make sure it’s OK — for example, check that the statusCode is 200 — and, if so, move the file from its temporary location to wherever you want to store it in the long term.

Please try that and let me know how you get along.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

There seems to be multiple parts to your question:

  • Downloading content
  • Managing files in the Documents directory
  • Displaying that content

Which one are you having problems with?

This is important because it informs how to route your question. For example, if you’re having problems with URLSession then that’s something I can help you with, but I’m not the best person to talk to about UI frameworks.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

I do use URLsession. From what I could find with research online and even checking with ChatGPT for more specific questions on the topics that seemed to be what I needed to allow the app to connect to a website and use URL links to download docx files for the app.

The problem that I am seeing is I am not sure if I am using the URLsession correctly or if there are additional steps I need to take when it comes to accessing this Documents Directory that is on the mobile device itself to access the documents.

From my own testing when Io run the app I try checking documents on the mobile device but nothing looks to update so my guess is I am not using the URLsession function correctly.

I am a beginner still with using swift and Xcode so I am trying to learn as much as I can for working on this project.

My Project is setup with multiple .view scripts to handle different parts of the app itself.

They are setup as follows: ContentView This script is used for viewing the app itself where I have buttons displayed that swap between different lists that could be viewed and searched through using the searchable modifier.

This function works correctly and I can change the lists by pressing the buttons just fine.

The listed data comes from txt files in the project as test data to work with.

FileUpdater This file would be used to download the docx files from the URL links when the device is connected to a specific wifi network that I have listed and set to check for the needed connection. I also have the URL links as well.

This should also download the files to save locally.

FileLoader This script is supposed to take the docx data that was downloaded and upload it to the txt files by conversion and writing the data and this whole process is a type of online update system. So when the online docx are modified the list will stay up to date with the new information when I use the app to search through.

FileInitializer This script is a singe run that is used to copy the bundle txt files to the document directory on the first launch of the app. This should help with the data conversion being done in my other scripts as mentioned.

The final two scripts are as follows: HighlightHelper SearchTextChangeHandler

These are small scripts used to help with the basic search functionality when using the application.

I am sorry if this is adding too much information or detail but I hope this helps understand what I am trying to create with this project.

I do use URLsession.

OK. Then let’s use this thread to focus on the networking side of things. Once you’ve got that working, you can start a new thread with an appropriate subtopic and tags for your next step.

URLSession is a very flexible API, so there are numerous ways you might use it to download a file. However, let’s explore a simple form. Imagine you have a button like this:

Button("Download") {
    Task {
        await runDownload()
    }
}

That calls the runDownload() function, which looks like this:

func runDownload() async {
    do {
        print("will run download")
        let url = URL(string: "https://example.com")!
        let request = URLRequest(url: url, cachePolicy: .reloadIgnoringLocalCacheData, timeoutInterval: 60.0)
        let (downloadURL, response) = try await URLSession.shared.download(for: request)
        let httpResponse = response as! HTTPURLResponse
        print("did run download, status: \(httpResponse.statusCode), downloadURL: \(downloadURL)")
    } catch let error as NSError {
        print("did not run download, error: \(error.domain) / \(error.code)")
    }
}

That calls the download(for:delegate:) method, which downloads the file to a temporary location and returns you the HTTP response (response) and the file URL (downloadURL). You would normally look at the response to make sure it’s OK — for example, check that the statusCode is 200 — and, if so, move the file from its temporary location to wherever you want to store it in the long term.

Please try that and let me know how you get along.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

I do have one question, the URL links I have for connecting online are linked directly to docx files in the site rather than just the site itself. Is that something that would cause errors for downloading the documents rather than connecting to the site first then try pulling the documents?

Is that something that would cause errors for downloading the documents … ?

It very much depends on the server. Given a URL for something downloadable, some servers allow you to download the resource directly and other require you to jump through hoops. You can see this even on Apple servers. For example, SF Symbols 7 is directly downloadable:

https://devimages-cdn.apple.com/design/resources/download/SF-Symbols-7.dmg

but Xcode 26.1.1 is not:

https://download.developer.apple.com/Developer_Tools/Xcode_26.1.1/Xcode_26.1.1_Universal.xip

In the Xcode case, the server redirect the download request to a login page. That login page assumes that the client is a person using a web browser, and it’s very hard to programmatically deal with the required authentication.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

The URL link would be connecting to a SharePoint site and in that site the URLs are for the docx files themselves.

Is SharePoint a server one that would cause issues with downloading docx files directly? Or would a login need to be taken care of in some way before hand?

Is SharePoint a server one that would cause issues with downloading docx files directly?

I expect that’ll depend on how the server is configured. I recommend that you write a small test project to fetch the URL and see what you get back. Usually this yields one of three results:

  • The request succeeds with a 2xx status code and returns the right data.
  • The request fails with a 4xx status code, indicating that you have to log in. In this case you may be able to gets things working using the authentication challenge mechanism in URLSession.
  • The request success with a 2xx status code but the actual data is a login page. This is hard to deal with.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

Xcode and Reading documents from a URL connection.
 
 
Q