Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't sync Realm with iCloud

I'm trying to sync the Realm in my app with iCloud / CloudKit, but without success...

Here it is my source code:

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

When I try to exec

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

I get this error

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

How can I solve it?

Also, as this is my first try to save something in iCloud, how should I use the iCloudCredential? In fact, if I replace usernameCredential with it, then user is always nil...

I followed steps at these links:

  1. https://realm.io/docs/objc/latest/#the-realm-mobile-platform
  2. https://realm.io/docs/realm-object-server/#authentication
like image 906
ddb Avatar asked Oct 18 '22 22:10

ddb


1 Answers

To prevent the crash, it is necessary to specify the "realm" protocol instead of "http", like below

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

thanks to this link

like image 71
ddb Avatar answered Oct 30 '22 02:10

ddb