How to share the contents of a UIWeb view controller to Social sites?

529 Views Asked by At

I'm confused about how to share the contents of a UIWebView in Swift to Social sites such as Facebook and Twitter. I have watched many videos and articles on how to share them. But couldn't find an answer.

I'm making a News App on Xcode where the view controller holds a UIWebView that displays the article for that particular day. My UIWebView loads the data successfully, but I can't find the code to share the contents to Social apps.

Kindly help.

1

There are 1 best solutions below

2
Vanita L. On

You can share the Url loaded in web view or else if you want share the contents you can check in cache for same, if no content found you will need to download the contents from url.

like:

if let url = aWebView.request?.url {
        //check in response cache for the pdf content else download from url
        let response = URLCache.shared.cachedResponse(for: aWebView.request!)
        if let pdfData = response?.data {

            //share content from pdfData to social
        } else if let pdfData = NSData(contentsOf: url) {
            //print("from url")
            //share content from pdfData social
        } else {
            self.showAlert(with: "Sorry", message: "Unable to share at this , please try again")
        }
    } else {
        //nothing found to share
        self.showAlert(with: "Sorry", message: "Unable to share at this , please try again")
    }

Edit: Share Url

for social share there is property url or else you can set it initial text also, as per your need

you can get the url like:

let url = aWebView.request?.url

and share this obtained url.