Creating a WebView programmatically and displaying NSStrings inside the view - iphone

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...

Related

Show multiple PDF with one UIwebView

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.

UIWebView not load

I'm trying to load a UIWebView, but I can't understand why doesn't work, I'm doing this:
Loading *load = [[Loading alloc] init];
[load searchName];
then Loading.h
#interface Loading : NSObject <UIWebViewDelegate>
{
UIWebView *myWebView;
}
- (void) searchName;
Loading.m
- (void) searchName{
myWebView = [[UIWebView alloc] init];
myWebView.delegate = self;
NSString *urlAddress = #"www.google.com";
//Create a URL object.
NSURL *url = [NSURL URLWithString:urlAddress];
//URL Requst Object
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
//Load the request in the UIWebView.
[myWebView loadRequest:requestObj];
}
- (void)webViewDidFinishLoad:(UIWebView *)thisWebView
{
NSString *yourHTMLSourceCodeString = [myWebView stringByEvaluatingJavaScriptFromString:#"document.body.innerHTML"];
NSLog(#"%#",yourHTMLSourceCodeString);
}
why never call webViewDidFinishLoad method?
The reason your webview is not displayed is you have taken NSObject class and NSObject classes dont have an XIB and the way you have done is just calling your -(void)searchName method and is not adding it to self.view.
Did you created this webview with IB? Did you connected the delegate to the webview?
Try to catch errors with webView:didFailLoadWithError: (post them if there are errors)
Try to set the delegate with code [myWebView setDelegate:self]; or self.myWebView.delegate = self;
Check output of delegate if nothing happend:
self.myWebView.delegate = self;
NSLog(#"%#",self);
Yes, take note into what Nathan said. I am just expanding.
If you are using IB, connect the outlet. Then use [myWebView setDelegate:self];
Be sure to use http://. Http on it's own won't do anything.
Can you get other URL's to load?
Finally, I have an open source basic web browser for Cocoa that you can take a look at.
Basic Web Browser

html load coming from NSString

html load coming from NSString.
I have the following problem: I get html code and want to get a webView just not how to do and searched and have not found examples if anyone would be so kind to tell me how you do or would appreciate any example of.
UIWebView* newWebView = [[UIWebView alloc] initWithFrame:CGRectMake(x,y,width,height)];
[newWebView loadHTMLString:htmlString baseURL:[NSURL URLWithString:urlString]];
[self.view addSubview:newWebView];
[newWebView release];

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.

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];