开发者

UIFileSharingEnabled juste save files

In my iOS app I w开发者_JAVA百科ould like that user can download some jpg file via iTunes. So I've enabled UIFileSharingEnabled. But users are now able to put files in my app. I would like to block that. Is there a way to do that ?

Thanks !


Don't think you can block it, but you can just delete unwanted files when your app becomes active.

Put some code a bit like the sample below - filling in the test to avoid deleting the files you want to be available in iTunes.

Call this from within applicationDidBecomeActive: in your application delegate.

If you're more cautious you might want to check the user hasn't dropped a jpg file with the same name as the one you've parked there. You could test for sameness of date or some such or, if you've not got many files, just delete everything and write them again when the app becomes active.

- (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]);
        }
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