How to make iOS application act as web server
10,329
Try CocoaHTTPServer - you can use that to serve up a page with download links to files in the documents directory. (and to serve up the files themselves)
Related videos on Youtube
Author by
Sayali
Updated on September 15, 2022Comments
-
Sayali almost 2 years
I have developed an iPhone application which will have some files (images, audio & video) in applications document directory (Note: File are in documents directory & not resources or main bundle).
I want to create a unique URL which will allow user to download these files(from apps document directory) from browser on other devices as Mac, smartphones, etc.
So may be in short make iOS application act as web server allowing to download application's files.
How to achieve this?
-
Sayali almost 12 yearsI am able to use CocoaHTTPServer. I am able to connect through localhost on Simulator but on iPhone I am not able to connect. Please suggest.
-
Ertebolle almost 12 yearsThat might have to do with your iPhone's internet configuration - is it connected to the same WiFi router as your computer?
-
Sayali almost 12 yearsYes. Its connected to same wifi. And if this is the issue then atleast it should connect using iphone.local:<port> on same device safari browser. Right? But its not connected on same device browser too.
-
Sayali almost 12 yearsI am not able to connect on device. I have also posted new question for same here Please suggest
-
Sayali almost 12 yearsAlso is there any seyting on iPhone to make it act like webserver. Is there any ports I need to explicitly open for iPhone. Or may be some ports on wifi router?
-
Ertebolle almost 12 yearsIt wouldn't be able to connect with the app running in the background - server only works when it's in the foreground. So that explains why it can't connect from Safari on the device. The port would be specified at the end of the URL (after the :) - it's possible that port might be blocked for internal traffic on your router but it's pretty unlikely. Make sure you enter that port number on your computer's web browser, though.
-
Sayali almost 12 yearsWhen i tried from other device with app in foreground it works. Thanks a lot.
-
Sayali almost 12 yearsNow I have another doubt related to it. I have added code to download files listed. I have added "Content-Disposition = attachment" to make it downlodable. But on device it opens a file in browser itself. How can I make it save on Iphone device
-
Ertebolle almost 12 yearsMaking it save on an iPhone is impossible with Safari - it has nowhere to save files, all it can do is open them. However, if you write an app with a custom UIWebView, you can design it to check each link tapped on (in shouldStartLoadWithRequest:) to see if it's a file that should be downloaded, and if so, to download it (via an NSHTTPURLRequest) / save it to a file instead of opening it.