[英]How can I use iCloud to synchronize a .zip file between my apps?
Is it possible to upload a .zip
file to iCloud
, and then have it be synchronized across all of a user's iOS devices? 是否可以将
.zip
文件上传到iCloud
,然后在所有用户的iOS设备上同步?
If so, how would I go about doing this? 如果是这样,我该怎么做呢?
If there is any File size limit, then also mention max. 如果有任何文件大小限制,那么也提到最大。 file size allowed.
文件大小允许。
This is how I synchronized zip files with iCloud . 这就是我用iCloud同步zip文件的方法。
Steps: 脚步:
1) http://transoceanic.blogspot.in/2011/07/compressuncompress-files-on.html . 1) http://transoceanic.blogspot.in/2011/07/compressuncompress-files-on.html 。 Refer this link to download zip api which is having code for zipping and unzipping folder.
请参阅此链接以下载zip api,其中包含用于压缩和解压缩文件夹的代码。
2) Now all you need to play with NSData. 2)现在您需要使用NSData。
3) "MyDocument.h" file 3)“MyDocument.h”文件
#import <UIKit/UIKit.h>
@interface MyDocument : UIDocument
@property (strong) NSData *zipDataContent;
@end
4) 4)
#import "MyDocument.h"
@implementation MyDocument
@synthesize zipDataContent;
// Called whenever the application reads data from the file system
- (BOOL)loadFromContents:(id)contents ofType:(NSString *)typeName error:(NSError **)outError
{
self.zipDataContent = [[NSData alloc] initWithBytes:[contents bytes] length:[contents length]];
[[NSNotificationCenter defaultCenter] postNotificationName:@"noteModified" object:self];
return YES;
}
// Called whenever the application (auto)saves the content of a note
- (id)contentsForType:(NSString *)typeName error:(NSError **)outError
{
return self.zipDataContent;
}
@end
5) Now somewhere in your app you need to zip folder and sync with icloud. 5)现在你的应用程序的某个地方你需要压缩文件夹并与icloud同步。
-(BOOL)zipFolder:(NSString *)toCompress zipFilePath:(NSString *)zipFilePath
{
BOOL isDir=NO;
NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *pathToCompress = [documentsDirectory stringByAppendingPathComponent:toCompress];
NSArray *subpaths;
NSFileManager *fileManager = [NSFileManager defaultManager];
if ([fileManager fileExistsAtPath:pathToCompress isDirectory:&isDir] && isDir){
subpaths = [fileManager subpathsAtPath:pathToCompress];
} else if ([fileManager fileExistsAtPath:pathToCompress]) {
subpaths = [NSArray arrayWithObject:pathToCompress];
}
zipFilePath = [documentsDirectory stringByAppendingPathComponent:zipFilePath];
//NSLog(@"%@",zipFilePath);
ZipArchive *za = [[ZipArchive alloc] init];
[za CreateZipFile2:zipFilePath];
if (isDir) {
for(NSString *path in subpaths){
NSString *fullPath = [pathToCompress stringByAppendingPathComponent:path];
if([fileManager fileExistsAtPath:fullPath isDirectory:&isDir] && !isDir){
[za addFileToZip:fullPath newname:path];
}
}
} else {
[za addFileToZip:pathToCompress newname:toCompress];
}
BOOL successCompressing = [za CloseZipFile2];
if(successCompressing)
return YES;
else
return NO;
}
-(IBAction) iCloudSyncing:(id)sender
{
//***** PARSE ZIP FILE : Pictures *****
NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
if([self zipFolder:@"Pictures" zipFilePath:@"iCloudPictures"])
NSLog(@"Picture Folder is zipped");
ubiq = [[NSFileManager defaultManager]URLForUbiquityContainerIdentifier:nil];
ubiquitousPackage = [[ubiq URLByAppendingPathComponent:@"Documents"] URLByAppendingPathComponent:@"iCloudPictures.zip"];
mydoc = [[MyDocument alloc] initWithFileURL:ubiquitousPackage];
NSString *zipFilePath = [documentsDirectory stringByAppendingPathComponent:@"iCloudPictures"];
NSURL *u = [[NSURL alloc] initFileURLWithPath:zipFilePath];
NSData *data = [[NSData alloc] initWithContentsOfURL:u];
// NSLog(@"%@ %@",zipFilePath,data);
mydoc.zipDataContent = data;
[mydoc saveToURL:[mydoc fileURL] forSaveOperation:UIDocumentSaveForCreating completionHandler:^(BOOL success)
{
if (success)
{
NSLog(@"PictureZip: Synced with icloud");
}
else
NSLog(@"PictureZip: Syncing FAILED with icloud");
}];
}
6) You can unzip data received from iCloud like this. 6)您可以解压缩从iCloud收到的数据。
- (void)loadData:(NSMetadataQuery *)queryData {
for (NSMetadataItem *item in [queryData results])
{
NSString *filename = [item valueForAttribute:NSMetadataItemDisplayNameKey];
NSURL *url = [item valueForAttribute:NSMetadataItemURLKey];
MyDocument *doc = [[MyDocument alloc] initWithFileURL:url];
if([filename isEqualToString:@"iCloudPictures"])
{
[doc openWithCompletionHandler:^(BOOL success) {
if (success) {
NSLog(@"Pictures : Success to open from iCloud");
NSData *file = [NSData dataWithContentsOfURL:url];
NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *zipFolder = [documentsDirectory stringByAppendingPathComponent:@"Pics.zip"];
[[NSFileManager defaultManager] createFileAtPath:zipFolder contents:file attributes:nil];
//NSLog(@"zipFilePath : %@",zipFolder);
NSString *outputFolder = [documentsDirectory stringByAppendingPathComponent:@"Pictures"];//iCloudPics
ZipArchive* za = [[ZipArchive alloc] init];
if( [za UnzipOpenFile: zipFolder] ) {
if( [za UnzipFileTo:outputFolder overWrite:YES] != NO ) {
NSLog(@"Pics : unzip successfully");
}
[za UnzipCloseFile];
}
[za release];
NSError *err;
NSArray *files = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:outputFolder error:&err];
if (files == nil) {
NSLog(@"EMPTY Folder: %@",outputFolder);
}
// Add all sbzs to a list
for (NSString *file in files) {
//if ([file.pathExtension compare:@".jpeg" options:NSCaseInsensitiveSearch] == NSOrderedSame) {
NSLog(@" Pictures %@",file);
// NSFileManager *fm = [NSFileManager defaultManager];
// NSDictionary *attributes = [fm fileAttributesAtPath:[NSString stringWithFormat:@"%@/%@",documentsDirectory,file] traverseLink:NO];
//
// NSNumber* fileSize = [attributes objectForKey:NSFileSize];
// int e = [fileSize intValue]; //Size in bytes
// NSLog(@"%@__%d",file,e);
}
}
else
{
NSLog(@"Pictures : failed to open from iCloud");
[self hideProcessingView];
}
}];
}
}
}
In order to enabling Document storage in iCloud your "document" needs to be encapsulated in a UIDocument object. 为了在iCloud中启用文档存储,您的“文档”需要封装在UIDocument对象中。
Because UIDocument
links to a file URL, you can easily create a UIDocument
pointing to file://myzipfile.zip
and then upload a zip document to iCloud . 由于
UIDocument
链接到文件的URL,你可以轻松地创建一个UIDocument
指向file://myzipfile.zip
,然后上传一个zip文件到iCloud。
I hope this helps 我希望这有帮助
Probably this tutorial can help you more: 可能本教程可以为您提供更多帮助:
http://www.raywenderlich.com/6015/beginning-icloud-in-ios-5-tutorial-part-1 http://www.raywenderlich.com/6015/beginning-icloud-in-ios-5-tutorial-part-1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.