2012-04-19 12 views

उत्तर

9

iCloud से दस्तावेज़ हटाने के लिए, पहले आप फ़ाइल नाम आप हटाना चाहते हैं पाने के लिए किया है। और फिर आप NSFileManager का उपयोग करके इसे हटा सकते हैं।

NSString *saveFileName = @"Report.pdf"; 
NSURL *ubiq = [[NSFileManager defaultManager] URLForUbiquityContainerIdentifier:nil]; 
NSURL *ubiquitousPackage = [[ubiq URLByAppendingPathComponent:@"Documents"] URLByAppendingPathComponent:saveFileName]; 
NSFileManager *filemgr = [NSFileManager defaultManager]; 
[filemgr removeItemAtURL:ubiquitousPackage error:nil]; 

यह तरीका है, जिसे मैं दस्तावेज़ को हटाने के लिए उपयोग करता था, इसे जांचें। यह मेरे लिए बहुत अच्छा है। धन्यवाद

+3

दस्तावेज़ों के मुताबिक आपको पृष्ठभूमि कतार पर सामान एसिंक को हटाना होगा। http://developer.apple.com/library/ios/documentation/DataManagement/Conceptual/DocumentBasedAppPGiOS/ManageDocumentLifeCycle/ManageDocumentLifeCycle। एचटीएमएल # // apple_ref/doc/uid/TP40011149-CH4-SW4 – Jonny

+0

यह कहां काम करता है यह 'एनआईडीएफएल' के 'एनआईडी दस्तावेज के कार्यान्वयन का लाभ नहीं उठाता ileCoordinator 'एड्रियन सरली का जवाब बेहतर है। – Joseph

+1

संदर्भ के लिए, कृपया ध्यान दें कि 'removeItemAtURL:' दस्तावेज़ को हटा नहीं देगा अगर इसे अभी तक iCloud से डिवाइस पर डाउनलोड नहीं किया गया है। – Mark

0

मुझे लगता है कि मैं एक समाधान मिला:

[[NSFileManager defaultManager] setUbiquitous:NO itemAtURL:url destinationURL:nil error:nil] 

स्रोत: http://oleb.net/blog/2011/11/ios5-tech-talk-michael-jurewitz-on-icloud-storage/

+1

कि यह नहीं है कि काम iCloud अब इस पर ध्यान नहीं देता से आते हैं एक nil-value destinationURL के साथ कॉल करें। –

0

के तहत "एक दस्तावेज़ के जीवन cyle प्रबंध" 'एक दस्तावेज़ को हटाया जा रहा एप्पल देखें प्रलेखन। "

13

Document-Based App Programming Guide for iOS की "Deleting a Document" अनुभाग से कॉपी किया गया

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(void) { 
    NSFileCoordinator* fileCoordinator = [[NSFileCoordinator alloc] initWithFilePresenter:nil]; 
    [fileCoordinator coordinateWritingItemAtURL:fileURL options:NSFileCoordinatorWritingForDeleting 
     error:nil byAccessor:^(NSURL* writingURL) { 
     NSFileManager* fileManager = [[NSFileManager alloc] init]; 
     [fileManager removeItemAtURL:writingURL error:nil]; 
    }]; 
}); 

एनबी:।" जब आप स्टोरेज से दस्तावेज़ हटाते हैं, तो आपके कोड को अनुमान लगाया जाना चाहिए कि यूआईडी दस्तावेज पढ़ने और लिखने के लिए क्या करता है। यह एक पृष्ठभूमि कतार पर अतुल्यकालिक रूप से हटाए जाने के प्रदर्शन करना चाहिए, और यह फ़ाइल समन्वय का उपयोग करना चाहिए। "

+1

यदि आप 'यूआईडी दस्तावेज' का उपयोग कर रहे हैं तो अपने स्वयं के NSFileCoordinator को लागू करने की कोई आवश्यकता नहीं है, यह पहले से ही 'UIDocument' में बेक है। अपनी विधि का उपयोग करना कई 'NSFileCoordinator' उदाहरणों में महंगा परिणाम है। देखें: https://developer.apple.com/library/ios/documentation/FileManagement/Conceptual/FileSystemProgrammingGuide/FileCoordinators/FileCoordinators.html – Joseph

+0

@ जोसेफ मैं आपके उत्तर से ऊपर सोच रहा हूं, यही कारण है कि मैं या तो हटाने में असमर्थ हूं एक आइटम, जो iCloud से UIDocument का ऑब्जेक्ट है या ऐसा करने का प्रयास करते समय ऐप लटकता है? मेरा संबंधित प्रश्न [यहां] (http://stackoverflow.com/questions/43821141/app-hangs-deleting-icloud-document-when-following-guideline-using-nsfilecoordi) – yohannes

1

स्विफ्ट 3 @AlexChaffee के जवाब

func deleteZipFile(with filePath: String) { 
    DispatchQueue.global(qos: .default).async { 
     let fileCoordinator = NSFileCoordinator(filePresenter: nil) 
     fileCoordinator.coordinate(writingItemAt: URL(fileURLWithPath: filePath), options: NSFileCoordinator.WritingOptions.forDeleting, error: nil) { 
      writingURL in 
      do { 
       try FileManager.default.removeItem(at: writingURL) 
      } catch { 
       DLog("error: \(error.localizedDescription)") 
      } 
     } 
    } 
} 
संबंधित मुद्दे