简体   繁体   English

从邮件中打开文件

[英]Open Files from mail

I'm struggling with file handling on iOS. 我正在努力处理iOS上的文件。 I could already assign my file type to iOS and I can launch my app from mail with a special file. 我已经可以将文件类型分配给iOS,并且可以从带有特殊文件的邮件中启动应用程序。 My app is launching and I'm firing this method: 我的应用正在启动,并且正在触发此方法:

- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url
{
    if([url isFileURL])
    {
        NSString *fileConts = [[NSString alloc] initWithContentsOfFile:[NSString stringWithFormat:@"%@", url] encoding:NSUTF8StringEncoding error:nil];
        [self.viewController openFile:fileConts];
        fileConts = nil;
    }
    return YES;
}

The openFile:(NSString) method is declared in the viewController and sets the value of a textView (for now). openFile:(NSString)方法在viewController中声明,并设置textView的值(目前)。 This method works fine. 此方法工作正常。 I tested it via [self.viewController openFile:@"test"]; 我通过[self.viewController openFile:@"test"];测试了它[self.viewController openFile:@"test"]; .

But when my application launches with file attached, the textView keeps empty. 但是,当我的应用程序启动并附加了文件时,textView保持为空。 It seems that it doesn't adopt the string value or that it can't read the string value. 似乎它没有采用字符串值或它无法读取字符串值。

handleOpenURL will be called only if application already running (in the background). 仅当应用程序已在后台运行时,才会调用handleOpenURL。

To make sure you correctly dispatch incoming files, you also need to check it on the app launch: 为了确保正确分发传入文件,您还需要在应用启动时对其进行检查:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
NSURL *url = (NSURL *)[launchOptions valueForKey:UIApplicationLaunchOptionsURLKey];
// Process url here
}

It's good idea to have 1 URL dispatcher called both from handleOpenURL and didFinishLaunchingWithOptions. 最好同时从handleOpenURL和didFinishLaunchingWithOptions中调用1个URL调度程序。

I could solve my problem. 我可以解决我的问题。 My mistake was to initWithContentsOfFile:(NSString *) 我的错误是initWithContentsOfFile:(NSString *)

I updated my code with NSString *fileConts = [[NSString alloc] initWithContentsOfURL:url encoding:NSUTF8StringEncoding error:&error]; 我使用NSString *fileConts = [[NSString alloc] initWithContentsOfURL:url encoding:NSUTF8StringEncoding error:&error];更新了代码NSString *fileConts = [[NSString alloc] initWithContentsOfURL:url encoding:NSUTF8StringEncoding error:&error];

Now I'm happy! 现在我很高兴! Thanks for help. 感谢帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM