iphone - UIFileSharingEnabled 只保存文件

标签 iphone ios cocoa itunes file-sharing

在我的 iOS 应用程序中,我希望用户可以通过 iTunes 下载一些 jpg 文件。所以我启用了 UIFileSharingEnabled。但用户现在可以将文件放入我的应用程序中。我想阻止它。有办法做到这一点吗?

谢谢!

最佳答案

不要认为您可以阻止它,但您可以在应用程序激活时删除不需要的文件。

放置一些类似于下面示例的代码 - 填写测试以避免删除您希望在 iTunes 中可用的文件。

从应用程序委托(delegate)中的 applicationDidBecomeActive: 中调用此函数。

如果您更加谨慎,您可能需要检查用户是否删除了与您停放的文件同名的 jpg 文件。您可以测试日期或类似的内容是否相同,或者,如果您没有很多文件,只需删除所有内容,然后在应用程序激活时重新写入它们即可。

- (void) removeUnwantedFiles;
{    
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];

    NSArray* directoryContents = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:inboxPath error:NULL];

    if (!directoryContents || [directoryContents count] == 0)
    {
        return;
    }

    for (NSString* fileName in directoryContents)
    {
        if ( /* some test of filename to see if it's one of my kosher files */ ) continue;

        NSString* filePath = [documentsDirectory stringByAppendingPathComponent:fileName];
        NSError* error = nil;
        BOOL success = [[NSFileManager defaultManager] removeItemAtPath:filePath error:&error];
        // NSLog(@"Deleting (%@): %@", success ? @"succeeded" : @"failed", [filePath lastPathComponent]);
        if (!success)
        {
            NSLog(@"Error: %@", [error localizedDescription]);
        }
    }
}

关于iphone - UIFileSharingEnabled 只保存文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7082247/

相关文章:

cocoa - 通过标签号获取 NSTextField ?

iphone - 显示蓝牙设置页面

macos - 全局禁用工具提示

iPhone Google Chrome - 选项卡选择屏幕(分组的 UIViews)

javascript - Strophe 和 Phonegap

ios - 网页未在 iOS 应用程序中打开

iphone - 使用xcode进行越狱开发

iphone - if(obj && obj != nil) 是否正确且必要?

iphone - 如何通过蓝牙获取iPhone的手机号码?

iphone - Facebook 页面链接在 Facebook 应用程序而不是 Safari 浏览器中打开