Im working on an app that now has to go to a URL online and read all filenames inside a folder and get them into the app for parsing. How do I achieve this in iOS? Ive looked at NSURLConnection and didn't find anything.
What didn't you find with NSURLConnection that you were looking for?
That would be one standard way of downloding a file using HTTP.
You could do this, but it is synchronous.
NSData *urlData = [NSData dataWithContentsOfURL:url];
The only way to do this turns out to be a server side script!
Related
I'm aware that there are similar questions posted, but this is a very specific issue that may or may not be related to code, it might be due to where I'm sourcing the file, and I need some advice.
I have an iPad app and am detecting whether there is an internet connection. If there is, then a .csv file is downloaded, saved, then split into an array. If not then a file held in the main bundle is used.
When using the file held in the main bundle, I can extract the data. The problem I have is when I try to download the .csv. The file is held on Document Manager, a Content Management System that is the only secure area for the file to be held for the company I work for and is therefore unavoidable. If I use this code:
NSString *urlString = #".../view-document.cgi?f=fundsspreadsheetc.csv";
(sorry, I need to keep the full link confidential)
NSURL *csvURL = [NSURL URLWithString:[urlString stringByAddingPercentageEscapesUsingEncoding:NSASCIIStringEncoding]];
NSData *urlData = [NSData dataWithContentsOfURL:csvURL options:NSDataReadingMappedAlways error:nil];
then urlData returns nil.
The original url opens the file on a windows laptop, but with a file name of view-document.cgi. I don't know if this is relevant.
If I change the url to:
"
http://download.finance.yahoo.com/d/quotes.csv?s=^GSPC+^IXIC+^dji+^GSPC+^BVSP+^GSPTSE+^FTSE+^GDAXI+^FCHI+^STOXX50E+^AEX+^IBEX+^SSMI+^N225+^AXJO+^HSI+^NSEI&f=sl1d1t1c1ohgv&e=.csv"
from a question set by shebi, then my code works.
What do I need to do to get my file to download?
much appreciated
Thanks for the help, it was problem with the link.
I have a few files I'm trying to download in the following format:
http://someserver.com/movie_title 5.1.12.mov
When I place the following code on it:
mediaURL =[[mediaURL stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
I get the following result:
http://someserver.com/movie_title%205.1.12.mov
If I copy paste that into my browser on any machine, it downloads correctly. However, It won't download as an NSURL on my project; it just fails.
Any suggestions? I'm guessing it's something silly that I'm forgetting.
If I were you I would confirm that the escaping is the problem, it could be some other part of your code that is failing (if you have access to the server manually rename the file with only characters.mov)
If you try this and it works, make sure you are using the expected headers and http method (POST or GET).
Else please put the code on git so we can download it and figure out what is wrong.
I've found that the server that the video was coming from did not support streaming capabilities. I tried putting the same link on my safari browser and the item did not stream.
Thanks everyone
I've written an iOS app that loads up an HTML5 web application inside a UIWebView. I'm trying to make the app cache everything in the UIWebView for use while on a 3G connection rather than load it all again.
It needs to follow all links while on a WiFi connection and download all pages and images. When on a 3G connection it should then load everything from the cache.
Is this possible? Any ideas on where I should start? Do I need to make my HTML5 application load all pages in the background rather than have the app do it?
Thanks for any help!
Firstly I would use Reachability to detect if there is a WIFI connection and your server is accessible. This way you can handle both 3G connectivity and no internet connection in the same way. (accessing the cached pages). It also means you can use the cache if you have WIFI but still cannot reach your server.
Reachability sample code
I would either download a zip file of your web pages and all the associated assets (if it is a fairly static website) and unzip and store them in the local file system. You can then point your UIWebView to the local directory when there is not WIFI.
You could use something like Zip Archive to handle this
ZipArchive GitHub page
If this is not an option then you could get the HTML from the page when you load it in the UIWebView. The problem with doing this is you are reliant on the user accessing pages in order to cache them.
You can use this to get the HTML code and store it locally:
// Determile cache file path
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *filePath = [NSString stringWithFormat:#"%#/%#", [paths objectAtIndex:0],#"index.html"];
// Download and write to file
NSURL *url = [NSURL URLWithString:#"http://www.google.co.uk"];
NSData *urlData = [NSData dataWithContentsOfURL:url];
[urlData writeToFile:filePath atomically:YES];
// Load file in UIWebView
[web loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:filePath]]];
You could use this code to get all the web pages in the background as well. To do this you will either need to hard code all the required URLs and look them up individually or better still download a list of web pages from your site first.
You could create a web service (JSON is probably your best option for this) giving a list of URLs to download. You could then parse through this JSON and use the URLs to feed into the code above and cache the HTML locally.
Just remember to be careful with your hyperlinks and links to any images and CSS within your HTML as this will break your cached website if they are looking for your full online URL rather than a relative path.
I am a beginner to developing apps on the iPhone and I am trying to upload a NSString on to a FTP server Filezilla. The codes I used is below.
NSString *inputtext = #"Test";
NSURL *ftpserver=[NSURL URLWithString:#"ftp://username:password#domian/test.txt"];
[inputtext writeToURL:ftpserver atomically:NO encoding:NSUTF8StringEncoding error:nil];
I am wondering is this the correct syntax? I am unable to see the file in the server and I want to exclude programming error before I changed any setting on the server. Thanks in advance.
After a weekend of work, I found out that FTP transfer is not supported on writeToURL. The only way to do this is to open a ftp input and output stream using CFNetwork classes. The code is pretty long to do a simple thing like this so I won't post it here unless anyone request for it below.
My app was rejected after App Store review for using the private method -[UIImage initWithData:cache:], but this is my code :
NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:[details image]]];
picture = [[UIImage alloc] initWithData:imageData];
An earlier version of the app I submitted did contain an initWithData:cache: call, but I removed this before resubmitting. Is it possible I need to clean the build? Is the build somehow "remembering" the old code?
All the functions you used in that snippet have public documentation, so it would be worth complaining to Apple and demanding clarification. Here are the links to the documentation for each of those, BTW:
NSURL URLWithString
NSData dataWithContentsOfURL
UIImage initWithData
Note that Apple's message refers to the "initWithData:cache:", however, the snippet you provided refers to "initWithData:". Are you sure that the code you've provided is what was referred to by Apple?
(Copied out from an edit to the question by the OP.)
I resolved this by creating a completely new project and copying files across from old project. It looks like something was ghosting from when the project did contain the initWithData:cache: method.