我正在尝试将我的应用中的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) 不是有效的Realm URL。
我该如何解决它?
另外,由于这是我第一次尝试在iCloud中保存数据,那我应该如何使用iCloud凭证? 实际上,如果我将用户名凭证替换为它,则用户始终为空...
我按照以下链接中的步骤进行操作: