【问题标题】:Can't sync Realm with iCloud无法将 Realm 与 iCloud 同步
【发布时间】:2017-03-25 19:41:12
【问题描述】:

我正在尝试将我的应用中的 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];

我收到此错误

提供的 URL (http://myMacBookNwtworkIP:9080) 不是有效的领域 URL。

我该如何解决?

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

我按照以下链接的步骤操作:

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

【问题讨论】:

    标签: ios objective-c realm icloud cloudkit


    【解决方案1】:

    为了防止崩溃,需要指定“realm”协议而不是“http”,如下所示

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

    感谢this link

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-01-27
      • 2016-06-30
      • 1970-01-01
      • 1970-01-01
      • 2014-12-13
      • 2011-09-29
      • 1970-01-01
      相关资源
      最近更新 更多