简体   繁体   中英

Remotely write to a .plist file iPhone SDK

I am wondering if theres a way to remotely write to a plist file stored on my server. The directory the file is located has write access, but i cannot seem to push the new data. Heres my code:

NSString *accountURL = [NSString stringWithFormat:@"http://www.mywebsite.com//%@.plist",txtUsername.text];
NSURL *url = [NSURL URLWithString:accountURL];
NSMutableDictionary *account = [[NSMutableDictionary alloc] initWithContentsOfURL:url];

    [account setObject:@"TEST" forKey:@"LastLogin"];
if ([account writeToURL:url atomically:NO]){
NSLog(@"saved");
}else{
NSLog(@"not saved");
}

The plist file exists because I can read from it with no problem. I just cannot write to the file.

On another note, is this even AppStore-friendly if the accounts will be stored on my server?

You need to parse this web service like this and need to implement the delegate methods of parser.

-(void)startParsingForLoginUser:(NSString*)UserName
{
    NSString *urlString = [NSString stringWithFormat:@"http://www.mywebsite.com//%@.plist",UserName]];
    NSURL *xmlURL = [NSURL URLWithString:urlString];
    NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] initWithURL:xmlURL cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:30.0]autorelease];
    NSURLResponse *returnedResponse = nil;
    NSError *returnedError = nil;
    NSData *itemData  = [NSURLConnection sendSynchronousRequest:request returningResponse:&returnedResponse error:&returnedError];
    xmlParser = [[NSXMLParser alloc] initWithData:itemData];        
    [xmlParser setDelegate:self];
    [xmlParser parse];
}

And then you would be able to write the plist file on server. Moreover, you could also use the method for parsing like POST, PUT and you can also send aSynchronousRequest to the server, that depends upon you.

Read-only is easy. As soon as you get into writing something you get into permissions for: server, application, user.

To solve your problem you need to create a web service that will authenticate the user and application, check permissions and save the file. Typically it would be a server accepting a POST or PUT from your iPhone app (similar to what browsers do for upload)

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