2014-02-25 37 views
4

我在與在Xcode 5書面文件AppSupport文件夾,爲iOS 7的麻煩 我想要做的事:附加您的<bundle_ID>目錄。它是什麼?

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES); 
NSString *plistPath = [[paths lastObject] stringByAppendingPathComponent:@「somedata.plist」]; 
if(plistData) { 
    NSLog(@"PATH: %@", plistPath); 
    BOOL success = [plistData writeToFile:plistPath atomically:YES]; 
    NSLog(@"SUCCESS: %hhd",success); 
} 
else { 
    NSLog(@"ERROR CREATING PLIST: %@",error); 
} 

而且我沒有得到作爲輸出:

PATH: /var/mobile/Applications/40954....7E3C/Library/Application Support/somedata.plist 
SUCCESS: 0 

蘋果的文件說:

Use the Application Support directory constant NSApplicationSupportDirectory, 
appending your <bundle_ID> for: … 

什麼意思你追加bundle_ID的?可能還有另一條路要我用? NSDocumentDirectory不適合我,因爲它是用戶文件的一個地方。

+0

什麼是plist文件?它總是需要嗎?它是如何重新創建? – Wain

+0

此外,默認情況下NSApplicationSupportDirectory不存在,所以如果需要,您需要檢查並創建它 – Wain

+0

基本上很少更新大數組字符串。如果用戶處於脫機狀態,那將是一個問題。如果他在線,可能需要很長時間。 –

回答

3

存儲到NSApplicationSupportDirectory是有點複雜多了,

按照此Apple sample code將文件寫入到這個目錄,

- (NSURL*)applicationDirectory 
{ 
    NSString* bundleID = [[NSBundle mainBundle] bundleIdentifier]; 
    NSFileManager*fm = [NSFileManager defaultManager]; 
    NSURL* dirPath = nil; 

    // Find the application support directory in the home directory. 
    NSArray* appSupportDir = [fm URLsForDirectory:NSApplicationSupportDirectory 
             inDomains:NSUserDomainMask]; 
    if ([appSupportDir count] > 0) 
    { 
     // Append the bundle ID to the URL for the 
     // Application Support directory 
     dirPath = [[appSupportDir objectAtIndex:0] URLByAppendingPathComponent:bundleID]; 

     //Modified code to write your plist file to the Application support dir 
     NSString *plistPath = [dirPath stringByAppendingPathComponent:@「somedata.plist」]; 

     //Assuming plistData is pre-populated ivar 
     //Else add your code to create plistData here 
     if(plistData) { 
      BOOL success = [plistData writeToFile:plistPath atomically:YES]; 
      NSLog(@"SUCCESS: %hhd",success); 
     } 
     else { 
      NSLog(@"ERROR CREATING PLIST: %@",error); 
     } 
    } 
} 
+0

這就是我正在尋找的:[[NSBundle mainBundle] bundleIdentifier]。謝謝。 –

+0

Downvoter,如果您提供反饋以改進答案,請諒解。 – Amar

+0

您修改了原始蘋果代碼而未進行測試。您的功能不返回任何。 – DanSkeel

相關問題