您现在的位置: IOS教程網 >> IOS系統 >  >> iOS 程式開發 >> iOS 開發綜合

iOS 應用實戰:設定檔案不自動備份到 iCloud

編輯:iOS 開發綜合

iOS 專案中若有內建下載或程式動態產生的檔案,就必須留意這些檔案要不要自動備份到 iCloud。若沒有正當理由就把大檔案自動上傳,很可能導致 App 無法通過審核,收到類似以下的答覆:

We also found that your app does not follow the iOS Data Storage Guidelines, which is required per the App Store Review Guidelines.

蘋果對此有明確的官方指引(QA1719:https://developer.apple.com/Library/ios/qa/qa1719/_index.html)。核心做法是:把可重新生成的暫存檔案存放到 Caches 或自訂目錄,並明確標記為「不備份到 iCloud」。

應用到專案中的具體程式碼如下:

#define DownLoad_Directory "Download_Dir"
#pragma mark Download_Dir
void DeviceClass::initDownloadDir() {
    string l_strDocumentDir = cocos2d::CCFileUtils::sharedFileUtils()->getWritablePath();
    l_strDocumentDir.append(DownLoad_Directory);
    NSString *l_strDownloadDir = [NSString stringWithUTF8String:l_strDocumentDir.c_str()];
    NSError *l_error;
    if (![[NSFileManager defaultManager] fileExistsAtPath:l_strDownloadDir]) {
        [[NSFileManager defaultManager] createDirectoryAtPath:l_strDownloadDir withIntermediateDirectories:NO attributes:nil error:&l_error];
        // exclude downloads from iCloud backup
        NSURL *url = [NSURL fileURLWithPath:l_strDownloadDir];
        if (strcmp(g_systemVersion.c_str(), "5.1") >= 0) {
            if ([url setResourceValue:[NSNumber numberWithBool:YES] forKey:NSURLIsExcludedFromBackupKey error:&l_error] == NO) {
                NSLog(@"Error: Unable to exclude l_strDownloadDir from backup: %@", l_error);
            }
        }
    }
}

重點就是在可寫目錄下建立一個新資料夾來存放下載檔案(若需要,動態產生的檔案也一併放在這裡),並透過 NSURLIsExcludedFromBackupKey 標記這個目錄不會被上傳到 iCloud。

測試方法

依序進入:設定 → iCloud → 管理儲存空間 → 備份(若有多個裝置則選擇對應裝置)。如果應用很多,可點擊「顯示所有應用」找到你的 App,查看它在 iCloud 上備份的內容總容量。

打開與關閉這個設定接口,你會發現 App 在 iCloud 上的備份容量是否會隨之改變——以此驗證排除備份的設定是否生效。

  1. 上一篇文章:
  2. 下一篇文章:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © IOS教程網 All Rights Reserved