Show multiple PDF with one UIwebView - iphone

I hope someone can help me.
In one view of the app I've got a UIWebView where I'm showing the PDF file. I need to show 3 different PDF (a.pdf, b.pdf, c.pdf) in the same UIWebView. I know how to show one.
CGRect frame2 = CGRectMake(20, 25, 280, 350);
testWebView = [[UIWebView alloc] initWithFrame:frame2];
NSString *path = [[NSBundle mainBundle] pathForResource:#"a" ofType:#"doc"];
NSURL *url = [NSURL fileURLWithPath:path];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[apneaWebView loadRequest:request];
[apneaWebView setScalesPageToFit:YES];
[self.view addSubview:testWebView];
But if are 3 different PDf what to do.
Thanks for any help.

Look at that: Can we display many pdf in a UIWebview?
You can only display a pdf in a UIWebView. You can try to create 3 different UIWebViews for every PDF or try to merge them.

Related

Adding youtube video to my application

This is how i added a youtube video to my project; I have added these to the viewDidLoad Function
UIWebView *web = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 480, 300)];
NSString *html = #"<html><head> .........."; // its too long so i cut it short
[webView loadHTMLString:html baseURL:[NSURL URLWithString:#"http://www.youtube.com/embed/vLBKOcUbHR0"]];
[self.view addSubview:web];
The view what i see is;
And what i want is some thing like this
If you look closer, you will see a ToolBar and a DOne button, and also a UIActivityIndicator in the second image, and none of these are displaying in mine. How can i solve this ?
That's not really how you use baseURL. The Base URL is what relative links will be relative to if there are links in the HTML - mapping it to youtube probably won't help at all.
Instead, just copy the embed code from youtube, which looks like this:
<iframe width="420" height="315" src="http://www.youtube.com/embed/2WNrx2jq184" frameborder="0" allowfullscreen></iframe>
Copy the URL from the iframe and open it directly in your web view, like this:
NSURL *URL = [NSURL URLWithString:#"http://www.youtube.com/embed/2WNrx2jq184"];
NSURLRequest *request = [NSURLRequest requestWithURL:URL];
[webView loadRequest:request];
That should work.

Creating a WebView programmatically and displaying NSStrings inside the view

I have created a WebView programmatically. This works perfect. The code is below.
What I need to try and do now is inject NSStrings into it. I have an array of 30 strings. 15 Headings and 15 body-texts.
Is it possible to get these displayed inside the WebView? I'm guessing I need to change them into a HTML, i may be reformat them all into 1 long NSString with HTML-tags and linebreaks and newling-tags maybe?
Anybody able to help me with some pointers/code snippets to get me on the right track and moving the the correct direction please?
- (void) initUIWebView
{
aWebView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 290)];//init and
create the UIWebView
aWebView.autoresizesSubviews = YES;
aWebView.autoresizingMask=(UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth);
[aWebView setDelegate:self];
NSString *urlAddress = #"http://www.google.com"; // test view is working with url to webpage
NSURL *url = [NSURL URLWithString:urlAddress];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[aWebView loadRequest:requestObj];
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 300)];
[[self view] addSubview:aWebView];
}
Thanks
-Code
Instead of using the loadRequest method you will have to use the loadHTMLString method of UIWebView
The following code might help you in displaying the NSString in UIWebView
NSString *html = #"<html><head></head><body>The Meaning of Life<p>...really is <b>42</b>!</p></body></html>";
[webView loadHTMLString:html baseURL:nil];
Hope this will resolve your issue...

Why does my UIWebView ignore my clicks?

