在我的 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/