iPhone OS 3.2 File Sharing Path - iphone

Currently I have this for my file path and file...
NSURL *storeUrl = [NSURL fileURLWithPath: [[self applicationDocumentsDirectory] stringByAppendingPathComponent: #"Shared\PartyPlanner.sqlite"]];
This allows me to share the file with iTunes, but instead of just having 'PartyPlanner.sqlite' in the 'applicationDocumentsDirectory\Shared'
I have "SharedPartyPlanner.sqlite" in the 'applicationDocumentsDirectory'
is there a cleaner or easier way to get to the shared folder inside of applicationDocumentsDirectory?

In UNIX-like systems (including the iPhone OS), the directory separator is /, not \.
Also, in C-like languages (including Objective-C), the \ in a string is used to escape a character, e.g. \n → a new line. You need to type Shared\\PartyPlanner.sqlite if you really need a backslash in the file name.

Related

encoding information in filenames (iPhone/mac)

I want to encode a short title in filenames. The problem is that occasionally the title will contain a character such as a colon or a slash. Is there a standard encoding that would be typical/appropriate for this?
EDIT: to clarify, I want to encode the title in such a way that the encoded title could be used as a filename. Or is that called percent escaping?
The way I do this is with a category on NSURL, which I use to get the NSURL for a filename in a particular directory. Once I have this NSURL, I can fetch or save the file using the URL after performing the usual checks about whether or not the file already exists and handling those cases accordingly.
The relevant code snippet is:
+ (NSURL *)adnURLForFileName:(NSString *)fileName inDirectory:(NSSearchPathDirectory)searchDirectory {
NSString *percentEscapedFileName = [fileName stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSFileManager *fileManager = [[NSFileManager alloc] init];
NSURL *URLForDirectory = [[fileManager URLsForDirectory:searchDirectory inDomains:NSUserDomainMask] objectAtIndex:0];
return [NSURL URLWithString:percentEscapedFileName relativeToURL:URLForDirectory];
}
You can download the full category code from GitHub - NSURL+ADNFileHelpers
You could use -stringByReplacingOccurrencesOfString:withString: to replace the slash character with U+2044, the "solidus" aka "fraction slash". It looks like this: ⁄
http://en.wikipedia.org/wiki/Solidus_(punctuation)
The slash is not allowed in the Unix APIs. The colon is not allowed in HFS and in the old File Manager APIs. The same filename character will show up as a colon in the former and as a slash in the latter. In practice: you can use the Finder to rename a file to "/" (because the Finder uses the traditional Mac separator of :), but it will show up as ":" if you use ls.
If you need to allow both colons and slashes, you need to encode the characters somehow. You could use URL-style escaping, but if you expect the user to look at the filename in the Finder or in some other program, it's going to look horrible. It's better to escape just the path separator. For example, if you're using the Unix style APIs (path separator /), you could encode / as :- and : as :: (to avoid ambiguity). Or you could use some other little-used character for the escape.
I have approached this problem by filtering the title before using it in the filename. NSString has some useful methods, such as stringByStandardizingPath and stringByReplacingOccurrencesOfString:withString:. The filtering approach is lossy, in that the original title information might not be restorable. Similarly, I don't think encoding would work because iOS allows such a wide range of characters in its filenames. One possible alternative solution could be a plist archive with key=filename, value=title.

Importing CSV made in iPhone application

I am making a simple CSV file in my application, and I use characters such as 'æøå'. When I open the file on my Mac with BBEdit it opens the file correctly, but if I import the file in Excel (MS Office 2011) with Unicode UTF-8 it does not show the characters correctly. How do I fix this?
Some of the code:
NSString *tmpPath = NSTemporaryDirectory();
NSString *fullPath = [tmpPath stringByAppendingPathComponent:#"log_export.csv"];
[csvComplete writeToFile:fullPath atomically:YES encoding:NSUTF8StringEncoding error:NULL];
I am mailing the csv.
[mailController addAttachmentData:data mimeType:#"text/csv" fileName:#"sertifikat-loggbok.csv"];
CSV is a text format that does not specify the character encoding. Different applications assume different encodings.
My experience is that Microsoft Excel assumes it's in the default encoding which happens to be CP1250 (similar to ISO 8859-1 / Latin 1 encoding) on my machine. Maybe it always assumes CP1250.
So try to switch from UTF-8 to CP1250. But I cannot guarantee it'll always work because there simply is no encoding specified for CSV.

iphone NSFilemanager not creating file

I'm using NSFileManager to create a file at a specified path with text view contents.
I'm using following code
[[NSFileManager defaultManager] createFileAtPath:saveFileName contents:[[tView text] dataUsingEncoding:NSASCIIStringEncoding] attributes:nil];
But the problem is that, its creating file with no contents. I'm sure that text contents are not nil.
How can I check and confirm that data is written into file.
Some suggestions:
Capture the BOOL return of the line to make sure it is completing.
Log the value of [tView text] to make sure it is not empty.
ASCII encoding can cause problems these days. Try NSUTF8StringEncoding or NSUTF16StringEncoding.
Localization may be a problem with plain ASCII encoding because ASCII does not handle multibyte character encodings.
Well, I know it's few months late, but you're sending NSString instead of NSData to the contents: parameter.
Try:
[[tView text] dataUsingEncoding:NSUTF8StringEncoding]

iPhone: Invalid Characters in filesystem

is the iPhone's filesystem or the use of NSFilemanager restricted to any characters or is everything allowed (this is what I'm currently assuming after doing some research).
iPhone OS is derived from BSD. Thus as in other Unix'es the only characters forbidden in individual path elements are '/' and obviously '\0'. Everything else is allowed.

Tilde in device name causing problems with NSOutputStream socket

In the networking between the iPhone and desktop versions of our application, the iPhone sends over the device name for use on the desktop. The problem is that some of the beta testers have tildes (`) in their device names. For some reason when this is in the device name it prevent the socket from sending the actual string data.
I've tried simply cleaning up the device name before sending it, but the tilde in the device name (as entered in iTunes) is not recognized at runtime as a tilde. Here's the code that doesn't work:
NSString *safedevicename = [[UIDevice currentDevice] name];
safedevicename = [safedevicename stringByReplacingOccurrencesOfString:#"`" withString:#"'"];
It finds no occurrences of a tilde, and replaces nothing. I've also used rangeOfString to search for tildes and it returns nothing. I'm 100% sure the character, at least as it's entered in iTunes, is a tilde.
Also, when printing a description of the string to the console, the character is encoded as \u00b4, and when hovering over the variable it appears as a period ..
Anyone know how I can grab this character and get it out of there? Also, isn't there a way in objective C to more easily clean up the string to make sure it's safe to send over a socket?
EDIT:
Also something that might be useful, to write the NSString to the NSOutputString I use the following line of code:
len = [oStream write:[[writeString dataUsingEncoding:NSASCIIStringEncoding] bytes] maxLength:[writeString lengthOfBytesUsingEncoding:NSASCIIStringEncoding]];
EDIT #2:
This line of code works to replace the Tilde, but I'm sure there are other characters I should be worrying about:
safedevicename = [safedevicename stringByReplacingOccurrencesOfString:#"\\u00b4" withString:#"'"];
Jason's comment was the correct answer: I needed to change the encoding from NSASCIIStringEncoding to NSUTF8StringEncoding.