iOS share pdf with webView - html

I have a problem with webView. I completely load the content of webView:
func openUrl(fileURL: URL, htmlString: String) {
webView.loadHTMLString(htmlString, baseURL: fileURL)
}
but then I want to share content.
When I tried:
let activityController = UIActivityViewController(activityItems: [webView.request?.url], applicationActivities: nil)
let popover = activityController.popoverPresentationController
popover?.sourceView = self.view
popover?.barButtonItem = self.navigationItem.rightBarButtonItem
present(activityController, animated: true, completion: nil)
It didn't work, there wasn't image what i load from function loadHTMLString
How can i share it?
Does anybody know how this problem solve?

Related

UIViewController presented without background

When im present new VC background is clear and I can see previous VC.
Second VC is empty (without any view)
How FIX?
let vc = AddScrollVC()
let navController = UINavigationController(rootViewController: vc)
navController.modalPresentationStyle = .overFullScreen
present(navController, animated: true)
Second VC is presented, shown only button in navBar
You can solve the issue with replacing this code
let VC = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "PresentVC") as! PresentVC
VC.modalPresentationStyle = .overFullScreen
self.present(VC, animated: true, completion: nil)
Change the code like this, This is work fine for me
let VC = PresentVC()
VC.modalPresentationStyle = .overFullScreen
self.present(VC, animated: true, completion: nil)

How to send data from AppDelegate to my subclass of ViewController in swift 4

