0

Hei, I'm rather new to objective C and I don't call myself a programmer but a creative coder (someone who makes things work, I don't care so much about elegancy of my solutions ;) ). I have the following problem to solve: My app takes photos and stores them in the apps documents directory. It sends these images to a custom database. Working fine so far.

Some test-users have requested to capture their photos offline and whenever they open the app again and internet connection is available it would send the images to the database. Now I've found the quite easy way to test whether there is internet connection available here: How to check for an active Internet connection on iOS or OSX?

works fine so far. Next I'm wondering how I'd best save the images that need to be sent whenever no internet connection is available. I'm using NSUserDefaults in my app already. Would it be an easy solution to save the names/paths of the images that need to be sent using NSUserDefaults and read them when the app is opened and send all the images at that time?

or can someone recommend another easy way to do it? Suggestions welcome!

Community
  • 1
  • 1
suMi
  • 1,506
  • 1
  • 15
  • 29

4 Answers4

1

NSUserDefaults could be the best way if you have 2-3-5 images to save, otherwise use coredata or create plist in your documents folder and when you have internet you can take all the paths from coredata or plist and upload the images

wootage
  • 866
  • 6
  • 13
  • I have like up to 42 at most... so I guess learning how to use core data makes some sense then :) thx – suMi Apr 17 '14 at 18:48
0

You can save the offline images in the caches directory and upload them as batch ones the internet connection is available. Have a look a this post.

How can I get a writable path on the iPhone?

Community
  • 1
  • 1
rustylepord
  • 5,331
  • 6
  • 34
  • 46
0

I think NSUserDefaults is the best way. You keep the observer for when the internet connection is available and then send. Here's is an example how to save an image in NSUserDefaults:

NSData *imageData = UIImageJPEGRepresentation(image, 1);

NSString *imagePath = [self documentsPathForFileName:[NSString stringWithFormat:@"image_%f.jpg", [NSDate timeIntervalSinceReferenceDate]]];

[imageData writeToFile:imagePath atomically:YES];

[[NSUserDefaults standardUserDefaults] setObject:imagePath forKey:kPLDefaultsAvatarUrl];
[[NSUserDefaults standardUserDefaults] synchronize];

Note: You shouldn't save image data directly into NSUserDefaults

Lucas Brito
  • 1,026
  • 1
  • 17
  • 34
0

You can probably use NSUserDefaults. However, a very practical and simple approach I would take is this:

  • Create a directory in which you save the images, you can't upload right away
  • Upload these, when internet is available
  • When the upload is finished, just delete the pictures.
  • Everytime the app is launched/ internet connection changes check if there are images left in the directory
JWKot
  • 350
  • 3
  • 11
  • could work but I also have some data to save with the images. that's why I was initially thinking about nsuserdefaults – suMi Apr 23 '14 at 08:02