1

i am using plist to save my data locally when network connection is not available, and when network is available i want to sync my locally saved data to the web server.  

i am using this code. but i dont know whether the data is saved in server or not. 

NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration]; AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration];

NSURL *URL = [NSURL URLWithString:@"http://example url"];
NSURLRequest *request = [NSURLRequest requestWithURL:URL];

NSURL *filePath = [NSURL fileURLWithPath:@"file://my plist file"];
NSURLSessionUploadTask *uploadTask = [manager uploadTaskWithRequest:request fromFile:filePath progress:nil completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {
    if (error) {
        NSLog(@"Error: %@", error);
    } else {
        NSLog(@"Success: %@ %@", response, responseObject);
    }
}];
[uploadTask resume];

i am assigning my api to the URL in which i want to save the data of plist, and my plist to the filepath in which my data is already saved. can anyone solve this issue.

  • Did you implement any code that continuously check for internet availability even on background? First do that, uploading is the second thing. Look for the answer here. https://stackoverflow.com/questions/38691583/detecting-internet-connectivity-continually and then add the uploading code that you might be using in your project in the ReachableViaWWAN or ReachableViaWiFi case. – Jamil Jul 10 '17 at 05:36
  • yes of course i have implemented code for checking network connection – Charishma Vasamsetti Jul 10 '17 at 05:47
  • Then include the afnetworking via pods and look for the uploading snippet under afnetworking documentation. Replace the file name with your plist. – Jamil Jul 10 '17 at 05:49
  • can you please give an example code. – Charishma Vasamsetti Jul 10 '17 at 05:52
  • Look under 'Creating an upload task' in https://github.com/AFNetworking/AFNetworking#usage – Jamil Jul 10 '17 at 06:01
  • my project getting crash and the reason is "[NSURL initFileURLWithPath:]: nil string parameter" – Charishma Vasamsetti Jul 11 '17 at 07:01

1 Answers1

0

I think you use normal API Call to stored pList file to a server. Becuase when you read plist file, your will data in NSDictionary and you send it server to store.

To read value from plist:

NSString* plistPath = [[NSBundle mainBundle] pathForResource:@"YOUR_PLIST_NAME" ofType:@"plist"];
NSDictionary *dicData = [NSDictionary dictionaryWithContentsOfFile:plistPath];

To send this dictionary to server, you can use AFNetworking.

To Stored valude into plist:

 NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES);
 NSString *documentsPath = paths.firstObject;
 NSString *plistPath = [documentsPath stringByAppendingPathComponent:@"File.plist"];
 NSError *writeError = nil;
 NSDictionary *finalDict = @{@"Objects": objects};
 NSData *plistData = [NSPropertyListSerialization dataWithPropertyList:finalDict format:NSPropertyListXMLFormat_v1_0 options:NSPropertyListImmutable error:&writeError];
 if(plistData){
     [plistData writeToFile:plistPath atomically:YES];
 }
 else {
     NSLog(@"Error in saveData: %@", error);
 }
Nirmalsinh Rathod
  • 5,079
  • 4
  • 26
  • 56