简体   繁体   中英

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.

Here is how I read the online .plist file:

@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. When I use writeToURL: it doesn't work.

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 . 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. 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. I have a chat socket written in C++. However it does a bit more than what you may need: https://github.com/theMonster/ModularServer . Also, there's a very popular chat server example for node.js here: http://socket.io/get-started/chat/

Let me know if you have any questions.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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