如何在IOS中使用共享表共享文件? [英] How do I share files using share sheet in IOS?

查看:546
本文介绍了如何在IOS中使用共享表共享文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在iPhone中使用Share Sheet功能在我的应用程序中本地共享一些文件。我在 UIWebview 中显示文件,当用户点击共享表时,我想显示选项(电子邮件,应用程序等)以共享<$上显示的文件C $ C>的UIWebView 。我知道我们可以使用

I want to share some files I have locally in my App using Share Sheet functionality in iPhone. I display the file in a UIWebview and when the user click the share sheet, I want to show options (email, whats app 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 string for example. How do I change this code to share documents?

提前致谢。

推荐答案

我想分享我的UIActivityViewController解决方案并将文本共享为图像文件。此解决方案适用于通过Mail共享甚至保存到Dropbox。

I want to share my solution of UIActivityViewController and sharing text as a image file. This solution works for sharing via Mail and even Save to Dropbox.

@IBAction func shareCsv(sender: AnyObject) {
    //Your CSV text
    let str = self.descriptionText.text!
    filename = getDocumentsDirectory().stringByAppendingPathComponent("file.png")

    do {
        try str.writeToFile(filename!, atomically: true, encoding: NSUTF8StringEncoding)

        let fileURL = NSURL(fileURLWithPath: filename!)

        let objectsToShare = [fileURL]
        let activityVC = UIActivityViewController(activityItems: objectsToShare, applicationActivities: nil)

        self.presentViewController(activityVC, animated: true, completion: nil)

    } catch {
        print("cannot write file")
        // failed to write file – bad permissions, bad filename, missing permissions, or more likely it can't be converted to the encoding
    }

}

func getDocumentsDirectory() -> NSString {
    let paths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)
    let documentsDirectory = paths[0]
    return documentsDirectory
}

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