• <small id='yZILd'></small><noframes id='yZILd'>

      <bdo id='yZILd'></bdo><ul id='yZILd'></ul>

      1. <legend id='yZILd'><style id='yZILd'><dir id='yZILd'><q id='yZILd'></q></dir></style></legend>

        <i id='yZILd'><tr id='yZILd'><dt id='yZILd'><q id='yZILd'><span id='yZILd'><b id='yZILd'><form id='yZILd'><ins id='yZILd'></ins><ul id='yZILd'></ul><sub id='yZILd'></sub></form><legend id='yZILd'></legend><bdo id='yZILd'><pre id='yZILd'><center id='yZILd'></center></pre></bdo></b><th id='yZILd'></th></span></q></dt></tr></i><div id='yZILd'><tfoot id='yZILd'></tfoot><dl id='yZILd'><fieldset id='yZILd'></fieldset></dl></div>

        <tfoot id='yZILd'></tfoot>

        如何在 iOS 中使用共享表共享文件?

        时间:2023-10-21
        <legend id='M5LXF'><style id='M5LXF'><dir id='M5LXF'><q id='M5LXF'></q></dir></style></legend>
        <tfoot id='M5LXF'></tfoot>

          <small id='M5LXF'></small><noframes id='M5LXF'>

          <i id='M5LXF'><tr id='M5LXF'><dt id='M5LXF'><q id='M5LXF'><span id='M5LXF'><b id='M5LXF'><form id='M5LXF'><ins id='M5LXF'></ins><ul id='M5LXF'></ul><sub id='M5LXF'></sub></form><legend id='M5LXF'></legend><bdo id='M5LXF'><pre id='M5LXF'><center id='M5LXF'></center></pre></bdo></b><th id='M5LXF'></th></span></q></dt></tr></i><div id='M5LXF'><tfoot id='M5LXF'></tfoot><dl id='M5LXF'><fieldset id='M5LXF'></fieldset></dl></div>

                  <tbody id='M5LXF'></tbody>
                • <bdo id='M5LXF'></bdo><ul id='M5LXF'></ul>
                  本文介绍了如何在 iOS 中使用共享表共享文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我想使用 iPhone 上的共享工作表功能共享我在应用程序中本地拥有的一些文件.我在 UIWebView 中显示文件,当用户单击共享表时,我想显示选项(电子邮件、WhatsApp 等)以共享 UIWebView.我知道我们可以使用

                  I want to share some files I have locally in my app using Share Sheet functionality on iPhone. I display the file in a UIWebView and when the user clicks the share sheet, I want to show options (email, WhatsApp, etc. ) to share the file displayed on the UIWebView. I know that we can use

                  func displayShareSheet(shareContent:String) {                                                                           
                      let activityViewController = UIActivityViewController(activityItems: [shareContent as NSString], applicationActivities: nil)
                      presentViewController(activityViewController, animated: true, completion: {})    
                  }
                  

                  例如共享一个字符串.如何更改此代码以共享文档?

                  to share a string for example. How do I change this code to share documents?

                  推荐答案

                  Swift 4.2Swift 5

                  如果您已经在目录中有一个文件并想要共享它,只需将它的 URL 添加到 activityItems 中:

                  If you already have a file in a directory and want to share it, just add it's URL into activityItems:

                  let fileURL = NSURL(fileURLWithPath: "The path where the file you want to share is located")
                  
                  // Create the Array which includes the files you want to share
                  var filesToShare = [Any]()
                  
                  // Add the path of the file to the Array
                  filesToShare.append(fileURL)
                  
                  // Make the activityViewContoller which shows the share-view
                  let activityViewController = UIActivityViewController(activityItems: filesToShare, applicationActivities: nil)
                  
                  // Show the share-view
                  self.present(activityViewController, animated: true, completion: nil)
                  

                  如果需要制作文件:

                  我正在使用此扩展程序从 Data 创建文件(阅读代码中的注释以了解其工作原理):

                  I'm using this extension to make files from Data (read the comments in the code for explanation how it works):

                  如 typedef 的回答,获取当前文档目录:

                  As in the typedef's answer, get the current documents directory:

                  /// Get the current directory
                  ///
                  /// - Returns: the Current directory in NSURL
                  func getDocumentsDirectory() -> NSString {
                      let paths = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)
                      let documentsDirectory = paths[0]
                      return documentsDirectory as NSString
                  }
                  

                  数据的扩展:

                  extension Data {
                  
                      /// Data into file
                      ///
                      /// - Parameters:
                      ///   - fileName: the Name of the file you want to write
                      /// - Returns: Returns the URL where the new file is located in NSURL
                      func dataToFile(fileName: String) -> NSURL? {
                  
                          // Make a constant from the data
                          let data = self
                  
                          // Make the file path (with the filename) where the file will be loacated after it is created
                          let filePath = getDocumentsDirectory().appendingPathComponent(fileName)
                  
                          do {
                              // Write the file from data into the filepath (if there will be an error, the code jumps to the catch block below)
                              try data.write(to: URL(fileURLWithPath: filePath))
                  
                              // Returns the URL where the new file is located in NSURL
                              return NSURL(fileURLWithPath: filePath)
                  
                          } catch {
                              // Prints the localized description of the error from the do block
                              print("Error writing the file: (error.localizedDescription)")
                          }
                  
                          // Returns nil if there was an error in the do-catch -block
                          return nil
                  
                      }
                  
                  }
                  

                  使用示例:

                  分享图片文件:

                  // Your image
                  let yourImage = UIImage()
                  

                  在 png 文件中

                  // Convert the image into png image data
                  let pngImageData = yourImage.pngData()
                  
                  // Write the png image into a filepath and return the filepath in NSURL
                  let pngImageURL = pngImageData?.dataToFile(fileName: "nameOfYourImageFile.png")
                  
                  // Create the Array which includes the files you want to share
                  var filesToShare = [Any]()
                  
                  // Add the path of png image to the Array
                  filesToShare.append(pngImageURL!)
                  
                  // Make the activityViewContoller which shows the share-view
                  let activityViewController = UIActivityViewController(activityItems: filesToShare, applicationActivities: nil)
                  
                  // Show the share-view
                  self.present(activityViewController, animated: true, completion: nil)
                  

                  在 jpg 文件中

                  // Convert the image into jpeg image data. compressionQuality is the quality-compression ratio in % (from 0.0 (0%) to 1.0 (100%)); 1 is the best quality but have bigger filesize
                  let jpgImageData = yourImage.jpegData(compressionQuality: 1.0)
                  
                  // Write the jpg image into a filepath and return the filepath in NSURL
                  let jpgImageURL = jpgImageData?.dataToFile(fileName: "nameOfYourImageFile.jpg")
                  
                  // Create the Array which includes the files you want to share
                  var filesToShare = [Any]()
                  
                  // Add the path of jpg image to the Array
                  filesToShare.append(jpgImageURL!)
                  
                  // Make the activityViewContoller which shows the share-view
                  let activityViewController = UIActivityViewController(activityItems: filesToShare, applicationActivities: nil)
                  
                  // Show the share-view
                  self.present(activityViewController, animated: true, completion: nil)
                  

                  分享文本文件:

                  // Your String including the text you want share in a file
                  let text = "yourText"
                  
                  // Convert the String into Data
                  let textData = text.data(using: .utf8)
                  
                  // Write the text into a filepath and return the filepath in NSURL
                  // Specify the file type you want the file be by changing the end of the filename (.txt, .json, .pdf...)
                  let textURL = textData?.dataToFile(fileName: "nameOfYourFile.txt")
                  
                  // Create the Array which includes the files you want to share
                  var filesToShare = [Any]()
                  
                  // Add the path of the text file to the Array
                  filesToShare.append(textURL!)
                  
                  // Make the activityViewContoller which shows the share-view
                  let activityViewController = UIActivityViewController(activityItems: filesToShare, applicationActivities: nil)
                  
                  // Show the share-view
                  self.present(activityViewController, animated: true, completion: nil)
                  

                  其他文件:

                  您可以从 Data 格式的任何内容创建文件,据我所知,Swift 中的几乎所有内容都可以转换为 Data,例如 String, Int, Double, Any...:

                  You can make a file from anything which is in Data format and as far as I know, almost everything in Swift can be converted into Data like String, Int, Double, Any...:

                  // the Data you want to share as a file
                  let data = Data()
                  
                  // Write the data into a filepath and return the filepath in NSURL
                  // Change the file-extension to specify the filetype (.txt, .json, .pdf, .png, .jpg, .tiff...)
                  let fileURL = data.dataToFile(fileName: "nameOfYourFile.extension")
                  
                  // Create the Array which includes the files you want to share
                  var filesToShare = [Any]()
                  
                  // Add the path of the file to the Array
                  filesToShare.append(fileURL!)
                  
                  // Make the activityViewContoller which shows the share-view
                  let activityViewController = UIActivityViewController(activityItems: filesToShare, applicationActivities: nil)
                  
                  // Show the share-view
                  self.present(activityViewController, animated: true, completion: nil)
                  

                  这篇关于如何在 iOS 中使用共享表共享文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:iOS 通过 UIWebView 访问 Safari Reader 功能 下一篇:使用 UIWebView loadRequest 发送自定义标头

                  相关文章

                        <bdo id='kY0ln'></bdo><ul id='kY0ln'></ul>

                    1. <legend id='kY0ln'><style id='kY0ln'><dir id='kY0ln'><q id='kY0ln'></q></dir></style></legend>
                      <tfoot id='kY0ln'></tfoot>
                    2. <small id='kY0ln'></small><noframes id='kY0ln'>

                    3. <i id='kY0ln'><tr id='kY0ln'><dt id='kY0ln'><q id='kY0ln'><span id='kY0ln'><b id='kY0ln'><form id='kY0ln'><ins id='kY0ln'></ins><ul id='kY0ln'></ul><sub id='kY0ln'></sub></form><legend id='kY0ln'></legend><bdo id='kY0ln'><pre id='kY0ln'><center id='kY0ln'></center></pre></bdo></b><th id='kY0ln'></th></span></q></dt></tr></i><div id='kY0ln'><tfoot id='kY0ln'></tfoot><dl id='kY0ln'><fieldset id='kY0ln'></fieldset></dl></div>