博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
iCloud 学习笔记
阅读量:4071 次
发布时间:2019-05-25

本文共 2743 字,大约阅读时间需要 9 分钟。

iCloud 学习笔记

1.准备新的mobileprovision,不能用通配符,并打开iCloud功能,然后在工程里Summary中选中iCloud,并编辑entitlements文件。

2.在后台线程中执行NSURL *urlUC = [[NSFileManager defaultManager] URLForUbiquityContainerIdentifier:nil]; 注意只有真机才行,模拟器不行。

测试环境,打开网页:https://developer.icloud.com/#documents

大概的测试代码如下:

- (void)getUbiquityContainerUrl

{

    NSURL *urlBase = [[NSFileManagerdefaultManager] URLForUbiquityContainerIdentifier:nil];

   if (urlBase == nil)

        return; // 未激活iCloud或模拟器环境

    NSDirectoryEnumerator *dirEnumerater = [[NSFileManagerdefaultManager] enumeratorAtPath:[selfgetDefaultDir]];

NSString *filePath = nil;

while((filePath = [dirEnumerater nextObject]) != nil) {

        NSRange rPosPath = [filePath rangeOfString:@"/"];

        if (rPosPath.length == 0) {

            NSURL *urlFile = [NSURL URLWithString:filePath relativeToURL:urlBase];

            

            // 判断iCloud里该文件是否存在

            if ([[NSFileManagerdefaultManager] isUbiquitousItemAtURL:urlFile]) {

                // 存在则修改

                if (![[NSStringstringWithContentsOfFile:[[selfgetDefaultDir] stringByAppendingPathComponent:filePath] encoding:NSUTF8StringEncodingerror:nil] writeToURL:urlFile atomically:YES])

                {

                    SQTRACE(@"存在则修改");

                }

                

                SQTRACE(@"上传成功");

                

                continue;

            }

            

            // 上传至iCloud

            // 指定本地文件完整路径

            NSURL *url = [NSURLfileURLWithPath:[[selfgetDefaultDir] stringByAppendingPathComponent:filePath]];

            NSError *error;

            // 官方文档建议本方法不要在主进程里执行

            if (![[NSFileManagerdefaultManager] setUbiquitous:YESitemAtURL:url destinationURL:urlFile error:&error])

            {

                NSLog(@"setUbiquitous error %@,\n%@", error, [error userInfo]);

                SQTRACE(@"上传失败");

                continue;

            } else {

                SQTRACE(@"上传成功");

            }// end of iCloud upload

        }

    } // end of while dir enumerator

}

 

- (void)testCloud

{

    [selfperformSelectorInBackground:@selector(getUbiquityContainerUrl) withObject:nil];

}

 

// 此方法是官方文档提供,用来检查文件状态并下载

- (BOOL)downloadFileIfNotAvailable:(NSURL*)file

{

    NSNumber isIniCloud = nil;

    

    if ([file getResourceValue:&isIniCloud forKey:NSURLIsUbiquitousItemKey error:nil]) {

        // If the item is in iCloud, see if it is downloaded.

        if ([isIniCloud boolValue]) {

            NSNumber isDownloaded = nil;

            if ([file getResourceValue:&isDownloaded forKey:NSURLUbiquitousItemIsDownloadedKey error:nil]) {

                if ([isDownloaded boolValue])

                    return YES;

                

                // Download the file.

                NSFileManager fm = [NSFileManager defaultManager];

                if (![fm startDownloadingUbiquitousItemAtURL:file error:nil]) {

                    return NO;

                }

                return YES;

            }

        }

    }

    

    // Return YES as long as an explicit download was not started.

    returnYES;

}

Documents中设置某些文件不去同步方法:

 

Documents路径里的文件会被默认同步到iCloud中(如果启用了iCloud功能)

Documents路径里,文件夹名以.nosync结尾的,将不被同步

如: Documents/mydata.nosync/ 该路径下的东西不被同步

Using Core Data with iCloud Release Notes 里有部分提到

如何忽略Documents文件夹里面的某些文件或文件夹:

 

 Documents路径里的文件会被默认同步到iCloud中(如果启用了iCloud功能)

 Documents路径里,文件夹名以.nosync结尾的,将不被同步

 如: Documents/mydata.nosync/ 该路径下的东西不被同步

 Using Core Data with iCloud Release Notes 里有部分提到

转载地址:http://yseji.baihongyu.com/

你可能感兴趣的文章
solaris10下vnc的安装
查看>>
sqlserver 2005导出数据为sql的办法
查看>>
Solaris Sparcv9下jdk64位的安装
查看>>
容易忽视但是功能灰常强大的Java API
查看>>
转来的一篇关于hibernate的查询资料,很不错
查看>>
httpclient3.1下的请求头和params
查看>>
java中的image 的一些概念
查看>>
easyui中不显示出来的一个问题
查看>>
httpclient自动执行http的302重定向
查看>>
真正最正确的用BAT运行JAVA不显示DOS窗口(连闪一下都不闪)
查看>>
把log4j的日志信息打印到swing组件上
查看>>
把log4j的日志信息打印到swing组件上
查看>>
ext中对Gridpanel的理解
查看>>
ext中在panel中动态添加一个panel
查看>>
修改extjs中表格的默认高度
查看>>
ext中的reader何proxy
查看>>
ext中的formpanel嵌套出现的问题
查看>>
最近状况
查看>>
winform中的状态栏,以及在状态栏目上显示时间
查看>>
给Winform添加登陆的form
查看>>