简体   繁体   English

将NSDictionary写入ONLINE .plist。 可能吗?

[英]Write NSDictionary to ONLINE .plist. Is it possible?

I'm writing an app for instant messaging aaand I'm stuck. 我正在编写一个用于即时通讯的应用程序,但遇到了麻烦。

I am able to read data (dictionary) from plist that's on my dropbox, but I can't modify it from my app, what is a thing I actually want to achieve. 我可以从我的保管箱上的plist读取数据(字典),但是我无法从我的应用程序修改它,这实际上是我想要实现的目标。

Here is how I read the online .plist file: 这是我阅读在线.plist文件的方式:

@Implementation
NSDictionary *wholePlist;
-(void)viewDidLoad
{
     wholePlist = [[NSDictionary alloc]initWithContentsOfURL: [NSURL URLWithString:[NSString stringWithFormat:@"https://dl.dropboxusercontent.com/s/cfpree9see19t00/users.plist"]]];

     self.allUsers = [wholePlist objectForKey:@"allUsers"];
} //self.allUsers is NSDictionary, also.

And this is how I am trying to save it if I change it 这就是我尝试保存它的方式

- (IBAction)registerButtonPressed:(UIButton *)sender {

    NSString *username = self.usernameTextField.text;
    NSString *password = self.setPassTextField.text;
    NSMutableArray *myContacts = [[NSMutableArray alloc]init];
    NSMutableArray *inbox = [[NSMutableArray alloc]init];

    NSDictionary *user = [[NSDictionary alloc]initWithObjects:@[username, password, myContacts, inbox] forKeys:@[@"username",@"pass",@"myContacts",@"inbox"]];

    if ([user isEqualToDictionary:[self.allUsers objectForKey:username]]) {
        [[[UIAlertView alloc]initWithTitle:@"Registration error" message:@"Username already taken. Please, choose another username." delegate:self cancelButtonTitle:nil otherButtonTitles:@"OK", nil]show];
    } else {
        NSURL *plistURL = [NSURL URLWithString:[NSString stringWithFormat:@"https://dl.dropboxusercontent.com/s/cfpree9see19t00/users.plist"]];
        [self.allUsers setValue:user forKey:username];
        [self.allUsers writeToURL:plistURL atomically:YES];
    }
}

If I do it locally/offline (in some folder inside my Mac or app directory) using writeToFile: it works. 如果我使用writeToFile:在本地/离线(在Mac或应用程序目录中的某个文件夹中)执行此操作, writeToFile:它将起作用。 When I use writeToURL: it doesn't work. 当我使用writeToURL:它不起作用。

My questions are: 我的问题是:

  1. Is this even possible, what am I trying to achieve? 这是否有可能,我想达到什么目标?
  2. Is it possible with any other storage client? 其他存储客户端是否可能?
  3. If it's possible with some other storage client, please give me source link on how to OR explain how to. 如果可以使用其他一些存储客户端,请给我源链接,以了解如何或说明如何使用。

Thanks! 谢谢!

Instant messaging applications are almost always best done using sockets . 即时消息应用程序几乎总是最好使用sockets来完成。 I'd HIGHLY recommend against using a file on a server to read and write from. 强烈建议您不要使用服务器上的文件进行读写。 While it's possible, you're asking for a world of pain and slugish-ness. 尽管有可能,但您正在寻求一个痛苦与迟钝的世界。

So to answer your questions in a striaght forward manner: 因此,请以直截了当的方式回答您的问题:

  1. Yes... Don't do it. 是的,不要这样做。
  2. Yes. 是。 of course you can use CloudKit either the DB or file upload part. 当然,您可以在数据库或文件上传部分中使用CloudKit。 Again, I recommend against this method because it's slow and has high overhead on the network. 再次,我建议您反对这种方法,因为它速度慢且网络开销大。
  3. I highly recommend reading up on sockets: http://en.wikipedia.org/wiki/Network_socket to better understand this approach. 我强烈建议您阅读套接字: http : //en.wikipedia.org/wiki/Network_socket,以更好地理解这种方法。 I have a chat socket written in C++. 我有一个用C ++编写的聊天套接字。 However it does a bit more than what you may need: https://github.com/theMonster/ModularServer . 但是,它的功能超出您的需求: https : //github.com/theMonster/ModularServer Also, there's a very popular chat server example for node.js here: http://socket.io/get-started/chat/ 另外,这里有一个非常流行的node.js聊天服务器示例: http : node.js

Let me know if you have any questions. 如果您有任何疑问,请告诉我。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM