2016-11-11 6 views
5

Я пытаюсь синхронизировать Realm в мое приложение с ICloud/CloudKit, но без успеха ...Не удается синхронизировать Realm с ICloud

Здесь мой исходный код:

-(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); 
             } 
            }]; 
    } 
} 

Когда я пытаюсь EXEC

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

Я получаю эту ошибку

предоставленному URL (http://myMacBookNwtworkIP:9080) не был действительным URL-адресом Realm.

Как это можно решить?

Кроме того, поскольку это моя первая попытка сохранить что-то в iCloud, как мне использовать iCloudCredential? В самом деле, если я заменю usernameCredential с ним, то user всегда ноль ...

Я последовал шаги в этих ссылок:

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

ответ

3

Чтобы предотвратить аварию , необходимо указать протокол «царство» вместо «http», как показано ниже

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

this link

Смежные вопросы