Encrypted pdf files crashes on UIWebView in iOS 7 - ios

I am trying to open pdf files in my UIWebView in below steps.
self.docViewer is a strong property.
self.docViewer.delegate = self;
NSURLRequest *urlReq = [NSURLRequest requestWithURL:[NSURL fileURLWithPath:docurl]];
[self.docViewer loadRequest:urlReq];
It opens normal pdf files and password protected files with no issues.
When the file is an encrypted one, it crashes after executing delegate methods.
- (void)webViewDidFinishLoad:(UIWebView *)webView
with below log
unsupported security handler `Adobe.PubSec'.
FlateDecode: decoding error: incorrect header check.
FlateDecode: decoding error: incorrect header check.
unsupported security handler `Adobe.PubSec'.
I checked all possible ways mentioned in suggested treads. I didn't find any solution.
Could anyone please help me out of this crash.Thanks a Lot

Related

WKWebView gives SecurityError when bundling html and javascript with app

We are trying to migrate a hybrid app from UIWebView (iOS < 8) to WKWebView (iOS 8), but we are getting SecurityErrors when trying to store stuff using the DOM WebDatabase API (i.e. 'web sql databases').
The following throws an error if the index.html has been loaded from a bundled file with the app
// throws SecurityError: DOM Exception 18
var db = openDatabase('mydb', '1.0', 'key value store', 1);
The same code works fine with UIWebView. I can fallback to using Local Storage for some reason, but using WebSQL databases is a no go. I can only speculate that this has something to do with the same origin policy or something related.
The funny thing is that loading index.html from the network works fine :-/
Any clues as to how I can work around this? Any options to set on the WKWebView that fixes it?
This is how we load the web related stuff:
NSString *htmlPath = [[NSBundle mainBundle] pathForResource:#"index" ofType:#"html"];
NSURL *baseURL = [NSURL fileURLWithPath:htmlPath];
NSURLRequest *request = [NSURLRequest requestWithURL:baseURL];
WKWebViewConfiguration *config = [[WKWebViewConfiguration alloc] init];
[config.userContentController addScriptMessageHandler:self.myCallbacks name:#"NativeApp"];
self.webView = [[WKWebView alloc] initWithFrame:self.view.frame configuration:config];
[self.webView loadRequest:request];
The html file simply loads a javascript file that has a relative path, "myCode.js".
There is an issue (OpenRadar) with WKWebView in iOS 8.0 (and 8.1 B1, I think) that prevents it from loading local files. It might be affecting local storage too. See this question for more details.
You can fix this by adding the following method to the UIDelegate of your WKWebView.
- (void) _webView:(WKWebView *)webView
decideDatabaseQuotaForSecurityOrigin:(WKSecurityOrigin *)securityOrigin
currentQuota:(unsigned long long)currentQuota
currentOriginUsage:(unsigned long long)currentOriginUsage
currentDatabaseUsage:(unsigned long long)currentUsage
expectedUsage:(unsigned long long)expectedUsage
decisionHandler:(void (^)(unsigned long long newQuota))decisionHandler {
decisionHandler(1024*1024*50); //default to 50MB
}
It gives all databases a quota of 50MB, instead of the default of 0 which allows them to be opened. This behavior isn't documented, so I don't know where Apple stands with this.
Also, it appears this issue will be fixed in iOS 10.
I've made a 'plugin' that allows you to use WebSQL (more an implementation of it) in the WKWebView. It can be found here
https://github.com/ajwhiteway/WKWebSQL
import WKWebSQL
.
.
.
var webView = WKWebView(frame: view.frame, configuration: WKWebViewConfiguration())
WKWebSQL.LoadPlugin(webView)
To get it loaded into the page. Versioning isn't really supported at this time. Feel free to add it.

Random error on loading a static resource with a UIWebView

I'm using a UIWebView to load a static resource shipped within my application bundle. Sometimes, I have no clear what the problem could be, I receive the following error within the delegate method - (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
NSUnderlyingError = "Error Domain=kCFErrorDomainCFNetwork Code=-1001
The resource is loaded through a NSURLRequest where I set up a timeout interval of 10 seconds, but that interval is not followed. In fact, in the debug console I'm able to see that the error delegate is called after about 2 seconds.
NSURL *htmlFile = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"root" ofType:#"html"] isDirectory:NO];
NSURLRequest* htmlRequest = [NSURLRequest requestWithURL:htmlFile cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:10.0];
[webView loadRequest:htmlRequest];
The fact is that I cannot always replicate the problem I have. Any suggestions?
P.S. I'm working with an app that runs from 4.3. The problem is also with iOS 7.
According to apple CFNetwork Error Codes Reference this code is a timeout error:
kCFURLErrorTimedOut = -1001
Timeout are not always easy to reproduce most likely you should extend the timeout value when you init the NSURLRequest.
According to the same docs you can query the object for additional information.
For example:
if (CFEqual(CFErrorGetDomain(err), kCFErrorDomainCFNetwork) && CFErrorGetCode(err) == kCFHostErrorUnknown) {
CFDictionaryRef userInfo = CFErrorCopyUserInfo(err);
CFNumberRef number = (CFNumberRef) CFDictionaryGetValue(userInfo, kCFGetAddrInfoFailureKey);
...
CFRelease(userInfo);
}
I suppose that the resource you are looking for is not available. Be sure that the path is correct (ios is case sensitive) and if you are downloading some contents from internet be sure that the download is completed and the file is copied from the temporary folder to the final destination.

reading PDF protected by password using UIWebview

I'm using this code to load my PDF into webView:
NSURL *targetURL = [NSURL URLWithString:#"http://example.com/demo.pdf"];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:targetURL];
[viewWeb loadRequest:requestObj];
and everything works fine, but there's one problem to resolve.
My demo.pdf is protected by password, of course I know this password but don't want to input it everytime when pdf is loaded.
Can I set my password as variable/string and load PDF without type password?
I don't think there's anything as straight forward as telling the webview what the password is, but instead I would download the PDF file manually (NSURLConnection or something), then unlock it using CGPDFDocumentUnlockWithPassword(doc, pass).
You could then generate an unlocked version as NSData in memory and load that into the web view (not password protected at this point)
You can draw it into NSData using the following (skip over the parts about custom drawing a red box on top):
http://b2cloud.com.au/how-to-guides/drawing-over-a-pdf-in-ios-pdf-template
Of course if your PDF is huge or has a ton of images this could take some time...
CGPDFDocument reference:
https://developer.apple.com/library/ios/documentation/graphicsimaging/reference/CGPDFDocument/Reference/reference.html

Phonegap / Cordova ios load remote url how to fallback to local index.html on error?

I use remote url for contents in my cordova, I use appcache to make it work offline - now the problem is handling the initial load before the appcache gets initialized.
In Android I let the device fallback to the local index.html - this could be informative eg. letting the user know that they have to be online to finalize the install.
// On error show default message page...
public void onReceivedError( int errorCode, String description, String failingUrl)
{
super.loadUrl("file:///android_asset/www/index.html");
return;
}
Question: "How do I accomplish the same in IOS?"
Dont have to write the code for me - hints to files and api would be appreciated
You can you the UIWebViewDelegate methods to detect that your remote content loading has failed to load. For example :
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
// here you can either check for the error type or for the url that has failed to load
if([webView.request.url.absoluteString isEqualToString:#"your_remote_url")]
{
NSURL *url = [NSURL urlWithString:#"your_local_url"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[webview loadRequest:request];
}
}
Since you simply want to do some error handling, one solution to this would be to do a SubView, and load it from whatever URL you need it from.
You can find a guide on how to do this, here:
http://docs.phonegap.com/en/3.0.0/guide_platforms_ios_webview.md.html#iOS%20WebViews
Of course, this gets called from the native part :)
Hope that helped!

Unable to display macro enable word file in UIWebview

I'm trying to display MS office files(word,powerpoint,excel) using UIWebview some of the files have macros enable UIWebview is unable to display these files any idea why this happen? is there a way to make UIWebview render these files?.
Note: I do not want the macros to work if i can display the content of the file that will be enough.
I know this is old, but I ran into it today. It looks UIWebView will NOT open macro-enabled Office files directly. For example, the following code fails -
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:filePath]]];
The code above fails ONLY for macro-enabled Office files - it works just fine for .docx, .pptx, .rtf, .pdf, .txt, etc. files. However, if we pull the file into NSData and then provide the mime type explicitly to UIWebView, the file will open. The code below will open these macro-enabled Office files -
// this will open a .pptm file - replace mime type as necessary for other macro-enabled file types
NSData* fileData = [NSData dataWithContentsOfFile:filePath];
[webView loadData:fileData MIMEType:#"application/vnd.ms-powerpoint.presentation.macroEnabled.12" textEncodingName:nil baseURL:nil];
Tested with .pptm, .ppsm, .potm, .docm, .xlsm

Resources