Ios 如何通过“打开应用程序后读取txt文件或获取文件路径”;在“中打开”;在swift中列出?

Ios 如何通过“打开应用程序后读取txt文件或获取文件路径”;在“中打开”;在swift中列出?,ios,Ios,我想读取txt文件并从其他应用程序(例如:电子邮件)获取文件路径 例如: 用户向我发送了一封电子邮件,附件是一个txt文件。我想打开txt文件并从我的应用程序中读取它 我参考以下链接: 当我从电子邮件中单击txt文件(附件)时,我的应用程序将显示在“打开…”菜单上。当我选择使用我的应用程序打开txt文件时,应用程序将打开 但是如何在应用程序打开后读取txt文件并获取文件路径?该文件将存储在应用程序文档目录的收件箱文件夹中,您可以按如下方式搜索文档目录: NSString *searchFile

我想读取txt文件并从其他应用程序(例如:电子邮件)获取文件路径

例如: 用户向我发送了一封电子邮件,附件是一个txt文件。我想打开txt文件并从我的应用程序中读取它

我参考以下链接:

当我从电子邮件中单击txt文件(附件)时,我的应用程序将显示在“打开…”菜单上。当我选择使用我的应用程序打开txt文件时,应用程序将打开


但是如何在应用程序打开后读取txt文件并获取文件路径?

该文件将存储在应用程序文档目录的收件箱文件夹中,您可以按如下方式搜索文档目录:

NSString *searchFilename = @"hello.txt"; // name of the txt file you are searching for

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
 NSString *inboxFolderPath = [documentsDirectory stringByAppendingPathComponent:@"Inbox"];
NSDirectoryEnumerator *direnum = [[NSFileManager defaultManager] enumeratorAtPath: inboxFolderPath];

NSString *documentsSubpath;
while (documentsSubpath = [direnum nextObject])
{
  if (![documentsSubpath.lastPathComponent isEqual:searchFilename]) {
    continue;
  }
  NSLog(@"found %@", documentsSubpath);
}
NSError *error;
NSURL *fileURL = launchOptions[UIApplicationLaunchOptionsURLKey];
NSString *myText = [NSString stringWithContentsOfURL:fileURL
                                            encoding:NSUTF8StringEncoding
                                               error:&error];

如果您不知道文件名,可以通过将documentsSubpath.lastPathComponent添加到tableview上显示的数组来显示文件名列表,该文件将存储在app document directory中的收件箱文件夹中,您可以按如下方式搜索文档目录:

NSString *searchFilename = @"hello.txt"; // name of the txt file you are searching for

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
 NSString *inboxFolderPath = [documentsDirectory stringByAppendingPathComponent:@"Inbox"];
NSDirectoryEnumerator *direnum = [[NSFileManager defaultManager] enumeratorAtPath: inboxFolderPath];

NSString *documentsSubpath;
while (documentsSubpath = [direnum nextObject])
{
  if (![documentsSubpath.lastPathComponent isEqual:searchFilename]) {
    continue;
  }
  NSLog(@"found %@", documentsSubpath);
}
NSError *error;
NSURL *fileURL = launchOptions[UIApplicationLaunchOptionsURLKey];
NSString *myText = [NSString stringWithContentsOfURL:fileURL
                                            encoding:NSUTF8StringEncoding
                                               error:&error];

如果您不知道文件名,可以通过将documentsSubpath.lastPathComponent添加到在tableview上显示的数组来显示文件名列表。您的解决方案似乎很好,请尝试从
didFinishLaunchingWithOptions:(NSDictionary*)中查找
launchOptions
从其他应用打开时,在AppDelegate中启动选项

应用程序是沙盒的,这意味着应用程序不能相互访问(共享同一组的应用程序除外)

但是… 你也要看看

基本上,当您收到电子邮件时,长按附件会提示您一个操作对话框,您可以在其中共享、打印和执行各种操作。您将能够在该列表中显示您自己的应用程序以及应用程序扩展

您可能需要查看整个,因为它非常完整和精确。也许你会发现很多意想不到的事情


然而, 我建议您继续使用自己的解决方案
didFinishLaunchingWithOptions:
用于了解应用程序何时启动以及如何启动(通过通知等)

uiapplicationaunchoptionsurlkey
包含指定要打开的文件的NSURL对象。 如果存在
UIApplicationLaunchOptionsURLKey
键,则应用程序必须打开该键引用的文件并立即显示其内容。
您可能应该这样做:

