Parse and Viewing Files on WebView - swift

I have some files between the extensions of pdf and doc on parse database
I have a tableview which populates from parse query so when i click on them i want to display the file for that row in a new webview. I'm on final stage now but i could not find a way to get file from parse and view it on webview via Swift.
No issue on code so far my tableview populates and cells are making segue to the new empty webview, of course without file. By the way i'm not sure webview can view the doc files so if it can't please advice me the best way to do it.
Thank you

You'll need to retrieve the URL for the file object, and tell the web view to load that URL. Lets assume you have a Place object that has a PFFile related to it via an attribute called brochure. You can get your PFFile object that describes the file (doesn't contain the actual file) with a call like:
let placeBrochure = myPlace["brochure"] as PFFile
Then, you'll need to extract the url from that PFFile
let brochureURL = NSURL(string: placeBrochure.url)! // Probably shouldn't force unwrap
and load it into your webview
let request = NSURLRequest(URL: brochureURL)
webview.loadRequest(request)
For your specific case, you'll need to extract the equivalent of the myPlace object from some sort of array that represents the data that's used to populate your tableview, and this will probably need to happen in didSelectRow.
In terms of support for Microsoft Word .doc format files, it looks like UIWebView should be able to handle them: https://developer.apple.com/library/ios/qa/qa1630/_index.html

Related

how can i let a user pick a local document?

I'm supposed to test PSPDFKit in a way that lets the user open a locally saved pdf file.
For this i require the files url(e.g. path, i guess). How can i let the user pick a local document?
Can anybody help me out?
I'm using storyboard and swift.
You can present a PDF document in PSPDFKit using the PDFViewController like so:
// Create the `Document`.
let fileURL = Bundle.main.url(forResource: "Document", withExtension: "pdf")!
let document = Document(url: fileURL)
// Create the PDF view controller.
let pdfController = PDFViewController(document: document)
// Present the PDF view controller within a `UINavigationController` to enable the toolbar.
present(UINavigationController(rootViewController: pdfController), animated: true)
For more details, take a look at the following guide here.
Also, to present a document that your end-user has selected, consider using a document picker or a file browser. Please refer to DocumentPickerSidebarExample.swift from Catalog and SwiftUI Document Browser for runnable example projects.
In the future, please reach out to our support portal at pspdfkit.com/support/request/ - we're happy to provide support there for our commercial SDK.

How to fetch image size and image resolution from HTML content loaded from UIWebview in Swift?

I am using 'SwiftSoup' SDK to parse HTML content from UIWebView. I want the image size and resolution for each and every image fetched from HTML content. How to get the image size and resolution as soon as an image is loaded and from HTML content received from SwiftSoup?
Assuming you know when all images from the HTML are loaded, here's a javascript code that can be used to grab a list of sizes for all images:
let script = "JSON.stringify([].slice.call(document.getElementsByTagName('img')).map(function(img) { return {url: img.src, width: img.clientWidth, height: img.clientHeight} }))"
Executing this javascript via stringByEvaluatingJavaScript(from:) should give you a JSON that you can parse to obtain the desired info.
For example the output for this page is
[{"url":"https://i.stack.imgur.com/rQQMq.png?s=48&g=1","width":24,"height":24},{"url":"https://www.gravatar.com/avatar/46be6ae9d14855f5ab1aaf1d510ab8bc?s=32&d=identicon&r=PG&f=1","width":32,"height":32},{"url":"https://clc.stackoverflow.com/impression.gif?an=49ocs77evOWqh-6LVv8gJiaWI_07xL9f6GBcyDt7dl3NWtNsxo9-twOOBzKwMPRsYGBosGdkZGCRZuqawDHjMeecj4L_WRhSDiyObbJ82JRhP5vp7j6Og2e4ru0W9mRhmNs0gbnc8pVcsT0jc2Z-sTTTwxaOliucyy4LsrAwFLRcfRVt-e6KDEjTco4LjVyda4XkgZqaJcJzLa9Oj4RoMrJk4eJgYpBgYGDg4o1ij5VnYhAHsvk4o9gSmZgYRIBsfmYbtrvCTAxiIHH2ALapwv3t9i1z_NRrAA&md=291","width":0,"height":0}]
I'd recommend though to switch to WKWebView since it's recommended also by Apple, and because stringByEvaluatingJavaScript(from:) is blocking (WKWebView provides an async version of this function, and one that also will do the parsing for you, so you can remove the JSON.stringify() call from the script).

Paste from the clipboard in iOS

So I have 2 apps. One is a sensors app (built with XCode) that records data (text) with hardware wireless sensors. The other is a checklist/reference manual (built with Titaniam Appcelerator). Using custom URL schemes, they can instantiate each other.
What I am trying to do is paste any text data the sensors app copies to the clipboard into a text field in the reference manual app. I have a UIWebview showing html pages (the checklist) with a text box displayed now. To demo the capability, I have to touch the field and select paste. I was thinking that javascript might work, but all my research poo poo's that idea. Any thoughts about how to grab the text that is on the clipboard and display it programmatically in the reference manual app without having to touch the field and select paste?
Should I even be looking at the clipboard or should I be looking into modifying the custom URL scheme to pass data that way instead?
To get the text from the clipboard:
UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
NSString *string = pasteboard.string;
if (string) {
// Do something
}
For more funcional communication between apps, take a look at URL Schemes.
So, I figured out a way to pass the data in the url with this tutorial. At the bottom it describes how to pass data after you set up the URL id for each app. Hope this helps someone.
Pasting in Swift
Get the pasteboard string with UIPasteboard.generalPasteboard().string.
The string is optional, so it must be unwrapped before being used.
if let pasteboardString = UIPasteboard.generalPasteboard().string {
// use the string, for example:
myTextView.insertText(pasteboardString)
}
Note
The original question is asking for something more complex than this. However, most people come here based on the question title rather than question content, so that I what I am answering here.

AFNetworking stop downloading images with same url

In my app I download images and insert them into a table view. In my case there could be several same images with identical URLs. To reduce the server overloa, I'd like to download each image only once. Is there built-in support for this? Or what is the best way to achieve my goal?
Create a NSMutableDictionary and as you download an image and place it in the table view, add the URL as the key in the dictionary and the table view row as the value. Before you download an image, check to make sure that the dictionary doesn't already contain the URL (if ([[dictionary allKeys] containsObject:url]). If it doesn't, download the image. If it does, then grab the row number and make the empty row's image the same as the image of the row in the dictionary.
http://example.com/image => 1

Drawing to existing PDF

When i did CGContextCreateWithURl(url) it created empty context even though i passed url of existing file. How to get the context of existing PDF file?
From my experience you can't actually draw directly to an existing pdf:
Convert pdf to image
Draw new content to that image
Convert appended image to pdf
Not the smoothest, but it works.
the URL isn't an input. It's where you want the context to write the resulting PDF to.
From the documentation:
url
A Core Foundation URL that specifies where you want to place the resulting PDF file.