【问题标题】:Enable iCloud Functionalities With the Use of an On-Off Switch in An iOS App, or Similar to the Pages.App使用 iOS 应用程序中的开关或类似于 Pages.App 的开关来启用 iCloud 功能
【发布时间】:2014-08-07 07:03:10
【问题描述】:

我在 App Store 中有一个完全构建的基于 Core Data 的应用程序,并且我正在进行更新,我正在为应用程序带来 iCloud 同步功能。这将是用户启用而不是立即启用的选项。

我有一个UITabBarController,在第 5 个选项卡中,我有一个设置窗格,其中一个选项是通过使用 Switch 来打开或关闭 iCloud 同步。

我不确定如何准确启用这样的功能,因为SettingsViewController(第 5 个选项卡)实际上需要与AppDelegate 对话才能调用它。

AppDelegate 中我的persistentStoreCoordinator 如下:

- (NSPersistentStoreCoordinator *)persistentStoreCoordinator
{
    if (_persistentStoreCoordinator != nil) {
        return _persistentStoreCoordinator;
    }

    NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"Lite.sqlite"];

    NSError *error = nil;

    NSURL *cloudURL = [self grabCloudPath:@"iCloud"];

    NSDictionary *options = nil;
    if ([[NSUserDefaults standardUserDefaults]boolForKey:@"OnLatestVersion"]) {
        options = @{                                       NSMigratePersistentStoresAutomaticallyOption : @YES,
                                                           NSInferMappingModelAutomaticallyOption : @YES,
                                                           NSPersistentStoreUbiquitousContentURLKey: cloudURL, NSPersistentStoreUbiquitousContentNameKey: @"LiteCloud"};
        NSLog(@"Using iCloud from migration");
    }
    else
    {
        NSLog(@"Using the other store without the iCloud Migration");
        options = @{                                       NSMigratePersistentStoresAutomaticallyOption : @YES,
                                                           NSInferMappingModelAutomaticallyOption : @YES};
    }

    _persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];

    if (![_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:options error:&error]) {

        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
        abort();
    }


    return _persistentStoreCoordinator;
}

persistentStore 为何如此,这与这个问题无关,但本质上,我想确保在 SettingsViewController 中,当用户打开 iCloud 开关时,它会调用 iCloud 同步。

我看过很多与关闭 iCloud 功能有关的帖子,但从来没有人谈论过如何做到这一点等。

编辑:小更新

经过一番思考,或许我可以这样做:

  • 从 AppDelegate 的 applicationDidFinishLaunchingWithOptions 方法中取出 storesWill 和 storesDidChange 通知,并将它们放入单独的方法中
  • 在 applicationDidFinishLaunchingWithOptions 中有一个通知监听器(自定义通知)
  • 此自定义通知将在调用时触发这些方法
  • 在 SettingsVC 中,在 Switch 开启 iCloud 时调用自定义通知
  • 让它在 AppDelegate 中执行所需的操作

这是正确的吗?如果是这种情况,我从理论上理解它,但只是不确定如何最好地实现它。

如果这一切都正确,下面的内容是否适合applicationDidFinishLaunchingWithOptions:

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(reloadFetchedResults:)
                                             name:@"SomethingChanged"
                                           object:[[UIApplication sharedApplication] delegate]];

如何实现这样的目标?任何想法将不胜感激。

【问题讨论】:

  • 您是想了解如何管理 iCloud,还是更关心如何在设置视图控制器中获取持久存储协调器?
  • 感谢汤姆的回复。我对如何在我的设置中获取persistentStoreCoordinator 感兴趣,因为我很乐意管理iCloud 是否可用等。我想要实现的基本上是如果在SettingsVC 中打开iCloud 开关,我'我想联系应用程序代表说好的,开始做你的工作..我猜persistentStoreCoordinator 是开始的地方。但是,我在 App Delegate 的 PSC 中没有 storesWill/DidChange 的通知处理程序。我在 didFinishLaunching 中有这些处理程序。所以我想这也需要调整?
  • 我想这会对你有所帮助:stackoverflow.com/questions/7795629/…
  • 谢谢大家的回复。最后的帖子非常有帮助。我感到困惑的是是否使用 NSNotification 或 NSUserDefaults。当用户从 On 切换到 Off 时,我是否应该同时使用 NSNotification 来监听 App Delegate 和 NSUserDefault?

标签: ios uiviewcontroller uitabbarcontroller icloud


【解决方案1】:

设置一个 NSUserDefaults 键,然后使用 NSNotification 检查该键应该可以工作。在设置视图中,当切换开关时,您更改 NSUserDefaults 中的 BOOL 值,然后在 appDelegate 中调用 NSNotification 以根据更改的 NSUserDefaults 值进行更新。在 applicationDidFinishLaunchingWithOptions 中,就像您说的那样,您可以这样做:

[[NSNotificationCenter defaultCenter] addObserver:self
                                     selector:@selector(reloadFetchedResults)
                                         name:@"SomethingChanged"
                                       object:nil];

并且在 reloadFetchedResults 中,您可以检查您在设置视图中设置的 NSUserDefaults 中的键。在更改 NSUserDefaults 中的密钥后立即在设置视图中调用 [[NSNotificationCenter defaultCenter] postNotification: @"SomethingChanged"];

【讨论】:

  • 非常感谢 Eytan - 这绝对是完美的,你的想法就像一个绝对的魅力。这是不应该如此困难的事情,但我真的很感谢你在这里的回答!再次感谢
猜你喜欢
  • 2014-11-07
  • 1970-01-01
  • 2013-06-13
  • 2011-07-31
  • 1970-01-01
  • 2012-02-03
  • 1970-01-01
  • 2014-04-07
  • 2015-10-04
相关资源
最近更新 更多