Cannot open PDF file using UIWebView in iOS8beta5 - xcode

I have working project in which i display pdf file in UIWebView
While testing my application in iOS8beta5 with XCode5, it did not work
In log it display failed to find PDF header : '%PDF' not found
I create sample application with below code which also have same problem
CODE :
self.myWebView.delegate = self;
NSString *path = [[NSBundle mainBundle] pathForResource:#"201" ofType:#"pdf"];
NSURL *targetURL = [NSURL fileURLWithPath:path];
NSURLRequest *request = [NSURLRequest requestWithURL:targetURL];
[self.myWebView loadRequest:request];
I read release note, Apple wrote that
Quicklook framework may did not display PDF file in some application
but they did not mention any thing about UIWebView,
Reference
NOTE :
This problem was not came in iOS8beta4.
Same problem is also occur in XCode6 + iOS8beta5.
I also check with different PDF files but have same problem.
QUESTION :
1] Is there any one had done fix for the above problem ?
If yes, please provide me some sample code.

I found a workaround for viewing the PDF in WebView
// Create pdf path & url
NSString *path = [[NSBundle mainBundle] pathForResource:#"201" ofType:#"pdf"];
NSURL *targetURL = [NSURL fileURLWithPath:path];
// Load pdf in WebView
NSData *pdfData = [[NSData alloc] initWithContentsOfURL:targetURL];
[self.webView loadData:pdfData MIMEType:#"application/pdf" textEncodingName:#"utf-8" baseURL:nil];

Related

Hide resources in iOS framework

I am developing Universal iOS framework using this guide.
How to hide images inside framework from other developers?
Also, I am not sure, do we need Info.plist file inside framework?
I solve my problem by encrypt the resources. I am not sure, that is the best way to resolve, but it works in my case.
Here is a simple OS X app that encrypt files before adding it to the framework.
Code for decrypt:
NSError* error;
NSData* encryptedData = [NSData dataWithContentsOfFile:filePath];
NSData* decryptedData = [RNDecryptor decryptData:encryptedData
withPassword:#"SAMPLE-KEY"
error:&error];
UIImage* image = [UIImage imageWithData:decryptedData];
`
Code for encrypt:
NSData* data = [NSData dataWithContentsOfFile:filePath];
NSError* error;
NSData* encryptedData = [RNEncryptor encryptData:data
withSettings:kRNCryptorAES256Settings
password:#"SAMPLE-KEY"
error:&error];
[encryptedData writeToFile:encryptedFilePath atomically:YES];

DiskImageCache: Could not resolve the absolute path of the old directory. failed to find PDF header: `%PDF' not found

In one of my views, I simply want to show a pdf file in a UIWebView.
My code of the header file:
#import <UIKit/UIKit.h>
#interface FCOMViewController : UIViewController
{IBOutlet UIWebView *fcomWebView;}
//flag to denote if this is first time the app is run
#property (nonatomic) BOOL firstRun;
#end
And here the method in the implementation file:
- (void)viewDidLoad {
//Show the procedures PDF
NSString *path = [[NSBundle mainBundle] pathForResource:#"b777normalprocedures" ofType:#"pdf"];
NSURL *url = [NSURL fileURLWithPath:path];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[fcomWebView loadRequest:request];
[fcomWebView setScalesPageToFit:YES];
The app starts fine, does not crash or anything. The referencing outlets are set, but it doesn't show the PDF. Console says:
DiskImageCache: Could not resolve the absolute path of the old
directory. failed to find PDF header: `%PDF' not found.
The pdf is in the project directory, I have double checked that it is written correctly.
What can I do here? Any help would be appreciated.
Try this if you are targeting iOS 8.0:
NSString *path = [[NSBundle mainBundle] pathForResource:#"b777normalprocedures" ofType:#"pdf"];
NSURL *targetURL = [NSURL fileURLWithPath:path];
NSData *pdfData = [[NSData alloc] initWithContentsOfURL:targetURL];
[_webView loadData:pdfData MIMEType:#"application/pdf" textEncodingName:#"utf-8" baseURL:nil];
For me the problem was with downloaded pdf files which had extensions that I presume was otherwise recognised by UIWebView (aspx,php etc.). When I renamed the downloaded pdf files so that they had a ".pdf" extension the error did not occur.

UIImage imageWithContentsOfFile: not working on iOS8

When I test my app on iOS8 beta3 and beta5, I found an bug that about [UIImage imageWithContentsOfFile:].
When image resources are stored in sub-bundle of a main bundle, it will return nil if we initialize UIImage via the following method:
NSString *testBundlePath = [[NSBundle mainBundle] pathForResource:#"test" ofType:#"bundle”];
NSBundle *bundle = [NSBundle bundleWithPath:testBundlePath];
NSString *path = [bundle pathForResource:#"play.png" ofType:nil];
UIImage *image = [UIImage imageWithContentsOfFile:path]; //iOS8 image = nil, iOS7 image != nil
But when the image resources are stored in main bundle, UIImage Initialization can be succeed through the following method:
NSString *path = [[NSBundle mainBundle] pathForResource:#"play.png" ofType:nil];
UIImage *image = [UIImage imageWithContentsOfFile:path]; //iOS8 and iOS7 image != nil
We have found this problem under iOS8 beta3 and it still exists under iOS8 beta 5.
The Demo app directory structure was below:
XXX.app
|----test~ipad.bundle
|----play.png
|----test~iphone.bundle
|----play.png
|----play.png
|----play~iphone.png
|----play~ipad.png
......
Does anyone have the same problem? I think this is a bug on iOS8, Therefore, many apps have used bundle to organize resources, such as images. But now, this bug will make thousands of apps become ugly and hard to use. We really hope that Apple can fix this bug in the next version, otherwise, all of my app will have to developing an new version for this bug!
It's not actually broken, you're just sending it the wrong path.
I'm guessing that you are using the simulator, and have saved a file somewhere in your app and then made note of the full path to that file. However, when you rebuild your app the UUID for the installed app changes, and your saved path is no longer valid (even though the files are still there). You can see your current active path by checking [[NSFileManager defaultManager] currentDirectoryPath] Note that the value changes each time you rebuild and re-deploy your app.
To get to your actual file, you'll need to do something like this:
NSString *image = #"myImage.png";
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *cache = [paths objectAtIndex:0];
NSString *fullPath = [NSString stringWithFormat:#"%#/%#", cache, image];
UIImage *image = [UIImage imageWithContentsOfFile:fullPath];
pathForResource:ofType: only looks in the Resources folder (and localised folders). If you are placing files into dedicated folders in the bundle then you need to use pathForResource:ofType:inDirectory: and supply the directory name.
Apple has fixed this bug on iOS8.1,Cheers
just use
NSString *fullPath = [bundle pathForResource:name ofType:#"png"];
NSData *data = [NSData dataWithContentsOfFile:fullPath];
UIImage *image = [UIImage imageWithData:data];

How do I load html into a webview?

I have just re-designed my iPhone app using Storyboard... questin is: how do I address the the UIWebView so I can load the html? (I am a newbie when it comes to Obj-C, having done my previous app in MonoTouch, so I'm kinda lost). :D
- (void)viewDidLoad {
NSString *urlAddress = #”http://www.stackoverflow.com”;
NSURL *url = [NSURL URLWithString:urlAddress];
NSURLRequest *req = [NSURLRequest requestWithURL:url];
[webView loadRequest:requestObj];
}
Apple Developer docs are really good resource. The usage of UIWebView is outlined here enter link description here
To load a network resource simply pass a NSURLRequest object to loadRequest.
[self.myWebView loadRequest:[NSURLRequest
requestWithURL:[NSURL URLWithString:#"http://www.apple.com/"]]];
You can also do this in viewDidLoad (a UIWebView is a subclass of UIView and inherits all its methods). For example, loading a local pdf resource.
- (void)viewDidLoad {
[super viewDidLoad];
NSString *thePath = [[NSBundle mainBundle] pathForResource:#"iPhone_User_Guide" ofType:#"pdf"];
if (thePath) {
NSData *pdfData = [NSData dataWithContentsOfFile:thePath];
[(UIWebView *)self.view loadData:pdfData MIMEType:#"application/pdf"
textEncodingName:#"utf-8" baseURL:nil];
}
}
To setup your UIWebView with a link, just use the following code.
var URLPath = "Insert email address here"
Then use...
loadAddressURL()
and finally...
func loadAddressURL(){
let requestURL = NSURL(string:URLPath)
let request = NSURLRequest(URL: requestURL!)
Webview.loadRequest(request)
}

loadHTMLString baseURL:nil not working in iOS5

I got a problem with UIWebView. I want to render my own html code in UIWebView.
if I use the below code, it's working fine.
NSBundle *thisBundle = [NSBundle mainBundle];
NSString *path = [thisBundle pathForResource:#"foo" ofType:#"html"];
NSURL *baseURL = [NSURL fileURLWithPath:path];
[self.webView loadHTMLString:#"foo.html" baseURL:baseURL];
I want to render the html with below code. But it's not working. foo.html contains exactly the same content with the NSString* one. Any hints? Thanks in advance. =)
NSString* one = #"<html><head><title></title></head><body><img src=\"image.jpg\"/></body></html>";
[self.webView loadHTMLString:one baseURL:nil] ;
I use this code that works perfectly :
NSString* htmlContent = #"<html><head><title></title></head><body><img src='image.jpg' /></body></html>";
[self.webView loadHTMLString:htmlContent baseURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]]];
Bu sure that image.jpg is included into your project.
create JS file demo.js and add code
var hello = function(str){
      return str;
};
add UIWebView => To embed JavaScript in a dummy html file and load that into the UIWebView
[self.webView loadHTMLString:#"<script src=\"demo.js\"></script>"
              baseURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] resourcePath]]];
Here is the code to make JS call
NString *str=#"hi JavaScript";
NSString *function = [[NSString alloc] initWithFormat: #"hello(%#)", str];
NSString *result = [webView stringByEvaluatingJavaScriptFromString:function];
Now, there is one important safety tip to make this actually work: The UIWebView must actually be loaded a view. It doesn’t have to be visible, but in order for the WebKit engine to execute the JS, it must be on a view.

Resources