2013-04-02 64 views
1

在寫入我的iPad中的文件時出現此錯誤。但同樣的應用程序在模擬器中工作正常。我在這裏附上代碼。在向ios中的文件寫入時出錯代碼516

NSString *filepath = [[[NSBundle mainBundle] bundlePath] stringByAppendingPathComponent:doc.contentStreamFileName]; 
         NSFileManager *fMngr = [NSFileManager defaultManager]; 
         if ([fMngr fileExistsAtPath:filepath]) { 
          NSLog(@"File already available"); 
          CMISExtensionElement *elem = [doc.properties.extensions objectAtIndex:0]; 
          NSString *description = [self crop:elem.description]; 
          [self loadImage:doc.contentStreamFileName desc:description]; 
         } 
         else{ 
          [doc downloadContentToFile:filepath completionBlock:^(NSError *error){ 
           if (nil == error) { 
            NSLog(@"successful"); 
           } 
           else 
           { 
            UIAlertView* alert = [[UIAlertView alloc]initWithTitle:@"Error retriving content" message:[NSString stringWithFormat:@"Error code: %d",error.code] delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil]; 
            [alert show]; 

           } 
          }progressBlock:^(unsigned long long bytesDownloaded, unsigned long long bytesTotal){ 
           if (bytesTotal == bytesDownloaded) { 
            CMISExtensionElement *elem = [doc.properties.extensions objectAtIndex:0]; 
            NSString *description = [self crop:elem.description]; 
            [self loadImage:doc.contentStreamFileName desc:description]; 
           } 
          }]; 
         } 

回答

2

您不應該嘗試在應用程序的捆綁目錄中編寫代碼。

如果應用程序生成和使用文件,則應該使用NSApplicationSupport目錄;如果希望文件對用戶可見,則應使用NSApplicationDocuments目錄。

它不是絕對必要的,但它的常見做法是在您的文件中創建一個子目錄。你使用NSFileManager的createDirectory ...方法。

2

您是否考慮過寫入應用程序Documents目錄?我不認爲你應該寫入應用程序包,只能從中讀取。

NSString *documentsDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; 
NSString *filePath = [documentsDir stringByAppendingPathComponent:doc.contentStreamFileName];