Unable to save file to local storage in ios swift





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







0















I am trying to download file content from server, creating one file on client end and tranfering all content to that. I am saving the file to local storage,but it is unable to save throws exception that you don't have permission to save in folder. Tried so many things but unable to do so. I am very new to this ios programming. Please help me out, thanks in advance.



Below is my code:



 let documentsUrl =  FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first
var fileURL : URL = URL(fileURLWithPath: documentsUrl!.path+filename)
fileURL = URL(fileURLWithPath: documentsUrl!.path+filename+".doc")
let data = NSData(contentsOf: tempLocalUrl)
do {
try data?.write(to: fileURL, options: .atomic)
} catch {
print(error,"helooooooooooooooooo")
}


I have used relativepath, absolutestring but still no luck .



This is the error :




Error Domain=NSCocoaErrorDomain Code=513 "You don’t have permission to save the file “DocumentsImport From IE.doc” in the folder “0103E60B-478F-475B-A8F2-788534F5D206”." UserInfo={NSFilePath=/var/mobile/Containers/Data/Application/0103E60B-478F-475B-A8F2-788534F5D206/DocumentsImport From IE.doc, NSUnderlyingError=0x1076efe90 {Error Domain=NSPOSIXErrorDomain Code=1 "Operation not permitted"}}











share|improve this question




















  • 1





    where is the documentsUrl coming from? how did you extract that URL?

    – holex
    Aug 16 '18 at 7:06











  • I have edited code, please check @holex

    – Ashish yadav
    Aug 16 '18 at 7:09






  • 1





    and I'm sure you have spotted the missing / between the path and filename... like .../Documents + / + Import From IE.doc, you currently don't have the / – it is better to use the addPathComponent(...) rather than concatenating the raw string values... that may help you to avoid such glitches in future.

    – holex
    Aug 16 '18 at 7:14













  • Never concatenate strings to build paths. Use the appropriate API of URL. This solves your issue.

    – vadian
    Aug 16 '18 at 7:17











  • Thanks @holex it works, you saved my time.

    – Ashish yadav
    Aug 16 '18 at 7:23


















0















I am trying to download file content from server, creating one file on client end and tranfering all content to that. I am saving the file to local storage,but it is unable to save throws exception that you don't have permission to save in folder. Tried so many things but unable to do so. I am very new to this ios programming. Please help me out, thanks in advance.



Below is my code:



 let documentsUrl =  FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first
var fileURL : URL = URL(fileURLWithPath: documentsUrl!.path+filename)
fileURL = URL(fileURLWithPath: documentsUrl!.path+filename+".doc")
let data = NSData(contentsOf: tempLocalUrl)
do {
try data?.write(to: fileURL, options: .atomic)
} catch {
print(error,"helooooooooooooooooo")
}


I have used relativepath, absolutestring but still no luck .



This is the error :




Error Domain=NSCocoaErrorDomain Code=513 "You don’t have permission to save the file “DocumentsImport From IE.doc” in the folder “0103E60B-478F-475B-A8F2-788534F5D206”." UserInfo={NSFilePath=/var/mobile/Containers/Data/Application/0103E60B-478F-475B-A8F2-788534F5D206/DocumentsImport From IE.doc, NSUnderlyingError=0x1076efe90 {Error Domain=NSPOSIXErrorDomain Code=1 "Operation not permitted"}}











share|improve this question




















  • 1





    where is the documentsUrl coming from? how did you extract that URL?

    – holex
    Aug 16 '18 at 7:06











  • I have edited code, please check @holex

    – Ashish yadav
    Aug 16 '18 at 7:09






  • 1





    and I'm sure you have spotted the missing / between the path and filename... like .../Documents + / + Import From IE.doc, you currently don't have the / – it is better to use the addPathComponent(...) rather than concatenating the raw string values... that may help you to avoid such glitches in future.

    – holex
    Aug 16 '18 at 7:14













  • Never concatenate strings to build paths. Use the appropriate API of URL. This solves your issue.

    – vadian
    Aug 16 '18 at 7:17











  • Thanks @holex it works, you saved my time.

    – Ashish yadav
    Aug 16 '18 at 7:23














0












0








0








I am trying to download file content from server, creating one file on client end and tranfering all content to that. I am saving the file to local storage,but it is unable to save throws exception that you don't have permission to save in folder. Tried so many things but unable to do so. I am very new to this ios programming. Please help me out, thanks in advance.



Below is my code:



 let documentsUrl =  FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first
