I'm showing a pdf file inside a UIDocumentInteractionController, like this:
let docController = UIDocumentInteractionController(url: documentsURL)
let url = NSURL(string:"itms-books:");
if UIApplication.shared.canOpenURL(url! as URL) {
docController.delegate = self
docController.presentPreview(animated: true)
}
I need to automatically scroll to the last page when the controller is shown, is there a way to do that? I didn't find one. Thanks to anyone who will send help!
I am afraid there is not a straight forward solution for this, even the documentation says it's a UIViewController but Xcode shows it is inherited from NSObject.
I'd recommend either rendering the PDF on a WKWebView or a UIScrollView. This will give you a room to wiggle.
Related
I am new to iOS development. In my project i am displaying a exerciseFilePath on tableview.
the response is given below.
"exerciseId" : 1,
"exerciseName" : "Fitness Exercise",
"exerciseFilePath" : "\/p\/pdf\/exercise_pdf\/fitness_exercise.pdf"
i need to display the pdf in another view on didSelectRowAtIndexpath.
i Dont know how to display the pdf and what are steps to be followed to display that pdf.
I hope you understand my problem. please help me how I can do this.
Why don't you use QLPreviewController or UIDocumentInteractionController?
Now in your case you can do it by using webView:
let req = NSURLRequest(url: pdf) //pdf is your pdf path
let webView = UIWebView(frame: CGRect(x:0,y:0,width:self.view.frame.size.width,height: self.view.frame.size.height-40)) //Adjust view area here
webView.loadRequest(req as URLRequest)
self.view.addSubview(webView)
Some tutorials:
QLPreviewController example
UIDocumentInteractionController example
I would like to choose a video from the library(which I have completed) NOW, I want to take that video and display it in a view controller. What do I do? I have looked everywhere and have found nothing.
Here is my code for trying to get the file name of a selected video from the library, which I am trying to display in a webview. It does not work.
videoURL = info[UIImagePickerControllerReferenceURL] as! NSURL?
let fileURL = NSURL(fileURLWithPath: " (videoURL)")
videoView.loadHTMLString( " ", baseURL : nil)
Any help is much appreciated. Thanks!
You'll need to implement the delegate methods.. Check this out:
How to select any Video or Movie file from UIImagePickerController
I would like to ask you about a problem that I'm facing with a project. The problem is that i'm trying to show a UIWebView control, I'm loading a video streaming into the web view , this is the code:
if(self.cameraUrl != nil) {
let url = NSURL (string: self.cameraUrl!)
let requestObj = NSURLRequest(URL: url!)
self.view_webvideo.loadRequest(requestObj)
self.view_webvideo.contentMode = UIViewContentMode.Center;
let zoom = self.view_webvideo.bounds.size.width / self.view_webvideo.scrollView.contentSize.width
self.view_webvideo.scrollView.setZoomScale(zoom, animated: true)
}
As you could see in the code I had research about that and try a lot of things but i can't scale the content, the video have width=360 and height=640, I can't change the video size because it is a third party service.
Also i'm using the property: Scales Page To Fit = true
I want the video scale to fix into the webview component and don't show any scroll
I hope somebody could help me with that.
Thank you in advanced
Try replacing the last three lines with this (similar to what manman said)
self.view_webvideo.stringByEvaluatingJavaScriptFromString("document.getElementsByTagName('video')[0].style.width='100%'")
I have a sign up and sign in view controller and a moving gif background along with other storyboarded UI elements.
Currently I have the gif coding on each view controller obviously making the gif restart as I segue between each view controller and also more concerning having a quick pop of white before it loads whenever moving between the pages.
What can I add to my code to:
1. Prevent the white pop when it segues
2. Swapping between view controllers smoothly with the gif just continuing as if nothing has happened, so it looks like you move the UI not the page.
Look at Vine Sign In / Sign Up for reference as it basically functions in a similar way and want the background gif to function as it does on Vine.
override func viewDidLoad() {
super.viewDidLoad()
let filePath = NSBundle.mainBundle().pathForResource("beachwater", ofType: "gif")
let gif = NSData(contentsOfFile: filePath!)
let webViewBG = UIWebView(frame: self.view.frame)
webViewBG.loadData(gif!, MIMEType: "image/gif", textEncodingName: "UTF-8", baseURL: NSURL(string: "")!)
webViewBG.userInteractionEnabled = false;
webViewBG.layer.zPosition = -3.0;
self.view.addSubview(webViewBG)
As a starting point don't use a UIWebView for this. It is the reason you are seeing a white background just before the gif, the web view has been rendered on screen then shortly after the gif is rendered.
I'd recommend separating out the frames of the gif and loading them into a UIImage (and finally into a UIImageView) using one of the following techniques:
animatedImageNamed:duration:
animatedImageWithImages:duration:
If you don't want to separate the frames, use a gif rendering library such as FLAnimatedImage
I'm having an issue when I try to Launch Instagram from my app. Everything works and I'm able to launch IG and even see the photo I sent etc.
The problem is that the UIDocumentInteractionController is crashing my app. And YES I have done my research.
I've seen the posts LIKE THIS that indicate that this is an Apple bug as as long as you can fix the crash, you should be able fine and ignore the Launch Services message.
The problem is I am still having the crash and trying to figure out how to resolve it.
I found a post that talks about adding an IF-STATEMENT after presenting the ViewController HERE, this post was written in Objective-C, and the example was not for a UIDocumentInteractionController.
I tried to take a stab at it in Swift, but it is still not working out for me. Would appreciate if someone can help out.
dic = UIDocumentInteractionController(URL: imageURL!)
dic.delegate = self
var annotationDictionary: [String: String] = ["InstagramCaption": "Test"]
dic.annotation = annotationDictionary
dic.UTI = "com.instagram.exclusivegram"
dic.presentOpenInMenuFromRect(CGRectMake(1, 1, 1, 1), inView: self.view, animated: true)
if dic.respondsToSelector("popoverPresentationController") {
Scripts.log("InstagramVC Did Respond to popoverPresentationController")
var popoverController: UIPopoverPresentationController = self.popoverPresentationController!
popoverPresentationController?.sourceView = self.view
}
The fix in my case was to declare the UIDocumentInteractionController variable as part of the viewcontroller's class instead of creating it in the same function where I set up the annotation and UTI and called .presentOpenInMenuFromRect
So near the top of my class outside of any functions I declared the variable:
var docController = UIDocumentInteractionController()
And then when I was ready to use it, I configured everything about the already existing UIDocumentInteractionController instead of creating one:
docController = UIDocumentInteractionController(URL: imageURL!)
docController.UTI = "com.instagram.exclusivegram"
docController.delegate = self
docController.annotation = ["InstagramCaption":"Text"]
docController.presentOpenInMenuFromRect(rect, inView: self.view, animated: true)
The app stopped crashing and Instagram now loads with the image/text assigned. 😃👍
I found the suggestion that led me to this fix here: https://stackoverflow.com/a/16057399/428981 and then adapted for Swift
It sounds like your instance of UIDocumentInteractionController is going out of scope. Try making it a property on the class or some other way to retain it.
I had the same problem: sending the picture to Instagram worked but instantaneously crashed my app. I think it has something to do with how the UIDocumentInteractionController is handled by the system once another app opens. If you try to send the picture via the embedded Facebook or Twitter frameworks that open a popover on top of your app, then no crash happens...
In any case the way I finally made it work is by NOT declaring my viewController as delegate, so:
// dic.delegate = self
The downside being that you can't use any of the delegate methods. In my case I wasn't using them anyway.