iCloud:我可以忽略那些禁用iCloud? [英] iCloud: can I ignore those who disable iCloud?

查看:545
本文介绍了iCloud:我可以忽略那些禁用iCloud?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对iCloud的想法感到困惑,并提出了一个更一般的问题尽可能不给出答案。让我们假设我有一个应用程序处理不同的txt文件。一旦我开始我的应用程序,我只是检查如果任何txt文件是在云中,如此:

   - (BOOL)应用程序: (UIApplication *)应用程序didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{
NSLog(@AppDelegate:app did finish launch launch);
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];

self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];

//(1)iCloud:init

NSURL * ubiq = [[NSFileManager defaultManager] URLForUbiquityContainerIdentifier:nil];
if(ubiq){
NSLog(@用户已启用iCloud!让我们从云中获取txt文件。
[self loadDocument];
} else {
NSLog(@用户没有启用iCloud,因此App是没有价值的。
}


return YES;
}

然后我有一个方法来检查云中是否有txt文件如果是,加载它们。如果没有,我只是在云中创建新的txt文件。



这意味着应用程序不会在文档文件夹中存储任何数据。据我所了解,一切都在我的设备的本地iCloud存储(如果用户离线也可访问)或在云中。所以文本文件存在于两个地方:在我的设备和云上。



因此,根本不需要在我的本地文档文件夹中存储第三个副本, 对?还是由于某种原因,我忽略了这是必要的?换句话说,如果我向我的用户提供iCloud,我应该如何使用本地文档文件夹? (我可以忽略不注册iCloud的用户吗?)






要澄清,当我在应用程序的沙盒中的标准文档文件夹时,我的意思是这一个:

  NSArray *路径= NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES); 
NSString * documentsDirectory = [paths objectAtIndex:0];


解决方案

没有理由将文档存储在本地存储以及在iCloud中。但是,您应该给用户选择关闭iCloud存储。关闭iCloud存储,您应该只在本地存储中查找文件(与iOS5之前的应用程序一样)。最好的办法是尝试隔离需要知道文档存储位置的代码部分,并测试iCloud是否可用并启用,并让该代码返回应存储文档的URL。 / p>

更新:
如果你想分歧iOS5和iOS4,你只需要测试iOS5的功能是否存在。有几种方法可以做到这一点。一种方法是检查:



if([UIDocument class] == nil)



在iOS4这将是真的,在iOS5这将是假的。我不知道你的文件有什么样的数据结构,但你可以做的是创建一个包装UIDocument。在这个包装类中,你可以有一个UIDocument结构的实例变量以及你在IOS4中需要的字段(例如文件的路径)。当您实例化类时,测试是否启用iCloud,如果UIDocument可用,并且如果可用,请使用它并设置字段。否则,设置其他字段,并将UIDocument字段保留为nil。当你需要对你的文件做操作时,测试UIDocument字段是否为nil,如果它是做旧的方式。否则,只需将请求传递给UIDocument对象。


I'm struggling a bit with the idea of iCloud and posted a more general question here. My biggest problem is to decide whether I should stop putting the user's data in the good old documents folder which is found in the app's sandbox. To illustrate my problem:

The docs don't give an answer as far as I can see. Let's suppose I have an App which handles different txt files. Once I start my app, I simply check if any txt files are in the cloud like so:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    NSLog(@"AppDelegate: app did finish launching");
    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];

    self.window.rootViewController = self.viewController;
    [self.window makeKeyAndVisible];

    // (1) iCloud: init

    NSURL *ubiq = [[NSFileManager defaultManager] URLForUbiquityContainerIdentifier:nil];
    if (ubiq) {
        NSLog(@"User has iCloud enabled! Let's get the txt files from the cloud.");
        [self loadDocument];
    } else {
        NSLog(@"User doesn't have iCloud enabled. App is thus worthless.");
    }


    return YES;
}

I then have a method to check if there are any txt files in the cloud and if so, load them. If not, I simply create new txt files in the cloud.

This means that the app does not store any data in the documents folder. As far as I understand it, everything is either in the local iCloud storage of my device (which is also accessible if the user is OFFLINE) or in the cloud. So the text file exists in two places: on my device and the cloud.

So there is simply no need to store a third copy in my local documents folder, right? Or is this essential for some reason I have overlooked? In other words, for what should I use the local documents folder if I offer iCloud to my users? (And can I simply ignore those who won't sign up for iCloud?)


EDIT: Just to clarify, when I'm talking about the standard documents folder in the app's sandbox, I mean this one:

NSArray *paths =NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];

解决方案

There is no reason to store documents both in local storage as well as in iCloud. However, you should give users the option of turning iCloud storage off. With iCloud storage off, you should only look for files in local storage (as with pre-iOS5 apps). The best thing is to try to isolate the part of your code that needs to know where documents are stored, and have it test whether or not iCloud is available and enabled and have that piece of code return the URL where documents should be stored.

Update: If you want to diverge for iOS5 vs. iOS4, you just need to test if the iOS5 features are there. There are several ways that you can do this. One way is to check:

if ([UIDocument class] == nil)

On iOS4 this will be true, and on iOS5 this will be false. I don't know what kind of datastructures you have for your files, but one thing you could do is create a wrapper around UIDocument. Inside this wrapper class, you could have instance variables for a UIDocument structure as well as for fields you would need in IOS4 (such as the path to the file). When you instantiate your class, test whether iCloud is enabled and if UIDocument is available, and if so, use it and set the field. Otherwise, set the other fields and leave the UIDocument field to be nil. When you need to do operations on your "file", test if the UIDocument field is nil, and if it is do it the "old" way. Otherwise, just pass on the request to the UIDocument object.

这篇关于iCloud:我可以忽略那些禁用iCloud?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