I am using Swift 4.1 and I am wondering how to pass the url data that is given here:
func application(_ app: UIApplication, open url: URL,
options: [UIApplicationOpenURLOptionsKey : Any]) -> Bool {
}
into my subclass of UIViewController I called HotSpotRISViewController. Here is what I attempted:
func application(_ app: UIApplication, open url: URL,
options: [UIApplicationOpenURLOptionsKey : Any]) -> Bool {
self.window = UIWindow(frame: UIScreen.main.bounds)
let vc = HotSpotRISViewController()
vc.setURL(theURL: url)
let navigationController = UINavigationController(rootViewController: vc)
self.window!.rootViewController = navigationController
}
My HotSpotRISViewController contains this function:
func setURL(theURL: URL) {
self.url = theURL
print(self.url ?? "nil")
}
Inside my HotSpotRISViewController I have a property of type URL ready to be set. The above code correctly passes the info, because my print statement prints out the URL, but I get a black screen instead of my app. What am I missing? And then on the other hand, the following makes the app start up correctly but I have no idea how to pass the url data using this method:
func application(_ app: UIApplication, open url: URL,
options: [UIApplicationOpenURLOptionsKey : Any]) -> Bool {
let mainStoryboard = UIStoryboard(name: "Main", bundle: nil)
let initialViewController = mainStoryboard.instantiateInitialViewController()
self.window = UIWindow(frame: UIScreen.main.bounds)
self.window?.rootViewController = initialViewController
self.window?.makeKeyAndVisible()
}
I'm wondering how I can get both my app started up correctly without a black screen and pass the url data. Thank you.
You can add a global variable in your appDelegate file and fetch it with your setURL function :
In your AppDelegate.swift
var myurl: URL!
....
func application(_ app: UIApplication, open url: URL,
options: [UIApplicationOpenURLOptionsKey : Any]) -> Bool {
self.myurl = url
let mainStoryboard = UIStoryboard(name: "Main", bundle: nil)
let initialViewController = mainStoryboard.instantiateInitialViewController()
self.window = UIWindow(frame: UIScreen.main.bounds)
self.window?.rootViewController = initialViewController
self.window?.makeKeyAndVisible()
}
...
In your HotSpotRISViewController.swift
func setURL(theURL: URL) {
DispatchQueue.main.async {
if let appDelegate = UIApplication.shared.delegate as? AppDelegate {
self.url = appDelegate.myurl
}
}

Redirect to specific view

I want to redirect the user of my app to a specific view if he is logged in or not. This is my code:
override func viewDidLoad() {
super.viewDidLoad()
if FIRAuth.auth()?.currentUser != nil {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let viewController = storyboard.instantiateViewController(withIdentifier :"LoginViewController") as! LoginViewController
self.present(viewController, animated: true)
}
else {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let viewController = storyboard.instantiateViewController(withIdentifier :"SWRevealViewController") as! SWRevealViewController
self.present(viewController, animated: true)
}
}
If I open my app I always get a white screen.
Does anyone know my mistake?
It worked. I didn't connected the navigation controller in the storyboard. The code itself works finde.

Pdf Created from Html using UIViewPrintFormatter not showing local images

I am creating pdf from an html file with images.I tried creating from from Html as well as from UIWebView by loading the html in to web view .The following are the methods use for both approach
1.From html
func exportHTMLContentToPDF(HTMLContent: String) {
let printPageRenderer = CustomPrintPageRenderer()
let printFormatter = UIMarkupTextPrintFormatter(markupText: HTMLContent)
printPageRenderer.addPrintFormatter(printFormatter, startingAtPageAtIndex: 0)
let pdfData = drawPDFUsingPrintPageRenderer(printPageRenderer)
pdfData.writeToFile(pdfPath(), atomically: true)
}
2.From UiWebView by loading Html
func createPdfFromViewFormatter(viewFormatter:UIViewPrintFormatter) {
let printPageRenderer = CustomPrintPageRenderer()
printPageRenderer.addPrintFormatter(viewFormatter, startingAtPageAtIndex: 0)
let pdfData = drawPDFUsingPrintPageRenderer(printPageRenderer)
pdfData.writeToFile(pdfPath(), atomically: true)
}
Pdf creation method`
private func drawPDFUsingPrintPageRenderer(printPageRenderer: UIPrintPageRenderer) -> NSData! {
let data = NSMutableData()
UIGraphicsBeginPDFContextToData(data, CGRect(x: 0.0, y: 0.0, width: printPageRenderer.printableRect.width, height: printPageRenderer.printableRect.height), nil)
printPageRenderer.prepareForDrawingPages(NSRange(location: 0, length:printPageRenderer.numberOfPages() ))
for i in 0..<printPageRenderer.numberOfPages() {
UIGraphicsBeginPDFPage()
printPageRenderer.drawPageAtIndex(i, inRect: UIGraphicsGetPDFContextBounds())
}
UIGraphicsEndPDFContext()
return data
}
The issue is the image in the html is is not shown in the created pdf.But it is displayed while loading it in a UIWebView.
I'm having the same problem :
IOS Swift sharing PDF documents with images
I believe it's a bug. The only solution I found so far is to create a dummy UIWebView that I hide. I load the HTML in the UIWebView and then create the PDF. Here is my code :
override func viewDidLoad() {
super.viewDidLoad()
hiddenWebView.loadHTMLString(htmlText, baseURL: nil)
hiddenWebView.isHidden = true
}
override func viewDidAppear(_ animated: Bool) {
pdfDocument = getPdf()
let tmpDirectoryURL = URL(fileURLWithPath: NSTemporaryDirectory())
let fileURL = tmpDirectoryURL.appendingPathComponent("document.pdf")
pdfDocument.write(to: fileURL, atomically: false)
documentWebView.loadRequest(URLRequest(url: fileURL))
}
I hope it helps.
I faced same issue and finally I got the solution. Hope anyone help this solution. Just Replace createPdfFromViewFormatter methods 's code.
`
func createPdfFromViewFormatter(viewFormatter:UIViewPrintFormatter) {
// 2. Assign print formatter to UIPrintPageRenderer
let render = UIPrintPageRenderer()
render.addPrintFormatter(formatter, startingAtPageAt: 0)
// 3. Assign paperRect and printableRect
let page = CGRect(x: 0, y: 0, width: 595.2, height: 841.8) // A4, 72 dpi
let printable = page.insetBy(dx: 0, dy: 0)
render.setValue(NSValue(cgRect: page), forKey: "paperRect")
render.setValue(NSValue(cgRect: printable), forKey: "printableRect")
// 4. Create PDF context and draw
let pdfData = NSMutableData()
UIGraphicsBeginPDFContextToData(pdfData, CGRect.zero, nil)
for i in 1...render.numberOfPages {
UIGraphicsBeginPDFPage();
let bounds = UIGraphicsGetPDFContextBounds()
render.drawPage(at: i - 1, in: bounds)
}
UIGraphicsEndPDFContext()
// 5. Save PDF file
let path = "\(NSTemporaryDirectory())\(filename).pdf"
pdfData.write(toFile: path, atomically: true)
}
`
Call createPdfFromViewFormatter method Like That
let pdfData = createPdfFromViewFormatter(viewFormatter: webView!.viewPrintFormatter())

Custom URL scheme for Pinterest on iOS Safari to open board URL?

I can't for the life of me find out how to simply open a Pinterest board (by launching the APP if it's installed) through an anchor within HTML.
I've taken a look at: https://developers.pinterest.com, but still can't find what i'm looking for. It seems most of the documentation out there is geared more towards the action of pinning an item, rather than viewing.
All I want to do is open a users profile. For example, i've dug up the following which works great for alternative social media links:
Facebook
Twitter
Instagram
While I'm able to launch the Pinterest app, I'm unsure what the parameters would be passed to open a specific profile / board on Pinterest:
Pinterest
Thanks to: http://handleopenurl.com/ for help with the above, but it seems Pinterest is absent. Any clues?
Looks like Pinterest has published an iOS SDK, and defined some URLS:
Pin
pinterest://pin/285063851385287883/
Board
pinterest://board/meaghanror/cats-cats-cats/
User
pinterest://user/carlrice/
See more at https://developers.pinterest.com/ios/
Working Code for Swift 4.x
func openSocialMedia(appURI : String, webURI : String){
let appURL = NSURL(string: appURI)!
let webURL = NSURL(string: webURI)!
if UIApplication.shared.canOpenURL(appURL as URL) {
if #available(iOS 10.0, *) {
UIApplication.shared.open(appURL as URL, options: [:], completionHandler: nil)
} else {
UIApplication.shared.openURL(appURL as URL)
}
} else {
//redirect to safari because the user doesn't have Instagram
if #available(iOS 10.0, *) {
UIApplication.shared.open(webURL as URL, options: [:], completionHandler: nil)
} else {
UIApplication.shared.openURL(webURL as URL)
}
}
}
How to call
#IBAction func didPressInstagram(_ sender: Any) {
let appHandle = "instagram://user?username=erashukr"
let webHandle = "https://instagram.com/erashukr"
self.openSocialMedia(appURI: appHandle, webURI: webHandle)
}
#IBAction func didPressGplus(_ sender: UIButton) {
let appHandle = "gplus://plus.google.com/u/0/+Ashutoshkumarrr"
let webHandle = "https://plus.google.com/u/0/+Ashutoshkumarrr"
self.openSocialMedia(appURI: appHandle, webURI: webHandle)
}
#IBAction func didPressFacebook(_ sender: Any) {
let appHandle = "fb://profile?id=erashukr"
let webHandle = "https://facebook.com/erashukr"
self.openSocialMedia(appURI: appHandle, webURI: webHandle)
}
#IBAction func didPressTwitter(_ sender: UIButton) {
let appHandle = "twitter://user?screen_name=ace_ashu"
let webHandle = "https://twitter.com/ace_ashu"
self.openSocialMedia(appURI: appHandle, webURI: webHandle)
}
#IBAction func didPressPinterest(_ sender: UIButton) {
let appHandle = "pinterest://user/apple"
let webHandle = "https://www.pinterest.com/apple/"
self.openSocialMedia(appURI: appHandle, webURI: webHandle)
}
100% Working