im making an application where i have to load my database from html file which is in my resource folder. i have a table view and when i click on a certain cell it will open html file based on that click. can any one help me how to do this. thanks
// create a web view in "cellForRowAtIndexPath"
UIWebView *wv = [[UIWebView alloc] initWithFrame:CGRectMake(0,0,320,480)];
[wv loadHTMLString: << your html >> baseURL: nil];
wv.delegate = self;
// implement the webView delegate
- (void) webViewDidFinishLoad:(UIWebView *)webView {
[self.view addSubView:webView]; // or push onto the naviation controller
}
I think when you 'didSelectCellAtIndex' in the table view will open a new view controller with the webview. And you have also mapping of each cell with the corresponding html file name. Then in the viewDidLoad method of your next VC, you can use this code, while webView is the outlet pointing to your UIWebView. And htmlFileName is the html file's name.
[webView loadRequest:[NSURLRequest requestWithURL:
[NSURL fileURLWithPath:[[NSBundle mainBundle]
pathForResource:htmlFileName
ofType:#"html"]
isDirectory:NO] ] ];
Related
My app used to use a single UIWebView from the storyboard to display a users online data.
Now I have added the ability to have multiple accounts stored in the app, and so I would like to be able to have a separate UIWebView for each account, and show the appropriate one.
My app is a tabbed application, with one tab holding the account details in a table view, and the other with the UIWebView. This is what I have tried so far to have multiple web views.
In the table view controller, my didSelectRowAtIndexPath looks like this:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//Generate the url (please note I've taken this code out)
...
//Get the view controller that will hold the web view
NSArray *viewControllers = self.tabBarController.viewControllers;
ViewerFirstViewController *viewerViewController = [viewControllers objectAtIndex:0];
//Look for the web view in an array of web views
[viewerViewController getWebView:indexPath.row];
[viewerViewController setViewerUrl:viewerURL];
[tableView deselectRowAtIndexPath:indexPath animated:YES];
//Move to the tab that will hold the web view (bonus question - how can I animate this move?
[self.tabBarController setSelectedIndex:0];
}
Now, for the view controller that will hold the web view
-(void)getWebView:(int)index
{
if ([webViews objectAtIndex:index] == NULL) {
[self createWebView:index];
[self setDidExist:false];
[self setWebView:[webViews objectAtIndex:index]];
} else {
[self setDidExist:true];
[self setWebView:[webViews objectAtIndex:index]];
}
}
-(void)createWebView(int)index
{
float width = self.view.bounds.size.width;
float height = self.view.bounds.size.height;
UIWebView *wv = [[UIWebView alloc] initWithFrame:CGRectMake(0, 20, width, height)];
wv.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
wv.scalesPageToFit = YES;
wv.delegate = self;
[self.view addSubview:wv];
[webViews insertObject:wv atIndex:index];
}
-(void)viewWillAppear:(BOOL)animated
{
[super viewDidLoad];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:viewerUrl];
if (didExist) {
[webView loadRequest:requestObj];
[webView setHidden:false];
} else {
[webView loadRequest:requestObj];
}
}
It appears that the web view is being created as I am presented with a white screen that I can scroll in, but no page is being loaded. I'm assuming this won't work to hide the web views not currently in use either.
The web view should only load the page if it didn't exist previously. If it did already exist then it should present the loaded web view that is stored in the array.
Am I loading the URL at the wrong time or something? I'm unsure as to why I get a web view created but it's not filled with anything..
What seems to be happening is the web view isn't being added to the array when I create it - the array is empty and so webView isn't being set and so the url is never loaded into it However the code I use to add it to the array seems ok to me..
[webViews insertObject:wv atIndex:index];
I changed it from storing them in an NSMutableArray to an NSMutable Dictionary. This solved my problems.
I'm trying create a subclass of UIActivity to add a custom button to a UIActivityViewController. I want this custom button to be able to open a link without leaving the application. I've found numerous solutions that allow for opening things in safari, but I can't figure out if it is possible to just open a UIWebview inside my app (Modal view perhaps?).
I've tried creating a `UIWebview in the delegate method to handle the click, but since this isn't a viewcontroller I can't add it to the view hierarchy.
- (void)prepareWithActivityItems:(NSArray *)activityItems{
UIWebView *webView=[[UIWebView alloc] initWithFrame:CGRectMake(0,0,320,480)];
NSURLRequest *urlRequest;
NSURL *urlforWebView;
urlforWebView=[NSURL URLWithString:#"http://www.google.com"];
urlRequest=[NSURLRequest requestWithURL:urlforWebView];
[webView loadRequest:urlRequest];
}
// example:Need to add your webview to your mainview first
UIWebView *webView = [[UIWebView alloc] init];
[webView setFrame:CGRectMake(0, 0, 320, 460)];
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:#"http://google.com"]]];
[[self view] addSubview:webView];
I believe this project implements just what you would like to do but it opens in safari: https://github.com/davbeck/TUSafariActivity Good for starting point.
The activity itself cannot open the view as it is not connected to the controller view hierarchy. You need some way to tell the host controller that the user has selected your activity. The easiest way to do this is through notifications:
The view controller registers for notifications with a given identifier.
The activity posts this notification if performed.
The notification is processed by the activity in the handler method and it opens the web view.
Don't forget to unregister the view controller from the notification center if the controller is being removed from the navigation stack.
There are lots of examples for using notifications, like this Send and receive messages through NSNotificationCenter in Objective-C?
Try this
UIPlainViewController here is just a custom UIViewController with a WebView added to it. Implement (UIViewController *)activityViewController in your derived UIActivity class as such:
(UIViewController *)activityViewController {
NSString *page = #"some url";
NSURL *url = [[NSURL alloc] initWithString:page];
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url];
[((UIPlainViewController*)self.plainViewController).webView loadRequest:request];
return self.plainViewController;
}
My webpage will not load it shows nothing I have 3 viewcontrollers in firstviewcontroller you click the button which loads the next page thirdviewcontroller up to this point all is good but the webpage doesnt load
code from firstviewcontroller
-(IBAction)moveToViewSiteView:(id)sender{
self.third = [[[SecondViewController alloc]initWithNibName:#"ThirdViewController" bundle:nil]autorelease];
[self.navigationController pushViewController:self.third animated:YES];
}
code in thirdviewcontroller
- (void)viewDidLoad {
NSString *urlAddress=#"http://www.apple.com/";
NSURL *url=[NSURL URLWithString:urlAddress];
NSURLRequest *requestObj= [NSURLRequest requestWithURL:url];
[self.cscsPage loadRequest:requestObj];
[super viewDidLoad];
}
the uiwebview i have called webview in the document window in ib if i drag from files owner to the webview then when i run it the app crashes.
hope all is clear
thanks
Move [super viewDidLoad]; to the top of viewDidLoad method. You should invoke the super class implementation of it first.
From my Objective C iPhone app, I want a user to click on a "Register" for account button and then open up a registration page on my website. What is the code on the iPhone to open up a website in response to a user action?
Create a UIButton and assign the action to a method like this:
- (IBAction)register:(id)sender {
//you'll want to subclass UIViewController
SomeViewController *webViewController = [[SomeViewController alloc]init];
[self presentModalViewController:webViewController animated:YES];
[webViewController release];
}
Your SomeViewController will have a webview that load's your register page. Then in your "SomeViewController"'s viewDidLoad, do something like this:
- (void)viewDidLoad
{
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:#"http://mysite.com/register"]];
//this is a webview that you either created in -loadView or IB
[self.webView loadRequest:request];
}
I got a UITabBarController and one of the bar items is a Navigation Controller with some buttons on it.
One of the buttons opens up a urlRequest and load it in a UIWebView.
NSURL * url = [NSURL URLWithString:myUrl];
NSURLRequest * urlRequest = [NSURLRequest requestWithURL:url];
UIWebView * web = [tView wView];
[web setScalesPageToFit:YES];
[web loadHTMLString:#"Loading" baseURL:nil];
[web loadRequest:urlRequest];
[self.navigationController pushViewController:tView animated:YES];
For some reason when i click the button for the first time nothing happens.
I used the UIWebViewDelegate protocol to debug it like so:
- (void)webViewDidStartLoad:(UIWebView *)webView
{
NSLog(#"webViewDidStartLoad");
}
When i click the button nothing happens, and i don't see the NSLog message.
When i hit back and click the button again, i see the debug and everything works just find.
Any idea what causing this ?
P.S if i put the :
[self.navigationController pushViewController:tView animated:YES];
in the webViewDidStarLoad method the application just hang, since it's not loading it on the first click.
You need to make sure that tView's view has been loaded. When a viewController is instantiated, its view (and all of it's IBOutlets) are all nil, and remain that way until the view is loaded.
You have two options: move your load stuff into tView's -viewDidLoad method, or force the view to load before calling [tView wView] by, for example, calling [tView view] (this will force the XIB file to be loaded, and all outlets to be connected.