Keeping, but not backng up, downloaded data in iOS5

With iOS5, Apple introduced iCloud and, in the process, decided to get a lot more strict about how much storage developers were using in an app’s documents directory. By default, the documents directory is backed up to iTunes upon syncing a device and will be backed up to iCloud if enabled.

This was not a huge problem for syncing to your computer; sync might be a bit slow, but it wouldn’t cost you money. With iCloud, large amounts of data will now cost money, for cloud based storage and bandwidth.

Our app, Japanese Flash, downloads “plugins” to provide users with additional functionality while staying under the 20MB over the air limit for app store purchases. Apps you can purchase over the air generally make more money than those that don’t, so we are very keen to keep under the limit for all our apps.

During the iOS5 Beta period we received an e-mail from Apple:

Dear Developer,

In recent testing it appears that Japanese Flash – Study & Learn Vocabulary, Kanji, Kana stores a fair amount of data in its Documents folder.

Since iCloud backups are performed daily over Wi-Fi for each user’s iOS device, it’s important to ensure the best possible user experience by minimizing the amount of data being stored by your app.

In addition to purchased music, apps, books, Camera roll, and device settings, everything in your app’s home directory, including its Documents folder, is backed up to iCloud.

Data stored in the application bundle itself, the caches directory, and the temp directory is not backed up to iCloud. Your app should store data in these locations according to the iOS Data Storage Guidelines on .

Please review these guidelines, make any required changes to your app, and submit an update to the App Store.

If you’re not the technical contact for your app, please make sure this email gets to your development team.

If you have any questions concerning this information, please let me know.

Thanks for developing for iOS!

The plugins in Japanese Flash are read only data files so you could download them again. The problem? Prior to iOS5 the caches directory was not backed up and never deleted. In iOS5, the caches directory will be “cleaned” any time the device runs low on space. You can always download that content again, right?

Not so fast. It can take up to 15 minutes over 3G to download the plugins in Japanese Flash. Further, fully offline usage (like on trains) is a selling point for our app. Our users expect the plugins to be available all the time. We had a problem, do as suggested and annoy our users by making them redownload features at unexpected times or keep the data in the documents directory and make the iCloud backup slower and more expensive. We went with the slower backup assuming that not that many people were on iCloud yet.

The real fix – a way to store data that is not backed up, but also not deleted in low storage situations is not yet available. Fortunately, Apple saw the error of their ways and included afix in iOS 5.0.1.

The fix is still under NDA, so we are unable to walk you through it yet. Check back here after the iOS 5.0.1 release and we’ll let you know by updating this post.

Until then, happy coding.

UPDATE:

The iOS 5.0.1 was released to the public today so here’s the fix:

First, data that should be available offline but can be downloaded again should be stored in the Documents directory – not the caches directory as the letter says.

Second, you need to mark the files as not backed up by setting the “Do Not Backup Extended Attribute”. Here is Apple’s method for doing so:

?
1
2
3
4
5
6
7
8
9
10
11
12
#include <sys/xattr.h>
 
- ( BOOL )addSkipBackupAttributeToItemAtURL:( NSURL *)URL
{
     const char * filePath = [[URL path] fileSystemRepresentation];
 
     const char * attrName = "com.apple.MobileBackup" ;
     u_int8_t attrValue = 1;
 
     int result = setxattr(filePath, attrName, &attrValue, sizeof (attrValue), 0, 0);
     return result == 0;
}

We frequently have paths available and it seems silly to convert them to NSURL just to convert back to NSString in the method. Here is what we ended up adding to our file utils, LWEFile. This way we can use whatever we happen to have.

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <sys/xattr.h>
 
//! Method to make a given file not backed up (also prevents it's deletion)
// See https://developer.apple.com/library/ios/#qa/qa1719/_index.html for more info
+ ( BOOL )addSkipBackupAttributeToItemAtURL:( NSURL *)URL
{
   return [LWEFile addSkipBackupAttributeToItemAtPath:[URL path]];
}
 
//! Sets the Skip Backup Extended Attribute for a file at a given path
+ ( BOOL )addSkipBackupAttributeToItemAtPath:( NSString *)path
{
   const char * filePath = [path fileSystemRepresentation];
   const char * attrName = "com.apple.MobileBackup" ;
   u_int8_t attrValue = 1;
 
   int result = setxattr(filePath, attrName, &attrValue, sizeof (attrValue), 0, 0);
   return result == 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值