var fileURL : URL = URL(fileURLWithPath: documentsUrl!.path+filename)
fileURL = URL(fileURLWithPath: documentsUrl!.path+filename+".doc")
let data = NSData(contentsOf: tempLocalUrl)
do {
try data?.write(to: fileURL, options: .atomic)
} catch {
print(error,"helooooooooooooooooo")
}


I have used relativepath, absolutestring but still no luck .



This is the error :




Error Domain=NSCocoaErrorDomain Code=513 "You don’t have permission to save the file “DocumentsImport From IE.doc” in the folder “0103E60B-478F-475B-A8F2-788534F5D206”." UserInfo={NSFilePath=/var/mobile/Containers/Data/Application/0103E60B-478F-475B-A8F2-788534F5D206/DocumentsImport From IE.doc, NSUnderlyingError=0x1076efe90 {Error Domain=NSPOSIXErrorDomain Code=1 "Operation not permitted"}}











share|improve this question
















I am trying to download file content from server, creating one file on client end and tranfering all content to that. I am saving the file to local storage,but it is unable to save throws exception that you don't have permission to save in folder. Tried so many things but unable to do so. I am very new to this ios programming. Please help me out, thanks in advance.



Below is my code:



 let documentsUrl =  FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first
var fileURL : URL = URL(fileURLWithPath: documentsUrl!.path+filename)
fileURL = URL(fileURLWithPath: documentsUrl!.path+filename+".doc")
let data = NSData(contentsOf: tempLocalUrl)
do {
try data?.write(to: fileURL, options: .atomic)
} catch {
print(error,"helooooooooooooooooo")
}


I have used relativepath, absolutestring but still no luck .



This is the error :




Error Domain=NSCocoaErrorDomain Code=513 "You don’t have permission to save the file “DocumentsImport From IE.doc” in the folder “0103E60B-478F-475B-A8F2-788534F5D206”." UserInfo={NSFilePath=/var/mobile/Containers/Data/Application/0103E60B-478F-475B-A8F2-788534F5D206/DocumentsImport From IE.doc, NSUnderlyingError=0x1076efe90 {Error Domain=NSPOSIXErrorDomain Code=1 "Operation not permitted"}}








ios swift swift3 ios7






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Aug 16 '18 at 7:09







Ashish yadav

















asked Aug 16 '18 at 7:03









Ashish yadavAshish yadav

267




267








  • 1





    where is the documentsUrl coming from? how did you extract that URL?

    – holex
    Aug 16 '18 at 7:06











  • I have edited code, please check @holex

    – Ashish yadav
    Aug 16 '18 at 7:09






  • 1





    and I'm sure you have spotted the missing / between the path and filename... like .../Documents + / + Import From IE.doc, you currently don't have the / – it is better to use the addPathComponent(...) rather than concatenating the raw string values... that may help you to avoid such glitches in future.

    – holex
    Aug 16 '18 at 7:14













  • Never concatenate strings to build paths. Use the appropriate API of URL. This solves your issue.

    – vadian
    Aug 16 '18 at 7:17











  • Thanks @holex it works, you saved my time.

    – Ashish yadav
    Aug 16 '18 at 7:23














  • 1





    where is the documentsUrl coming from? how did you extract that URL?

    – holex
    Aug 16 '18 at 7:06











  • I have edited code, please check @holex

    – Ashish yadav
    Aug 16 '18 at 7:09






  • 1





    and I'm sure you have spotted the missing / between the path and filename... like .../Documents + / + Import From IE.doc, you currently don't have the / – it is better to use the addPathComponent(...) rather than concatenating the raw string values... that may help you to avoid such glitches in future.

    – holex
    Aug 16 '18 at 7:14













  • Never concatenate strings to build paths. Use the appropriate API of URL. This solves your issue.

    – vadian
    Aug 16 '18 at 7:17











  • Thanks @holex it works, you saved my time.

    – Ashish yadav
    Aug 16 '18 at 7:23








1




1





where is the documentsUrl coming from? how did you extract that URL?

– holex
Aug 16 '18 at 7:06





where is the documentsUrl coming from? how did you extract that URL?

– holex
Aug 16 '18 at 7:06













I have edited code, please check @holex

– Ashish yadav
Aug 16 '18 at 7:09





I have edited code, please check @holex

– Ashish yadav
Aug 16 '18 at 7:09




1




1





and I'm sure you have spotted the missing / between the path and filename... like .../Documents + / + Import From IE.doc, you currently don't have the / – it is better to use the addPathComponent(...) rather than concatenating the raw string values... that may help you to avoid such glitches in future.

– holex
Aug 16 '18 at 7:14







and I'm sure you have spotted the missing / between the path and filename... like .../Documents + / + Import From IE.doc, you currently don't have the / – it is better to use the addPathComponent(...) rather than concatenating the raw string values... that may help you to avoid such glitches in future.

