2010-10-05 9 views
6

मैं अपने ऐप में आईट्यून्स फ़ाइल साझाकरण का उपयोग कर रहा हूं, और कोर डेटा के स्क्लाइट डेटाबेस को कहीं और डालने की आवश्यकता है ताकि उपयोगकर्ता इसके साथ परेशान न हों। कोर डेटा का उपयोग करने वाली एसक्लाइट फ़ाइल को छिपाने के सर्वोत्तम तरीके के बारे में मैंने a previous SO post पढ़ा है।आईट्यून्स फ़ाइल शेयरिंग सक्षम होने पर कोर डेटा स्क्लाइट फ़ाइल छुपाएं

लाइब्रेरी/प्राथमिकताओं में डेटाबेस या .data नामक निर्देशिका में डालने के बारे में विवादित राय प्रतीत होती है, लेकिन मुझे लगता है कि मैं .data निर्देशिका का उपयोग करने का सबसे अच्छा तरीका मानता हूं।

वर्तमान में एक -applicationDocumentsDirectory विधि है कि कोर डाटा टेम्पलेट कोड द्वारा प्रदान किया गया है:

- (NSString *)applicationDocumentsDirectory { 
    return [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject]; 
} 

मैं एक समारोह applicationHiddenDocumentsDirectory कहा जाता है मुझे ".data" उपनिर्देशिका पहुँच दे देंगे कि लागू करने के लिए चाहते हैं, लेकिन मुझे निर्देशिका तक पहुंचने के लिए उद्देश्य-सी या कोको/फाउंडेशन ढांचे के बारे में पर्याप्त जानकारी नहीं है।

क्या कोई मुझे इस विधि को लागू करने में मदद कर सकता है?

धन्यवाद!

== रोवन ==

उत्तर

14

आपको यह कैसे पसंद है? यदि कोई त्रुटि होती है तो आपको उचित कार्रवाई जोड़नी होगी।
संपादित करें: मैंने इसे बदल दिया है, इसलिए डेटाबेस लाइब्रेरी-निर्देशिका में सहेजा गया है, जिसका उपयोग आईट्यून्स द्वारा समर्थित है और उपयोगकर्ता को दिखाई नहीं देता है। यह Apple Q&A

- (NSString *)applicationHiddenDocumentsDirectory { 
    // NSString *path = [[self applicationDocumentsDirectory] stringByAppendingPathComponent:@".data"]; 
    NSString *libraryPath = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) lastObject]; 
    NSString *path = [libraryPath stringByAppendingPathComponent:@"Private Documents"]; 

    BOOL isDirectory = NO; 
    if ([[NSFileManager defaultManager] fileExistsAtPath:path isDirectory:&isDirectory]) { 
     if (isDirectory) 
      return path; 
     else { 
      // Handle error. ".data" is a file which should not be there... 
      [NSException raise:@".data exists, and is a file" format:@"Path: %@", path]; 
      // NSError *error = nil; 
      // if (![[NSFileManager defaultManager] removeItemAtPath:path error:&error]) { 
      //  [NSException raise:@"could not remove file" format:@"Path: %@", path]; 
      // } 
     } 
    } 
    NSError *error = nil; 
    if (![[NSFileManager defaultManager] createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:&error]) { 
     // Handle error. 
     [NSException raise:@"Failed creating directory" format:@"[%@], %@", path, error]; 
    } 
    return path; 
} 
+0

उत्कृष्ट प्रतिक्रिया, और के लिए लिंक के लिए धन्यवाद ऐप्पल क्यू एंड ए। मैंने संदर्भित एसओ थ्रेड पर इसके बारे में कुछ चर्चा की थी, लेकिन जब तक आप उससे जुड़े नहीं थे तब तक मुझे ऐप्पल से आधिकारिक सलाह नहीं मिली। –

+1

हाँ, यह एक समाधान है। लेकिन इसे 3 डी पार्टी libs के लिए कैसे लागू करें, उदा। गूगल विश्लेषिकी? "आईफोन फाइल शेयरिंग गैंट्रेकर" समस्या है :( – slatvick

5

द्वारा सुझाव दिया गया था के रूप में नई CoreData खाके पर लौट NSURL बल्कि वस्तुओं NSString पथ से, यहाँ ऊपर कोड के अपने अद्यतन संस्करण है:

- (NSURL *)applicationHiddenDocumentsDirectory { 
    // NSString *path = [[self applicationDocumentsDirectory] stringByAppendingPathComponent:@".data"]; 
    NSString *libraryPath = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) lastObject]; 
    NSString *path = [libraryPath stringByAppendingPathComponent:@"Private Documents"]; 
    NSURL *pathURL = [NSURL fileURLWithPath:path]; 

    BOOL isDirectory = NO; 
    if ([[NSFileManager defaultManager] fileExistsAtPath:path isDirectory:&isDirectory]) { 
    if (isDirectory) { 
     return pathURL; 
    } else { 
     // Handle error. ".data" is a file which should not be there... 
     [NSException raise:@"'Private Documents' exists, and is a file" format:@"Path: %@", path]; 
     // NSError *error = nil; 
     // if (![[NSFileManager defaultManager] removeItemAtPath:path error:&error]) { 
     //  [NSException raise:@"could not remove file" format:@"Path: %@", path]; 
     // } 
    } 
    } 
    NSError *error = nil; 
    if (![[NSFileManager defaultManager] createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:&error]) { 
    // Handle error. 
    [NSException raise:@"Failed creating directory" format:@"[%@], %@", path, error]; 
    } 
    return pathURL; 
} 
संबंधित मुद्दे