Load a PDF from internet in xcode - xcode

I have a view controller that has a UIWebView object within it.
I would like to load and display a PDF from the internet within the UIWebView.
I have the code working to display the a PDF loaded from the resource bundle, but i am lost on having the PDF loaded from the internet.
I was thinking it would be as simple as changing the fileURLWithPath to point to the location on the internet... although the form displays, it does not show the contents. So I am assuming that I have the wrong URL in the string???
Does anyone have a code snippet to load and display a PDF from the internet within a UIWebView that they would be willing to share???
thanks
tony

I take it you are looking at iOS stuff, not Mac. On iOS, it should really be just as simple as
NSURL* url = [NSURL URLWithString:stringURL];
[aWebView loadRequest:[NSURLRequest requestWithURL:url]];

Use the following for xCode 5.
Interface section - myWebView is the outlet to the UIWebView in my storyboard.
#property (weak, nonatomic) IBOutlet UIWebView *myWebView;
#property NSURL *url;
The rest goes into viewDidLoad section of the ViewController class.
- (void)viewDidLoad
{
[super viewDidLoad];
_url = [[NSURL alloc] init];
_url = [NSURL URLWithString:#"http://deimos3.apple.com/WebObjects/Core.woa/FeedEnclosure/utah.edu.1668842900.01668842919.1681195338/enclosure.pdf"];
[_myWebView loadRequest:[NSURLRequest requestWithURL: _url]];
}
This worked for me but there maybe a more efficient way of doing this.

Here is how i implemented it....
visitNoteViewController *controller = [[visitNoteViewController alloc] initWithNibName:#"visitNoteView" bundle:nil];
NSArray *documentPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
controller.pdfUrl = [NSURL URLWithString:#"http://deimos3.apple.com/WebObjects/Core.woa/FeedEnclosure/utah.edu.1668842900.01668842919.1681195338/enclosure.pdf"];
[self presentModalViewController:controller animated:YES];
[controller release];
got it working.

Related

UIWebView in IOS 8 tables are not showing correctly

we recently upgraded to IOS 8 (and compiled the application).
we saw that tables with text are not showing well anymore (we use UIWebView).
did anyone encounter this? is this a known issue or a bug?
this is the code:
#import "ViewController.h"
#interface ViewController ()
#property (weak, nonatomic) IBOutlet UIWebView *viewWeb;
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
NSString* fullPath = #"/Users/deployment/Downloads/table-2.ppt";
[_viewWeb loadData:[NSData dataWithContentsOfFile:fullPath] MIMEType:#"application/vnd.ms-powerpoint" textEncodingName:nil baseURL:nil];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
thanks!
Are you tried this code...?
UIWebView *webView = [[UIWebView alloc]initWithFrame:CGRectMake(10.0,10.0,300,300)];
[webView setScalesPageToFit:YES];
webView.backgroundColor=[UIColor clearColor];
NSString *ppt = [[NSBundle mainBundle] pathForResource:#"test" ofType:#"ppt"];
NSURL *pptURL = [NSURL fileURLWithPath:ppt];
NSURLRequest *request = [NSURLRequest requestWithURL:pptURL];
[webView loadRequest:request];
[self.view addSubview:webView];
If this code also not worked with your ppt file. Please share your ppt file too. This code working in my case. May be some issue with presentation file.

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.

UIWebView not loading content when it is passed from an UITableView in iOS6, but works perfectly in iOS5

First of all I would like to thank you reading this and helping me out if you know the answer, I am a bit of a newby to iOS and there is an issue that is driving me crazy.
I have an App that works perfectly in iOS5, it has an UITableVIewController and clicking on a row takes you to an UIView with a UIWebView. The idea is that the URL of the webpage to load is passed from the UITableViewController in this way.
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if (([[segue identifier] isEqualToString:#"ViewWeb"])) {
WebViewController *detailViewController = [segue destinationViewController];
detailViewController.title=parameter;
detailViewController.detailItem = parameterURL;
}
}
Then in the WebViewController I use the following code. I declare everything in the .h:
#import <UIKit/UIKit.h>
#interface WebViewController : UIViewController <UIWebViewDelegate>{
IBOutlet UIWebView *webView;
NSString *detailItem;
}
#property(strong,nonatomic) IBOutlet UIWebView *webView;
#property (strong, nonatomic) NSString *detailItem;
#end
And In my .m I use the following code to load the URL:
#interface WebViewController ()
#end
#implementation WebViewController
#synthesize webView;
#synthesize detailItem;
- (void)viewDidLoad
{
[super viewDidLoad];
//Create a URL object.
NSURL *url = [NSURL URLWithString:detailItem];
//URL Requst Object
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
//Load the request in the UIWebView.
[webView loadRequest:requestObj];
}
This is working perfectly fine in the iOS5 simulator, but in iOS6, the UIWebWiew remains blank and doesn't load up. I have read this solution UIWebView not loading URL when URL is passed from UITableView but placing the code in the (void)viewWillAppear:(BOOL)animated does not solve the issue. It works perfectly fine if I hard code the URL, so I am totally lost as to how to solve this.
Thank you very much in advance, I know everyone here is really busy and the fact that great guys take a few minutes to hep me is something amazing.

XCode Prevent UIWebView flashing white screen after Launch Image

I am fairly new to XCode - I'm designing an app with a single view that just displays a UIWebView that loads my jQuery mobile web site.
I have the Default.png and Default#2x.png files for the Launch images in place. When I run the app on my test device or in the emulator, it shows my Launch Image, and then flashes a white screen while the UIWebView loads the first page.
I know that I can change the background color and opacity so it will flash a different color than white, but I would like to prevent the flash altogether. I would like the app to launch, show the Launch image, and not show the UIWebView until the first page has completely loaded. Help?
# Andreas Volz (and anyone else wondering how I solved the problem of the white "flash")
First, add the name-of-loading-image.png and name-of-loading-image#2x.png files to your Xcode project. The regular image should be 320 x 460, and the #2x image should be 640 x 920.
Then, in my ViewController.h file I added these properties:
#property (nonatomic, retain) UIWebView *webView;
#property(nonatomic, strong) UIImageView *loadingImageView;
#end
And then in my ViewController.m file I added this:
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController;
#synthesize webView;
#synthesize loadingImageView;
- (void)viewDidLoad
{
[super viewDidLoad];
//**************** Set website URL for UIWebView
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:#"http://www.example.com"] cachePolicy:NSURLCacheStorageAllowed timeoutInterval:20.0]];
//**************** Add Static loading image to prevent white "flash" ****************/
UIImage *loadingImage = [UIImage imageNamed:#"name-of-loading-image.png"];
loadingImageView = [[UIImageView alloc] initWithImage:loadingImage];
loadingImageView.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:#"name-of-loading-image.png"],
nil];
[self.view addSubview:loadingImageView];
}
- (void)webViewDidFinishLoad:(UIWebView *)webView {
// Remove loading image from view
[loadingImageView removeFromSuperview];
}
To know when your webView has finished loading, you must implement the UIWebViewDelegate protocol.
In your viewController's .h file:
#interface viewController : UIViewController <UIWebViewDelegate>
{
UIWebView *webView;
}
#end
Then in your viewController's .m file, add this method:
- (void)webViewDidFinishLoad:(UIWebView *)webView {
NSLog(#"content loading finished");
// Display your webView
[self.view addSubview:webView];
}
You can use the -viewDidLoad method in your viewController's .m to continue displaying your launch image. You can also begin the loading of content for your webView:
- (void)viewDidLoad {
[super viewDidLoad];
// continue to display launch image
UIImage *launchImage = [[UIImage alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"launchImage.png" ofType:nil ]];
UIImageView *launchImageView = [[[UIImageView alloc] initWithImage:launchImage] autorelease];
[self.view addSubview:launchImageView];
// now setup the base view for the webView controller
UIView *contentView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
contentView.backgroundColor = [UIColor blueColor];
// for rotation
contentView.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
self.view = contentView;
//create a frame to size and place the web view
CGRect webFrame = [[UIScreen mainScreen] applicationFrame];
UIWebView *aWebView = [[UIWebView alloc] initWithFrame:webFrame];
self.webView = aWebView;
//aWebView.scalesPageToFit = YES;
aWebView.autoresizesSubviews = YES;
aWebView.autoresizingMask=(UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth);
//set the web view delegates for the web view to be itself
[aWebView setDelegate:self];
//determine the path the to the index.html file in the Resources directory
NSURL *url = [[NSBundle mainBundle] URLForResource:#"index" withExtension:#"html"];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[aWebView loadRequest:requestObj];
}

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)
}

Resources