NSString *searchFilename = @"hello.txt"; // name of the txt file you are searching for

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
 NSString *inboxFolderPath = [documentsDirectory stringByAppendingPathComponent:@"Inbox"];
NSDirectoryEnumerator *direnum = [[NSFileManager defaultManager] enumeratorAtPath: inboxFolderPath];

NSString *documentsSubpath;
while (documentsSubpath = [direnum nextObject])
{
  if (![documentsSubpath.lastPathComponent isEqual:searchFilename]) {
    continue;
  }
  NSLog(@"found %@", documentsSubpath);
}
NSError *error;
NSURL *fileURL = launchOptions[UIApplicationLaunchOptionsURLKey];
NSString *myText = [NSString stringWithContentsOfURL:fileURL
                                            encoding:NSUTF8StringEncoding
                                               error:&error];

我找到了所有内容。

您的解决方案似乎不错,请尝试从
查看您的
launchOptions
,当从另一个应用程序打开时,使用选项:(NSDictionary*)在您的应用程序中启动选项

应用程序是沙盒的,这意味着应用程序不能相互访问(共享同一组的应用程序除外)

但是… 你也要看看

基本上,当您收到电子邮件时,长按附件会提示您一个操作对话框,您可以在其中共享、打印和执行各种操作。您将能够在该列表中显示您自己的应用程序以及应用程序扩展

您可能需要查看整个,因为它非常完整和精确。也许你会发现很多意想不到的事情


然而, 我建议您继续使用自己的解决方案
didFinishLaunchingWithOptions:
用于了解应用程序何时启动以及如何启动(通过通知等)

uiapplicationaunchoptionsurlkey
包含指定要打开的文件的NSURL对象。 如果存在
UIApplicationLaunchOptionsURLKey
键,则应用程序必须打开该键引用的文件并立即显示其内容。
您可能应该这样做:

NSString *searchFilename = @"hello.txt"; // name of the txt file you are searching for

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
 NSString *inboxFolderPath = [documentsDirectory stringByAppendingPathComponent:@"Inbox"];
NSDirectoryEnumerator *direnum = [[NSFileManager defaultManager] enumeratorAtPath: inboxFolderPath];

NSString *documentsSubpath;
while (documentsSubpath = [direnum nextObject])
{
  if (![documentsSubpath.lastPathComponent isEqual:searchFilename]) {
    continue;
  }
  NSLog(@"found %@", documentsSubpath);
}
NSError *error;
NSURL *fileURL = launchOptions[UIApplicationLaunchOptionsURLKey];
NSString *myText = [NSString stringWithContentsOfURL:fileURL
                                            encoding:NSUTF8StringEncoding
                                               error:&error];

我找到了所有东西。

你把文本文件存放在哪里了?@iAnurag我还没有把文本文件存放起来。所以…在我阅读文本文件之前,我应该将该文件存储到我的应用程序文件夹中吗?是的,您应该首先将其存储在文档中directory@iAnurag如何做?谢谢。谷歌搜索……你会很容易找到的。你在哪里存储了文本文件?@iAnurag我还没有存储文本文件。所以…在我阅读文本文件之前,我应该将该文件存储到我的应用程序文件夹中吗?是的,您应该首先将其存储在文档中directory@iAnurag如何做?谢谢。谷歌…你会很容易找到的。你是说文件存储在电子邮件文件夹中,我只需要通过我的应用程序在电子邮件中搜索文件?但电子邮件是接收文本文件的一种方式,它可能是谷歌驱动程序、dropbox。。。因此,当所有文本被按下时,我将我的应用程序添加到“打开”列表中。所以…我需要知道如何在我的应用程序上读取或获取文本文件。不只是在文件夹中搜索电子邮件。你是说文件存储在电子邮件文件夹中,我只需要通过我的应用程序在电子邮件中搜索文件?但电子邮件是接收文本文件的一种方式,它可能是谷歌驱动程序、dropbox。。。因此,当所有文本被按下时,我将我的应用程序添加到“打开”列表中。所以…我需要知道如何在我的应用程序上读取或获取文本文件。不仅仅是在文件夹中搜索电子邮件。感谢您的回复。我知道应用程序是沙盒的,所以…我应该先保存应用程序的附件吗?如果我使用
didfishlaunchingwithoptions
,如何读取或获取文本文件?感谢您的回复。我知道应用程序是沙盒的,所以…我应该先保存应用程序的附件吗?如果使用
didfishlaunchingwithoptions
,如何读取或获取文本文件?