– holex
Aug 16 '18 at 7:14















Never concatenate strings to build paths. Use the appropriate API of URL. This solves your issue.

– vadian
Aug 16 '18 at 7:17





Never concatenate strings to build paths. Use the appropriate API of URL. This solves your issue.

– vadian
Aug 16 '18 at 7:17













Thanks @holex it works, you saved my time.

– Ashish yadav
Aug 16 '18 at 7:23





Thanks @holex it works, you saved my time.

– Ashish yadav
Aug 16 '18 at 7:23












3 Answers
3






active

oldest

votes


















1














Never build paths like



documentsUrl!.path+filename+".doc"


Don't do that. You could forget the path separators which you actually did.



URL has dedicated API to build paths



let documentsUrl =  FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
let fileURL = documentsUrl.appendingPathComponent(filename).appendingPathExtension("doc")
do {
let data = try Data(contentsOf: tempLocalUrl) // not NSData !!
try data.write(to: fileURL, options: .atomic)
} catch {
print(error,"helooooooooooooooooo")
}





share|improve this answer


























  • Thanks It worked for me

    – Ashish yadav
    Aug 16 '18 at 7:42



















0














Your path in the error is:



/var/mobile/Containers/Data/Application/0103E60B-478F-475B-A8F2-788534F5D206/DocumentsImport From IE.doc


instead of:



/var/mobile/Containers/Data/Application/0103E60B-478F-475B-A8F2-788534F5D206/Documents/Import From IE.doc


And use appendingPathComponent to concatenate path elements.






