3 Replies
      Latest reply on Sep 15, 2019 7:44 AM by eskimo
      Relbot Level 1 Level 1 (0 points)

        I am trying to build an app where I am able to access(read/write) windows/mac shared folders in my local network with swift.

        Is there any possible way to do that with swift?

        There is an App in the App Store called "FileExplorer" https://apps.apple.com/de/app/fe-file-explorer-file-manager/id510282524 where you can access these shared folders, but I do not know how they programmed this and with which language. I also tried to access my shared folders via this App and yes it worked I can see my shared folders on my Phone.

        But there needs to be a way to do it with swift...

        I already tried different things(code bellow). In the code bellow I tried to access the shared folder of my second mac and write the Text "Write this text to the fileURL as text in iOS using Swift" into the file named "Test.txt" and after that I want to read the same file again.

        @IBAction func Button(_ sender: UIButton)
        {
            var uc = URLComponents()
            uc.scheme = "smb"
            uc.user = "user"
            uc.password = "password"
            uc.host = "ip-adress"
            uc.path = "document-directory"
            
            // Save data to file
            let fileName = "Test"
            let url = uc.url
            //let DocumentDirURL = URL(fileURLWithPath: "/Users/f/d/t/App/Assets/Apps/TestApp")
            let DocumentDirURL = try! URL(resolvingAliasFileAt: url!)
            let fileURL = DocumentDirURL.appendingPathComponent(fileName).appendingPathExtension("txt")
            print("FilePath: \(fileURL.path)")
            
            
            let writeString = "Write this text to the fileURL as text in iOS using Swift"
            do {
                // Write to the file
                try writeString.write(to: fileURL, atomically: true, encoding: String.Encoding.utf8)
            } catch let error as NSError {
                print("Failed writing to URL: \(fileURL), Error: " + error.localizedDescription)
            }
            
            
            
            var fullString: String = "" // Used to store the file contents
            do {
                // Read the file contents
                fullString = try String(contentsOf: fileURL, encoding: .utf8)
                
            } catch let error as NSError {
                print("Failed reading from URL: \(fileURL), Error: " + error.localizedDescription)
            }
            print("File Text: \(readString)")
        }
        
        

         

        If I run the code as shown, he always gives me the error "smb scheme is not supported" and then some additional errors that he can not write/read the file because he can not access it.

        When I change the code and only search on the device I am programming on and run the simulator to search for this file everything works fine. So I have problems with "smb".

        Thank you for every helpful answer.

        • Re: Access Windows/Mac Shared Folder Locally With smb from iOS
          eskimo Apple Staff Apple Staff (11,815 points)

          Historically, iOS did not include an SMB client, so most old apps that do SMB include their own SMB code.  This isn’t integrated at the file system level, instead they treat SMB as any other network protocol and they have their own implementation of that protocol within their app.

          This story changes in iOS 13, which does have a built-in SMB client integrated at the file system level.  However, that client is only accessible via the iOS document architecture.  If you want to use that, you have to integrate with that architecture.  For more info, see WWDC 2019 Session 719 What’s New in File Management and Quick Look.

          Share and Enjoy

          Quinn “The Eskimo!”
          Apple Developer Relations, Developer Technical Support, Core OS/Hardware
          let myEmail = "eskimo" + "1" + "@apple.com"

            • Re: Access Windows/Mac Shared Folder Locally With smb from iOS
              Relbot Level 1 Level 1 (0 points)

              Hey eskimo,

               

              Thank you for your fast answer. So if I understand everything right. I would be able to programm my on network protocol for SMB in Swift myself or I wait until iOS 13 and be able to implement it with Swift.

              You said that it is only accessible via the iOS document architecture, so will I be possible in the way I programmed it (code I posted) or in a different way?

              Will there be a documentation in the Apple Developer documentation about that topic how to implement smb and how it works?

              Is there a date when iOS 13 will be published? Or will it be published when the new IPhone is buyable?

               

              Yours sincerely
              Tell aka. Relbot

                • Re: Access Windows/Mac Shared Folder Locally With smb from iOS
                  eskimo Apple Staff Apple Staff (11,815 points)

                  Is there a date when iOS 13 will be published?

                  iOS 13 GM Seed is currently available.  According to the [main Apple web site][ref13], iOS 13 is Available 9.19, which one must presume to mean 19 Sep 2019.

                  You said that it is only accessible via the iOS document architecture, so will I be possible in the way I programmed it (code I posted) or in a different way?

                  The code you posted is unlikely to ever be useful.  Even on a wide-open platform, like macOS, it’s not possible to access SMB resources via an smb URL.  Rather, SMB resources are accessed via a file URL, and then only when:

                  • The relevant volume is mounted

                  • You’ve been granted accessed by the sandbox (where that’s applicable)

                  iOS has no APIs for doing these things directly.  Rather, when your app integrates with the document architecture, iOS takes care of that for you, giving you back a URL that you can access.

                  I strongly recommend that you watch the video I referenced above; it’s a good introduction to how the various parts fit together.

                  Share and Enjoy

                  Quinn “The Eskimo!”
                  Apple Developer Relations, Developer Technical Support, Core OS/Hardware
                  let myEmail = "eskimo" + "1" + "@apple.com"