How to I show website on my tab in application?
And create refresh button
Assuming you want to add a UIWebView to a UIViewController. Put this in your viewDidLoad method:
UIWebView *wv = [[UIWebView alloc] initWithFrame:self.view.frame];
NSURLRequest *req = [[NSURLRequest alloc] initWithURL:[NSURL urlWithString:#"www.google.com"]];
[wv loadRequest:req];
[self.view addSubview:wv];
Regarding the refresh button: Make sure 'wv' is defined in your .h file and use this method:
- (IBAction)refresh
{
[wv reload];
}
Related
I have an application which is bascially a TabBar where the tabs are UINavigationControllers which move back and forth between different ViewControllers containing UIWebViews. My problem is that although I can click on the buttons for the UIWebViews, I can't scroll the content when it is larger than the screen.
Am creating the TabController
tabBarController = [[MainTabBarController alloc] init];
[tabBarController setDelegate:self];
When switching between tabs, I have this code:
WebViewController *wvc = [[WebViewController alloc] initWithNibName:#"WebController" bundle:nil];
UIWebView *wv = [[UIWebView alloc] init];
[wv setUserInteractionEnabled:YES];
[wvc setTitle:[nc title]];
[wv setDelegate:self];
[wv loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:requestURL]]];
[wvc setWebpage:wv];
[wvc setView:wv];
[nc pushViewController:wvc animated:NO];
NSLog(#"NC.viewcontroller count: %d", [nc.viewControllers count]);
[nc setLastURL:requestURL];
[wvc startLoaderIndicator];
[wv release];
[wvc release];
requestOk = YES;
And when a link within one of the UIWebViews is clicked, I have this:
ItemNavigationController *nc = (ItemNavigationController *)[tabBarController selectedViewController];
WebViewController *wvc = [[WebViewController alloc] initWithNibName:#"WebController" bundle:nil];
UIWebView *wv = [[UIWebView alloc] initWithFrame:CGRectMake(0,0,320,480)];
//wvc.view = wvc.sView;
[wvc setTitle:[nc title]];
[wv setDelegate:self];
[wv loadRequest:request];
[wvc setWebpage:wv];
[wvc setView:wv];
[nc pushViewController:wvc animated:NO];
NSLog(#"NC.viewcontroller count: %d", [nc.viewControllers count]);
[nc setLastURL:request.URL.absoluteString];
[wvc startLoaderIndicator];
[wv release];
[wvc release];
requestOk = YES;
Everything works perfectly, except that the WebViews don't scroll. It looks something like this:
First thing I don't like in this code block...
WebViewController *wvc = [[WebViewController alloc] initWithNibName:#"WebController" bundle:nil];
UIWebView *wv = [[UIWebView alloc] initWithFrame:CGRectMake(0,0,320,480)];
wvc.view = wvc.sView;
Instead create and add the subView to the view in the viewDidLoad block of the view controller. Furthermore, avail yourself of IB and add the WebView via the XIB and simply connect the IBOutlet and delegate.
If you insist upon doing it this way don't overwrite the view,
[wvc.view addSubview:wv];
I wrote this in a simple WebViewController:
- (void)viewDidLoad
{
[super viewDidLoad];
self.view.autoresizingMask = UIViewAutoresizingFlexibleHeight;
UIWebView *webView = [[UIWebView alloc] initWithFrame:self.view.bounds];
webView.autoresizingMask = UIViewAutoresizingFlexibleHeight;
[self.view addSubview:webView];
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:#"http://news.google.com"]]];
}
and called it from a AppDelegate like this:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
self.window.backgroundColor = [UIColor whiteColor];
CPWebController *webController = [[CPWebController alloc] init];
CPNavController *navController = [[CPNavController alloc] initWithRootViewController:webController];
[self.window addSubview:navController.view];
[self.window makeKeyAndVisible];
return YES;
}
It sizes correctly, has a navbar, and scrolls correctly. Please forgive the lack of releases for code brevity.
From what I can see you're setting it with a fixed height here:
UIWebView *wv = [[UIWebView alloc] initWithFrame:CGRectMake(0,0,320,**480**)];
So the content size is only going to be 480 so there's nowhere to scroll.
Try something like (0,0,320,1000); to test if that is the issue
If so set the height to be the cotent size so it's dynamic
Create WebViewController class and inside .m in viewDidLoad add:
- (void)viewDidLoad
{
[super viewDidLoad];
UIWebView *wv = [[UIWebView alloc] initWithFrame:CGRectMake(0,0,320,480)];
[wv setDelegate:self];
NSURL*url = [NSURL URLWithString:#"http://www.link.com/twitter_iPhone.php"];
[wv loadRequest:[NSURLRequest requestWithURL:url]];
[self.view addSubview:wv];
}
Actually i have one table view ,when Select rowAtIndexPath then it load my webView and play my video, how to return from UIWebView to my tableView ?
-(void)playButtonPressed1:(UIButton*)sender
{
Video *currentVideo= [[xmlParservideo videoNames] objectAtIndex:btn1.tag];
NSLog(#"content============== %#", [currentVideo content2]);
CGRect webFrame = CGRectMake(0.0, 0.0, 320.0, 460.0);
UIWebView *webView = [[UIWebView alloc] initWithFrame:webFrame];
[webView setBackgroundColor:[UIColor greenColor]];
NSString *urlString=currentVideo.content2;
NSURL *url = [NSURL URLWithString:urlString];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[webView loadRequest:requestObj];
[[self view] addSubview:webView];
// [self.navigationController popViewControllerAnimated:YES];
NSLog(#"jointed2 jjjjjjjjj %d",btn1.tag);
Place the Uiwebview in another view controller and push to it when a row is selected and when the video complete pop back to the tableview controller.
On didSelectRowAtIndexPath
videoViewController *newView = [videoViewController alloc]initWithNibName:#"videoViewController" bundle:nil];
[self.navigationController pushViewController:newView animated:YES];
and in your init method of the view controller create the webView programatically
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
self.title = #"Video Player";
UIWebView *videoWebView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 290)];
[videoWebView setDelegate:self];
NSString *urlAddress = #"http://www.google.com";
NSURL *url = [NSURL URLWithString:urlAddress];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[videoWebView loadRequest:requestObj];
[[self view] addSubview:videoWebView];
}
return self;
}
Ok, Now just pop your view after completing your video
[self.navigationController popViewControllerAnimated: NO];
A workaround that may work is using this: [webView setHidden:YES];
This hides the webView from the view, so use that line of code whenever you want the webview to disappear. When you want it to return to the view, use this: [webView setHidden:NO];
i have a view controller and in view did load method i have this code
- (void)viewDidLoad
{
self.navigationItem.title=#"Twitter Profile";
CGRect webFrame = CGRectMake(0.0, 0.0, 320.0, 370.0);
UIWebView *webView = [[UIWebView alloc] initWithFrame:webFrame];
[webView setBackgroundColor:[UIColor whiteColor]];
NSString *urlAddress = #"http://twitter.com/#!/SummerApps";
NSURL *url = [NSURL URLWithString:urlAddress];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[webView loadRequest:requestObj];
[self.view addSubview:webView];
[webView release];
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
}
in simulator everything works fine but on device the page just doesn't load.
I also have the same code on another view for facebook and it works fine the link is the only difference! Any help?
have you connected your webView with outlet connection ? or check your internet setting in Simulator.
Well when i open the same link with safari on my i-device pops up an alert that tells me that the certificate is not trusted if i click ok then it loads? Do you know whats going on i think that's why it doesn't load in my built in web view
I managed to show a UIWebView that shows content from the internet.
But apparently it does not rotate as it should:
http://www.youtube.com/watch?v=VV0BmNkSrvU
I'm using this code:
UIWindow* window = [UIApplication sharedApplication].keyWindow;
if (!window) {
window = [[UIApplication sharedApplication].windows objectAtIndex:0];
}
_webView = [[UIWebView alloc] initWithFrame:rect];
_webView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
NSURL* url = [[NSURL alloc] initWithString:URL]; NSURLRequest* request = [[NSURLRequest alloc] initWithURL:url];
[_webView loadRequest:request];
//Unity's UIView
UIView* subView = [window.subviews objectAtIndex:0];
[url release];
[subView addSubview:_webView];
It seems that this code has not been placed inside a UIViewController. Autorotation works correctly only inside a UIViewController. If you want to manage it outside a UIViewController you must observe all orientation change notifications and then transform your view accordingly.
I've to switch a part of my app: I will implement a web view insread of a picture (imageview).
So I tried to make it with the interface builder but there is too many errors so I prefer to use code!
for the pictures, I user those lines :
image = [UIImage imageWithData:[NSData dataWithContentsOfURL:url]];
self.imageView = [[UIImageView alloc] initWithImage:image];
imageView.frame = CGRectMake(0,64,320,367);
[self.view addSubview:imageView];
[self.imageView release];
Now, I tried to use this code but nothing's showed:
#property (nonatomic, retain) IBOutlet UIWebView *webView;
...
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:#"http://www.google.fr"]]];
[self.view addSubview:webView];
If you know how to make .... would be nice for me,
THANKS!
Declare in your h file:
UIWebView *contentWebView;
And also tell to your viewController that you will implement <UIWebViewDelegate>
In viewDidLoad Add:
contentWebView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
contentWebView.delegate = self;
contentWebView.multipleTouchEnabled = YES;
contentWebView.scalesPageToFit = NO;
[self.view addSubview:contentWebView];
In dealloc don't forget to release the webView:
[contentWebView release];
And implement the delegate methods:
#pragma mark -
#pragma mark WebViewDelegates
- (void)webViewDidStartLoad:(UIWebView *)webView{
}
- (void)webViewDidFinishLoad:(UIWebView *)webView{
}
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error{
}
And also you need to load an request. You can doit in viewDidLoad after you have added the webView to your view:
[contentWebView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:#"http://www.apple.com/"]]];
adjust the frame as u needed and if add anything like imageview as subview to webview then u have to use bringsubviewtofront function.
- (void) initUIWebView {
NSLog(#"DetailViewController->initUIWebView {");
UIWebView *aWebView;
// init and create the UIWebView
aWebView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 400)];
aWebView.autoresizesSubviews = YES;
aWebView.autoresizingMask=(UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth);
//set the web view delegates for the web view to be itself
[aWebView setDelegate:self];
//Set the URL to go to for your UIWebView
NSString *urlAddress = #”http://www.google.com”;
//Create a URL object.
NSURL *url = [NSURL URLWithString:urlAddress];
//URL Requst Object
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
//load the URL into the web view.
[aWebView loadRequest:requestObj];
//add the web view to the content view
[self.view addSubview:aWebView];
[aWebView release], aWebView = nil;
}
- (void)viewDidLoad {
[self initUIWebView]; // <<<<<<<<<<<< this calls the UIWebView Function
[super viewDidLoad];
}