share|improve this answer































    0














    I Save my image like that



    let fileManager = FileManager.default
    do {



    let documentDirectory = try fileManager.url(for: .documentDirectory, in: .userDomainMask, appropriateFor:nil, create:false)
    let fileURL = documentDirectory.appendingPathComponent(name)
    let image = #imageLiteral(resourceName: "Notifications")if let imageData = UIImageJPEGRepresentation(image, 0.5) {
    try imageData.write(to: fileURL)
    return true }} catch { print(error)}





    share|improve this answer


























      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f51871355%2funable-to-save-file-to-local-storage-in-ios-swift%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      1














      Never build paths like



      documentsUrl!.path+filename+".doc"


      Don't do that. You could forget the path separators which you actually did.



      URL has dedicated API to build paths



      let documentsUrl =  FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
      let fileURL = documentsUrl.appendingPathComponent(filename).appendingPathExtension("doc")
      do {
      let data = try Data(contentsOf: tempLocalUrl) // not NSData !!
      try data.write(to: fileURL, options: .atomic)
      } catch {
      print(error,"helooooooooooooooooo")
      }





      share|improve this answer


























      • Thanks It worked for me

        – Ashish yadav
        Aug 16 '18 at 7:42
















      1














      Never build paths like



      documentsUrl!.path+filename+".doc"


      Don't do that. You could forget the path separators which you actually did.



      URL has dedicated API to build paths



      let documentsUrl =  FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
      let fileURL = documentsUrl.appendingPathComponent(filename).appendingPathExtension("doc")
      do {
      let data = try Data(contentsOf: tempLocalUrl) // not NSData !!
      try data.write(to: fileURL, options: .atomic)
      } catch {
      print(error,"helooooooooooooooooo")
      }





      share|improve this answer


























      • Thanks It worked for me

        – Ashish yadav
        Aug 16 '18 at 7:42














      1












      1








      1







      Never build paths like



      documentsUrl!.path+filename+".doc"


      Don't do that. You could forget the path separators which you actually did.



      URL has dedicated API to build paths



      let documentsUrl =  FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
      let fileURL = documentsUrl.appendingPathComponent(filename).appendingPathExtension("doc")
      do {
      let data = try Data(contentsOf: tempLocalUrl) // not NSData !!
      try data.write(to: fileURL, options: .atomic)
      } catch {
      print(error,"helooooooooooooooooo")
      }





      share|improve this answer















      Never build paths like



      documentsUrl!.path+filename+".doc"


      Don't do that. You could forget the path separators which you actually did.



      URL has dedicated API to build paths



      let documentsUrl =  FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
      let fileURL = documentsUrl.appendingPathComponent(filename).appendingPathExtension("doc")
      do {
      let data = try Data(contentsOf: tempLocalUrl) // not NSData !!
      try data.write(to: fileURL, options: .atomic)
      } catch {
      print(error,"helooooooooooooooooo")
      }






      share|improve this answer














      share|improve this answer



      share|improve this answer








      edited Aug 16 '18 at 8:01

























      answered Aug 16 '18 at 7:25









      vadianvadian

      155k17165191




      155k17165191













      • Thanks It worked for me

        – Ashish yadav
        Aug 16 '18 at 7:42



















      • Thanks It worked for me

        – Ashish yadav
        Aug 16 '18 at 7:42

















      Thanks It worked for me

      – Ashish yadav
      Aug 16 '18 at 7:42





      Thanks It worked for me

      – Ashish yadav
      Aug 16 '18 at 7:42













      0














      Your path in the error is:



      /var/mobile/Containers/Data/Application/0103E60B-478F-475B-A8F2-788534F5D206/DocumentsImport From IE.doc


      instead of:



      /var/mobile/Containers/Data/Application/0103E60B-478F-475B-A8F2-788534F5D206/Documents/Import From IE.doc


      And use appendingPathComponent to concatenate path elements.






      share|improve this answer




























        0














        Your path in the error is:



        /var/mobile/Containers/Data/Application/0103E60B-478F-475B-A8F2-788534F5D206/DocumentsImport From IE.doc


        instead of:



        /var/mobile/Containers/Data/Application/0103E60B-478F-475B-A8F2-788534F5D206/Documents/Import From IE.doc


        And use appendingPathComponent to concatenate path elements.






        share|improve this answer


























          0












          0








          0







          Your path in the error is:



          /var/mobile/Containers/Data/Application/0103E60B-478F-475B-A8F2-788534F5D206/DocumentsImport From IE.doc


          instead of:



          /var/mobile/Containers/Data/Application/0103E60B-478F-475B-A8F2-788534F5D206/Documents/Import From IE.doc


          And use appendingPathComponent to concatenate path elements.






          share|improve this answer













          Your path in the error is:



          /var/mobile/Containers/Data/Application/0103E60B-478F-475B-A8F2-788534F5D206/DocumentsImport From IE.doc


          instead of:



          /var/mobile/Containers/Data/Application/0103E60B-478F-475B-A8F2-788534F5D206/Documents/Import From IE.doc


          And use appendingPathComponent to concatenate path elements.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Aug 16 '18 at 7:20









          KerberosKerberos

          2,72522646




          2,72522646























              0














              I Save my image like that



              let fileManager = FileManager.default
              do {



              let documentDirectory = try fileManager.url(for: .documentDirectory, in: .userDomainMask, appropriateFor:nil, create:false)
              let fileURL = documentDirectory.appendingPathComponent(name)
              let image = #imageLiteral(resourceName: "Notifications")if let imageData = UIImageJPEGRepresentation(image, 0.5) {
              try imageData.write(to: fileURL)
              return true }} catch { print(error)}





              share|improve this answer






























                0














                I Save my image like that



                let fileManager = FileManager.default
                do {



                let documentDirectory = try fileManager.url(for: .documentDirectory, in: .userDomainMask, appropriateFor:nil, create:false)
                let fileURL = documentDirectory.appendingPathComponent(name)
                let image = #imageLiteral(resourceName: "Notifications")if let imageData = UIImageJPEGRepresentation(image, 0.5) {
                try imageData.write(to: fileURL)
                return true }} catch { print(error)}





                share|improve this answer




























                  0












                  0








                  0







                  I Save my image like that



                  let fileManager = FileManager.default
                  do {



                  let documentDirectory = try fileManager.url(for: .documentDirectory, in: .userDomainMask, appropriateFor:nil, create:false)
                  let fileURL = documentDirectory.appendingPathComponent(name)
                  let image = #imageLiteral(resourceName: "Notifications")if let imageData = UIImageJPEGRepresentation(image, 0.5) {
                  try imageData.write(to: fileURL)
                  return true }} catch { print(error)}





                  share|improve this answer















                  I Save my image like that



                  let fileManager = FileManager.default
                  do {



                  let documentDirectory = try fileManager.url(for: .documentDirectory, in: .userDomainMask, appropriateFor:nil, create:false)
                  let fileURL = documentDirectory.appendingPathComponent(name)
                  let image = #imageLiteral(resourceName: "Notifications")if let imageData = UIImageJPEGRepresentation(image, 0.5) {
                  try imageData.write(to: fileURL)
                  return true }} catch { print(error)}






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Nov 16 '18 at 10:30

























                  answered Nov 16 '18 at 10:24









                  sukh.iossukh.ios

                  11




                  11






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f51871355%2funable-to-save-file-to-local-storage-in-ios-swift%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Florida Star v. B. J. F.

                      Error while running script in elastic search , gateway timeout

                      Adding quotations to stringified JSON object values