2012-10-19 40 views
0

我的應用程序需要從服務器下載相當數量的內容才能運行。我應該如何防止文件在iOS 5.0上備份到iCloud和iTunes?

有關數據存儲蘋果的指導方針提到,這種類型的數據,「需要到工作的,但是,易refetchable」不應該被包含在iCloud中/ iTunes的備份:不夠公平。

棘手的部分是,防止目錄被備份的代碼在iOS 5.0,5.0.1和5.1之間不同(參見this technical note)。

我的應用當前支持iOS 5.0作爲部署目標。

我應該不同下列選項之間做:

  • 設置的部署目標爲5.1(直線前進,但我不能找到用戶的比例數據的iOS 5.0和5.0仍然.1舒適引入蘋果公司提供的主題給我的老闆)
  • 實現雙方5.0.1和5.1代碼但它帶來了一些問題:
    • 我一貫的方法來檢測設備是否運行一個speficic iOS版本就是使用respondsToSelector:在我選擇的iOS版本中引入了一個選擇器,但iOS 5.1 seems to introduce constants and not-universal classes only。如何確定我正在運行iOS 5.1或更高版本?
    • 運行iOS 5.0的設備怎麼樣?將數據存儲到高速緩存是超級討厭處理既爲開發團隊和用戶體驗

任何其他選項建議?

+0

我以前在這裏提供了這個問題的確切解決方案:http://stackoverflow.com/q/12371321/1633251 - 如果你喜歡問與答請upvote兩者。 –

回答

0

對於被存儲到文件系統中的每個文件,你必須添加「不備份」屬性。看到這個答案:Adding the "Do Not Backup" attribute to a folder hierarchy in iOS 5.0.1。要檢查系統版本,您可以撥打[[UIDevice currentDevice] systemVersion]或使用宏如__IPHONE_5_0#if defined()#ifndef。祝你好運!

+0

這就是我之前做的,但現在我在iOS 6設備上運行,setxattr方法不再返回0。所以這種方法已經不夠了。 –

+0

感謝您的__IPHONE_5_0提示!(從systemVersion開始,我認爲這是獲得iOS版本最直接的方式,但我不喜歡它,因爲它需要一些NSString解析以及對未來iOS版本名稱的一些期望) –

-1

如果您的數據存儲到緩存目錄,而不是文件的目錄下的icloud不能備份您的數據...

,所以我認爲這是停止iCloud雲備份的最佳途徑。

「不備份」屬性,你需要設置該標誌爲每個文件。

使用NSCachesDirectory insted的NSDocumentDirectory。

+0

隨時可以從緩存目錄中刪除文件,並且可以將「不備份」屬性設置爲文件夾,這意味着該文件夾中的文件也不會被備份。如果你打算幫助別人,請確保你的幫助至少可以幫助他們70%,比如蘋果:) –

+0

對Fahri評論+1。 –

+0

你好fahri謝謝你的投票...但我的一個應用程序被蘋果拒絕這個icloud備份問題。之後,我已搜索很多,並找到解決方案緩存目錄... –

0

我標誌着我的圖片文件夾不進行備份,因爲我下載圖像供脫機使用。 Here蘋果討論如何做到這一點。

#include<sys/xattr.h> 

- (BOOL)addSkipBackupAttributeToItemAtURL:(NSURL *)URL { 
    if (&NSURLIsExcludedFromBackupKey == nil) { // iOS <= 5.0.1 
     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; 
    } else { // iOS >= 5.1 
     NSError *error = nil; 
     [URL setResourceValue:[NSNumber numberWithBool:YES] forKey:NSURLIsExcludedFromBackupKey error:&error]; 
     return error == nil; 
    } 
} 

- (void)createSkipBackupImagesFolder { 
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentsDirectory = [paths objectAtIndex:0]; 
    NSString *dataPath = [documentsDirectory stringByAppendingPathComponent:@"/images"]; 

    NSError *error; 
    if (![[NSFileManager defaultManager] fileExistsAtPath:dataPath]) { 
     [[NSFileManager defaultManager] createDirectoryAtPath:dataPath withIntermediateDirectories:NO attributes:nil error:&error]; 
     NSURL *toURL = [NSURL fileURLWithPath:dataPath]; 
     [self addSkipBackupAttributeToItemAtURL:toURL]; 
    } 
} 

[self createSkipBackupImagesFolder]; 
相關問題