1

I have an Android app that I am porting to iPhone and an important feature requires opening a simple text file that the user downloads. On the Android, the usual way is for the user to get the file as an email attachment, but any way the user can download a file to their iPhone so that my app can open it would work. Is there a way to do this on the iPhone?

Cezar
  • 55,636
  • 19
  • 86
  • 87
Don
  • 85
  • 1
  • 7

1 Answers1

0

I'm not quite sure how you're handling the text file, but the following methods can retrieve the text file from an attached email when the user selects to open the attachment from the Mail app in your App.

First you need to register your app as being able to open text files. To do this, go to your app's info.plist file and add the following section:

<key>CFBundleDocumentTypes</key>
  <array>
      <dict>
        <key>CFBundleTypeName</key>
        <string>Text Document</string>
        <key>CFBundleTypeRole</key>
        <string>Viewer</string>
        <key>LSHandlerRank</key>
        <string>Alternate</string>
        <key>LSItemContentTypes</key>
        <array>
            <string>public.text</string>
        </array>
    </dict>
</array>

This will tell iOS that your app can open text files. Now where-ever there is a button that says, "Open In..." (ex. in Safari or Mail) and the user wants to open a text file, your app will show in the list.

You'll also have to handle the opening of the text file in your AppDelegate:

-(BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{    
if (url != nil && [url isFileURL]) {
    //Removes the un-needed part of the file path so that only the File Name is left
    NSString *newString = [[url absoluteString] substringWithRange:NSMakeRange(96, [[url absoluteString] length]-96)];
    //Send the FileName to the USer Defaults so your app can get the file name later
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setObject:newString forKey:@"fileURLFromApp"];
    //Save the Defaults
    [[NSUserDefaults standardUserDefaults] synchronize];
    //Post a Notification so your app knows what method to fire
    [[NSNotificationCenter defaultCenter] postNotificationName:@"fileURLFromApp" object:nil];
} else {
}
return YES;
}

You'll have to register for that notification in your ViewController.m:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(fileURLFromApp) name:@"fileURLFromApp" object:nil];

Then you can create the method that you need and retrieve the file:

- (void) fileURLFromApp
{   
//Get stored File Path
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *filePath = [defaults objectForKey:@"fileURLFromApp"];
NSString *finalFilePath = [documentsDirectory stringByAppendingPathComponent:filePath];
//Parse the data
//Remove "%20" from filePath
NSString *strippedContent = [finalFilePath stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
//Get the data from the file
NSString* content = [NSString stringWithContentsOfFile:strippedContent encoding:NSUTF8StringEncoding error:NULL];

The above method will give you the contents of the text file in an NSString called content

And that should work! Best of luck!

Sam Spencer
  • 8,492
  • 12
  • 76
  • 133