ios - 无法将 Realm 与 iCloud 同步

标签 ios objective-c realm icloud cloudkit

我正在尝试将我的应用程序中的 Realm 与 iCloud/CloudKit 同步,但没有成功...

这是我的源代码:

-(void)sync
{
    NSURL *baseURL = [[NSFileManager defaultManager] URLForUbiquityContainerIdentifier:nil];

    if (baseURL) //iCloud is active
    {
        NSURL *serverURL = [NSURL URLWithString:@"http://myMacBookNwtworkIP:9080"]; //Realm Object Server is up and running at this URL
        RLMSyncCredential *usernameCredential = [RLMSyncCredential credentialWithUsername:@"myUsername"
                                                                                 password:@"myPassword"
                                                                                  actions:RLMAuthenticationActionsUseExistingAccount];
        RLMSyncCredential *iCloudCredential = [RLMSyncCredential credentialWithICloudToken:@"myiCloudToken"];

        [RLMSyncUser authenticateWithCredential:usernameCredential
                                  authServerURL:serverURL
                                   onCompletion:^(RLMSyncUser *user, NSError *error) {
                                       if (user) { //user recognized in my real object server
                                           // can now open a synchronized RLMRealm with this user
                                           RLMRealmConfiguration *config = [RLMRealmConfiguration defaultConfiguration];
        /* CRASH AT THIS LINE! */          config.syncConfiguration = [[RLMSyncConfiguration alloc] initWithUser:user realmURL:serverURL];
                                           RLMRealm *realm = [RLMRealm realmWithConfiguration:config error:nil];// Any changes made to this Realm will be synced across all devices!
                                       } else if (error) {
                                           // handle error
                                           NSLog(@"error %@",error);
                                       }
                                   }];
    }
}

当我尝试执行时

config.syncConfiguration = [[RLMSyncConfiguration alloc] initWithUser:user realmURL:serverURL];

我收到这个错误

The provided URL (http://myMacBookNwtworkIP:9080) was not a valid Realm URL.

我该如何解决?

此外,由于这是我第一次尝试在 iCloud 中保存内容,我应该如何使用 iCloudCredential?事实上,如果我用它替换 usernameCredential,那么 user 总是 nil...

我按照这些链接中的步骤操作:

  1. https://realm.io/docs/objc/latest/#the-realm-mobile-platform
  2. https://realm.io/docs/realm-object-server/#authentication

最佳答案

为防止崩溃,有必要指定“realm”协议(protocol)而不是“http”,如下所示

config.syncConfiguration = [[RLMSyncConfiguration alloc] initWithUser:user realmURL:[NSURL URLWithString:@"realm://myMacBookNwtworkIP:9080"]];

感谢this link

关于ios - 无法将 Realm 与 iCloud 同步,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40550287/

相关文章:

java - Android 上的 Junit 测试 Realm 。无法在未调用 Looper.prepare() 的线程内创建处理程序

ios - 有时后台 Realm 会丢失所有数据库

ios - UIView 作为 subview 方向框架问题

iphone - 查看 Xcode 中调用了哪些方法

ios - Swift 将数据从 subview 发送到父 View Controller

ios - 在 iOS 中从文件中获取图像

IOS:管理缓存的项目

ios - 如何使用 cocoapod 包含 ios 应用程序和 watch 扩展的共享框架

ios - 从 AFNetworking 1.3 迁移到 AFNetworking 2.0 的问题

ios - 使用自动完成获取错误的键盘框架