I have a simple iPhone app which will display html content I have placed in the Documents directory, but once it is displayed, the links do not work.
The following code is called from the init method of my app delegate.
Can anyone suggest what I have missed please?
-(void) loadWebView:(NSString*) appDirectory {
CGRect rect = CGRectMake(0, 0, 320, 480);
webView = [[UIWebView alloc] initWithFrame:rect];//init and create the UIWebView
[webView setBounds:rect];
// NSString* webViewFile = [appDirectory stringByAppendingString:#"index.html"];
// NSString* protocol=#"file://";
// NSString* fullUrl=[protocol stringByAppendingString:webViewFile];
fullUrl=#"http://www.google.com";
NSLog(#"Attempting to open url (unencoded) %#", fullUrl);
fullUrl = [fullUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSLog(#"Attempting to open url (encoded) %#", fullUrl);
//Create a URL object.
NSURL *url = [NSURL URLWithString:fullUrl];
//URL Requst Object
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
webView.delegate=self;
[webView loadRequest:requestObj];
window = [[UIWindow alloc] init];
[window addSubview:webView];
[window makeKeyAndVisible];
window.hidden=NO;
}
I don't think you should create your own UIWindow object. This object is already in your xib and it is probably "madeKeyAndVisible" already in the applicationDidFinishLoading method.
Try to remove all lines relating to window, and add the subview to self.view, like
[self.view addSubview:webView];
The answer turns out to be that there are two clipping functions: one for display and one for interaction.
I had set my bounds to the screen size, but not my frame.
Obvious? Not really.

Displaying ppt, doc, and xls in UIWebView doesn't work but pdf does

It looks like a few people on stackoverflow get this to work but their code isn't posted. I'm using
[web loadData:data MIMEType:MIMEType textEncodingName:#"UTF-8" baseURL:nil];
where MIMEType is:
#"application/vnd.ms-powerpoint"
#"application/vnd.ms-word"
#"application/vnd.ms-excel"
(BTW, I've seen DOC files use mimetype #"application/msword" but the "vnd" version seems more appropriate. I tried both just in case.)
I verified that my 'data' is correct. PDF and TXT files work. When the UIWebView displays PPT, DOC, or XLS files, it's blank. I put NSLOG statements in my UIWebViewDelegate calls.
shouldStartLoadWithRequest:<NSMutableURLRequest about:blank> navType:5
webViewDidStartLoad:
didFailLoadWithError:Error Domain=NSURLErrorDomain Code=100 UserInfo=0x122503a0 "Operation could not be completed. (NSURLErrorDomain error 100.)"
didFailLoadWithError:Error Domain=WebKitErrorDomain Code=102 UserInfo=0x12253840 "Frame load interrupted"
so obviously the load is failing, but why? If I change my mimetype to #"text/plain" for a PPT file, the UIWebView loads fine and displays unprintable characters, as expected. That's telling me the 'data' passed to loadData: is ok.
Meaning my mimetypes are bad?
And just to make sure my PPT, DOC, and XLS files are indeed ok to display, I created a simple html file with anchor tags to the files. When the html file is displayed in Safari on the iPhone, clicking on the files displays correctly in Safari.
I tried to research the error code displayed in didFailLoadWithError (100) but all the documented error codes are negative and greater than 1000 (as seen in NSURLError.h).
-(void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error { NSLog(#"didFailLoadWithError:%#", error); }
Have you tried using the following documented method?:
-(void)loadDocument:(NSString*)documentName inView:(UIWebView*)webView
{
NSString *path = [[NSBundle mainBundle] pathForResource:documentName ofType:nil];
NSURL *url = [NSURL fileURLWithPath:path];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[webView loadRequest:request];
}
// Calling -loadDocument:inView:
[self loadDocument:#"mydocument.rtfd.zip" inView:self.myWebview];
It works for these in iPhone OS 2.2.1:
Excel (.xls)
Keynote (.key.zip)
Numbers (.numbers.zip)
Pages (.pages.zip)
PDF (.pdf)
Powerpoint (.ppt)
Word (.doc)
iPhone OS 3.0 supports these additional document types:
Rich Text Format (.rtf)
Rich Text Format Directory (.rtfd.zip)
Keynote '09 (.key)
Numbers '09 (.numbers)
Pages '09 (.pages)
The only way i found to read an Office object (tested with .doc or .xls) is to save the NSData object in a temp file, then read it.
-(void)openFileUsingExtension:(NSString*)extension {
NSString *path = [NSString stringWithFormat:#"%#temp.%#",NSTemporaryDirectory(),extension];
NSLog(#"%#",path);
if ([objectFromNSData writeToFile:path atomically:YES]) {
NSLog(#"written");
NSURL *url = [NSURL fileURLWithPath:path];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[self.webview loadRequest:request];
self.webview.scalesPageToFit = YES;
self.webview.delegate = self;
[self.view addSubview:self.webview];
}
}
then you can remove the file inside the UIWebViewDelegate method:
- (void)webViewDidFinishLoad:(UIWebView *)webView{
NSString *path = [NSString stringWithFormat:#"%#temp.%#",NSTemporaryDirectory(),extension];
[[NSFileManager defaultManager] removeItemAtPath:path error:nil];
}
UIWebView *webView = [[UIWebView alloc]initWithFrame:CGRectMake(0.0, 0.0, 1000, 760)];
[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];
[webView release];
Did you try this on the device or in the simulator only?
I find that I cannot get UIWebView to display .doc and .pages documents in the simulator (but can display .txt and .pdf ones), but both file types load and display just fine on both the iPad and iPhone devices.
Bug in simulator?
Gregor,
Sweden
After searching thru, the solution I found was because when we import xls into Xcode by drag and drop the xls file into Project->Supporting Files, we need to specific the 'Add to targets:' , we need to tick the project to add

iPhone SDK: Preferences

I wanted an entire page of text(multiple lines) in my settings similar to the one on iphone:
settings->general settings->About->Legal
I have not been able to do so. I tried entering title in PSGroupSpecifier, it gives me multiple lines but with a gray background. i wanted a white background.
Any help would be highly appreciated.
Thanks in advance
Use a UIWebView and store your page as a HTML file in your resources.
// Create the UIWebView
UIWebView *webView = [[[UIWebView alloc] initWithFrame:self.view.frame] autorelease];
// Add the UIWebView to our view
[self.view addSubview:webView];
// Load the HTML document from resources
NSString *pagePath = [[NSBundle mainBundle] pathForResource:#"legal" ofType:#"html"];
NSURL *pageURL = [NSURL fileURLWithPath:pagePath];
NSURLRequest *pageReq = [NSURLRequest requestWithURL:pageURL];
[webView loadRequest:pageReq];