Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
566 views
in Technique[技术] by (71.8m points)

objective c - How do I export text from iOS application to desktop PC?

Lets say I have written an application for iOS which allows the user to enter some text and save it. How can I export this text to be viewed on a desktop PC running windows OS? If this is not possible, what are the alternatives?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can save the text to your app's Documents directory and allow them to export it through iTunes. You can also allow them to email it.

Saving it to disk:

To save in the App's documents directory, you'll need to do a few things. First of all, you'll need to get the URL to the path directory. A method for this is conveniently generated by Xcode when you make a Core Data based project. Here's that method:

- (NSURL *)applicationDocumentsDirectory{
  return [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
}

Next, you'll want to take that that URL and use it to write our to your App's documents directory. In the simples case, we have a string, called someText, which we'll be writing out to the Documents directory. Here's what that looks like:

NSString *someText = "Here's to some awesome text.";

We have a path, called path. Note that we take the path to the documents directory and then append a filename. You could replace someText.txt with whatever filename you want to use.

NSString *path = [NSString stringWithFormat:@"%@",[[[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"someText.txt"] absoluteString]];

We tell the the string to write itself out to the file (in this case atomically) and if it fails, we populate the error object, which we can read out later if necessary. Note the option for "atomically" here. If it's set to YES, the app will write the text into a buffer and rename it afterward. If it's not set to YES, the text will be written straight to text. This makes a difference in multithreaded environments and can protect your text from being some mangled result, but atomic writing is slower.

Here's all of the above code at once:

//Write to the file
[someText writeToFile:path atomically:YES encoding:NSUTF8StringEncoding error:&error];

NSString *someText = "Here's to some awesome text.";

NSError *error = nil;

NSString *path = [NSString stringWithFormat:@"%@",[[[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"someText.txt"] absoluteString]];

//Write to the file
[someText writeToFile:path atomically:YES encoding:NSUTF8StringEncoding error:&error];

Reading the file from iTunes:

This is the fun part. In Xcode, you need to add a key to your app's Info.plist file. Here's what that is going to look like in Xcode 4:

Enabling iTunes sharing

Now, in iTunes, your App's documents directory will be visible.


Alternatively (or in addition to iTunes), you could use the MessageUI framework and allow the user to email the file.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...