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

后端 未结 4 658
长发绾君心
长发绾君心 2020-12-18 15:49

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

相关标签:
4条回答
  • 2020-12-18 15:50

    To add another solution, in addition to what Moshe and Lopper have already said, if you want to be able to export the file both local and remote you can use FTP. All windows and Mac machines can be easily turned into a FTP server within minutes. However, it's important not to forget that the FTP native library on iOS doesn't support encryption (FTPS)

    0 讨论(0)
  • 2020-12-18 15:58

    For iOS users, it is the most common way to export text from iOS to PC via iTunes. Backup your iPhone to your computer with iTunes, all the data (messages, contacts) will be contained in the backup file, but it is a unreadable format, you can use Vibosoft iPhone/iPad/iPod backup extractor program to rip this backup file and extract messages from iPhone to computer, you can edit and print them.

    0 讨论(0)
  • 2020-12-18 16:04

    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.

    0 讨论(0)
  • 2020-12-18 16:12

    Just to add to what Moshe has said, I have found additional documentation/code that show how this can be achieved at the following websites.

    • Exporting through iTunes
    • Exporting through email
    0 讨论(0)
提交回复
热议问题