如何使用 NSFileManager 保护或加密视频文件?
How to secure or encrypt video files with NSFileManager?
我需要保护 iOS 上文档目录或(库目录)中的文件,尤其是视频。我不希望用户可以通过 Xcode 或 iExplorer 应用程序下载文档目录中的视频。我需要保护库目录中的私有内容(或)如何在从服务器下载后加密视频文件。请帮我解决这个问题。
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@.mp4",videoUrlName]]];
NSString* path = [NSString stringWithFormat:@"%@.mp4",videoName];
AFDownloadRequestOperation *videoDownloadRequest = [[AFDownloadRequestOperation alloc] initWithRequest:request targetPath:path shouldResume:YES];
[videoDownloadRequest setProgressiveDownloadProgressBlock:^(AFDownloadRequestOperation *operation, NSInteger bytesRead, long long totalBytesRead, long long totalBytesExpected, long long totalBytesReadForFile, long long totalBytesExpectedToReadForFile)
{
float progress = totalBytesReadForFile / (float)totalBytesExpectedToReadForFile;
NSString *progressMessage = [NSString stringWithFormat:@"%@%@ / %@", @"Downloading...", [self fileSizeStringWithSize:totalBytesReadForFile], [self fileSizeStringWithSize:totalBytesExpectedToReadForFile]];
}];
[videoDownloadRequest setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject)
{
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Message" message:[NSString stringWithFormat:@"%@ has been added to your Playlist",detailCellView.titleLabel.text] delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
[alertView show];
}
failure:^(AFHTTPRequestOperation *operation, NSError *error)
{
}];
[[NSOperationQueue mainQueue] addOperation:videoDownloadRequest];
您可以使用任何加密算法来加密您的数据。 AES256 是最常用的算法之一。
您可以使用这个使用 AES 的优秀包装器 https://github.com/RNCryptor/RNCryptor
用法也非常简单。
[videoDownloadRequest setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject)
{
NSData *data = [[NSFileManager defaultManager] contentsAtPath:path];
NSString *password = @"Secret password";
NSData *ciphertext = [RNCryptor encryptData:data password:password];
[data writeToFile:path atomically:YES];
}
但请记住,真正的安全取决于您保存密钥的安全程度(即上述情况下的 Secret 密码)
Apple 已阻止从 App Store 下载的应用访问大部分 Apps 目录 Refer this
我们需要在应用提交之前做一件事
https://developer.apple.com/library/ios/qa/qa1719/_index.html
我需要保护 iOS 上文档目录或(库目录)中的文件,尤其是视频。我不希望用户可以通过 Xcode 或 iExplorer 应用程序下载文档目录中的视频。我需要保护库目录中的私有内容(或)如何在从服务器下载后加密视频文件。请帮我解决这个问题。
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@.mp4",videoUrlName]]];
NSString* path = [NSString stringWithFormat:@"%@.mp4",videoName];
AFDownloadRequestOperation *videoDownloadRequest = [[AFDownloadRequestOperation alloc] initWithRequest:request targetPath:path shouldResume:YES];
[videoDownloadRequest setProgressiveDownloadProgressBlock:^(AFDownloadRequestOperation *operation, NSInteger bytesRead, long long totalBytesRead, long long totalBytesExpected, long long totalBytesReadForFile, long long totalBytesExpectedToReadForFile)
{
float progress = totalBytesReadForFile / (float)totalBytesExpectedToReadForFile;
NSString *progressMessage = [NSString stringWithFormat:@"%@%@ / %@", @"Downloading...", [self fileSizeStringWithSize:totalBytesReadForFile], [self fileSizeStringWithSize:totalBytesExpectedToReadForFile]];
}];
[videoDownloadRequest setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject)
{
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Message" message:[NSString stringWithFormat:@"%@ has been added to your Playlist",detailCellView.titleLabel.text] delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
[alertView show];
}
failure:^(AFHTTPRequestOperation *operation, NSError *error)
{
}];
[[NSOperationQueue mainQueue] addOperation:videoDownloadRequest];
您可以使用任何加密算法来加密您的数据。 AES256 是最常用的算法之一。
您可以使用这个使用 AES 的优秀包装器 https://github.com/RNCryptor/RNCryptor
用法也非常简单。
[videoDownloadRequest setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject)
{
NSData *data = [[NSFileManager defaultManager] contentsAtPath:path];
NSString *password = @"Secret password";
NSData *ciphertext = [RNCryptor encryptData:data password:password];
[data writeToFile:path atomically:YES];
}
但请记住,真正的安全取决于您保存密钥的安全程度(即上述情况下的 Secret 密码)
Apple 已阻止从 App Store 下载的应用访问大部分 Apps 目录 Refer this
我们需要在应用提交之前做一件事
https://developer.apple.com/library/ios/qa/qa1719/_index.html