Transfer Strings as .txt over FTP on iPhone - ios

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.

Related

Codename one Unicode/emoji on iOS

I've been working on an app which includes messaging.
We've noticed that emoji characters are causing some issues.
I've updated the server database to support them, and everything is working fine on Android and the simulator but iOS is failing.
For some reason when you send multiple emojis only some of them arrive.
Our data syncing system packages everything into a tar ball which is then unpacked into storage and read into the database.
I think one of the following points is causing this encoding issue;
During the network transfer (unlikely since it's a binary file)
When writing to disk
When reading from disk
When writing to SQL
When reading from SQL
By downloading the container for the app and inspecting the db file I can see it's already been broken by the time it get's there, so I think it's either the file stuff or writing into SQL.
I also attempted the techniques described in this;
Unicode File IO in Codename One
At some point along the line encoding breaks. Make sure you don't use any of the problematic methods such as:
new String(byte[])
String.toByteArray()
OutputStreamWriter(OutputStream)
InputStreamReader(InputStream)
There are a few more but all of these can lose encoding. You'll need to narrow this down to the exact method that's failing for you.
I was experiencing the same problem myself. What fixed it for me was calling the following on iOS before I save the text in sqlite
NSData *data = [originalValue dataUsingEncoding:NSNonLossyASCIIStringEncoding];
NSString *goodValue = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];

getting csv to download in iOS

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.

EXC_BAD_ACCESS / SIGSEGV when uploading with FTP library on iOS (CFWriteStreamOpen)

I am using FTPHelper to perform basic download/upload FTP functions. I have been getting frequent crashes when using it to upload to my destination. The crashes don't seem to ever occur on the first upload but become more and more likely as I make subesequent calls. Xcode spits out EXC_BAD_ACCESS(code=1, address=0x30) at line 517 of the helper class::
success = CFWriteStreamOpen(writeStream);
I've enabled exception breakpoints and zombies but I'm not seeing anything I can make sense of. Here's a snap of what my stack trace looks like at each crash:
I have been able to recreate this same stack trace with several other FTP libraries (BlackRaccon and FTPManager).
The file being uploaded is a simple one generated from the user's interaction with the UI, meaning it's dynamic based on what the user selects on screen. Here is the code that generates, locally saves, and uploads the file:
//Prepare to write a new file to the device and then upload it
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *putContent = [NSString stringWithFormat:#"V%d\n", (int)(funnySlider.value * -1)];
NSString *writepath = [documentsDirectory stringByAppendingPathComponent:#"foo.dat"];
//if the path exists
if([[NSFileManager defaultManager] fileExistsAtPath:writepath]){
//Upload the newly created file
[putContent writeToFile:writepath atomically:NO encoding:NSASCIIStringEncoding error:nil];
[FTPHelper upload:#"foo.dat"];
}
Some odd things I wanted to highlight as well... Filling foo.dat with lots of mumbo jumbo content to increase its file size makes the app much more stable. It's almost reliable at 100KB+. I also have no crashes when using an iPod Touch 4th Generation (okay, I had ONE but it was after dozens and dozens of test sessions). iPhones and iPads do however. Any ideas on how to fix this or why it might be happening? Should I abandon this mode of uploading?
The issue encountered in the posted question is actually a reported bug in CFFTPStream. I received this information from another posting I made to the Apple dev forums when this post wasn't getting any attention.
After calling CFWriteStreamCreateWithFTPURL, immediately disable the stream's persistent connections like so:
CFWriteStreamSetProperty(writeStream,
kCFStreamPropertyFTPAttemptPersistentConnection,
kCFBooleanFalse);
More information can be found here in another StackOverflow question I stumbled upon when doing some more research and in the post I made to the dev forums. Hope this helps somebody!

iOS Cache a UIWebView

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.

Get list of files from URL iOS

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!

Resources