1

I have an angular project that I want to deploy using swifter in swiftUI. I have tried the below code.

import Foundation
import Swifter
class WebServer {
 let server = HttpServer()
 let path: String
 init(path: String) {
 self.path = path
 }
 func startServer() {
 if let webAppPath = Bundle.main.path(forResource: "index", ofType: "html", inDirectory: "/Users/user/Downloads/dirFile") {
 // Serve the Flutter web build files
 server["/"] = shareFilesFromDirectory(webAppPath)
 do {
 try server.start(8081)
 print("Web server started on port 8081")
 } catch {
 print("Failed to start the web server: \(error)")
 }
 } else {
 print("Web app path not found.")
 }
 }
 func stopServer() {
 server.stop()
 print("Web server stopped")
 }
}

When trying this, I am getting the following error:

Web app path not found.

I have tried giving path of the directory where my web application is from both, separate directory, and copied angular project directory in the Xcode project (Both gives the same result). Can someone help me out?

HangarRash
16.7k5 gold badges31 silver badges64 bronze badges
asked Jul 19, 2023 at 9:46

1 Answer 1

0

I could imagine it's because of sandboxing. That your app simply cannot access your downloads folder?!? (I assume you have a macOS app, right? If it's an iOS app than you cannot access files like that at all)

Check if that's the case. To test simply give your app full access to everything for testing: https://developer.apple.com/documentation/security/app_sandbox/accessing_files_from_the_macos_app_sandbox#4144040

If that's the problem, then depending on your use-case embed the index.html file in your apps bundle.

answered Jul 19, 2023 at 10:33
Sign up to request clarification or add additional context in comments.

3 Comments

Hi @Georg, Thanks for the answer 1- I am using this for an IOS app. I have tried giving the path of web application which I have copied inside the swiftUI project already. I have even tried giving absolute and related path. 2- If I give the path of downloads directory, will it not work on simulator?
No, the app will not really be able to leave it's sandbox. So you need to embed your files in your app. Try removing the inDirectory: part for the sake of testing it. I am quite sure this is your problem...
I embedded the files in the app and it started the server but when trying on localhost, it gives 404. I am sure all the files related to web are there. I have tried using npm http-server in the same directory, and it works that way. Do you have any idea why that can happen. Also if you can point out to any repo where same thing is achieved, may be with some different package. Let me know if you need any additional questions.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.