I have the following UIWebView implementation and I want to set the scroll after the webview is loaded. However, scroll programmatically has no effect on UIWebView.
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
NSString *urlAddress = #"http://www.xxxx.com/private-dining";
//Create a URL object.
NSURL *url = [NSURL URLWithString:urlAddress];
//URL Requst Object
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
//Load the request in the UIWebView.
[privateDiningWebView loadRequest:requestObj];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 1 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
privateDiningWebView.scrollView.contentOffset = CGPointMake(0, 300);
});
}
you need to call this in webView is Finish loading web content .
don't forget to add UIWebViewDelegate in you ViewController
– webViewDidFinishLoad:(UIWebView *)webView
{
// UI Operation should be Done in Main Queue so you need to get
// main Queue to Access your webView
dispatch_async(dispatch_get_main_queue(), ^{
privateDiningWebView.scrollView.contentOffset = CGPointMake(0, 300);
});
}
You can set delegate for UIWebview and try this code :
- (void)webViewDidFinishLoad:(UIWebView *)webView {
privateDiningWebView.scrollView.contentOffset = CGPointMake(0, 300);
}
Related
I have a UITableView within a UINavigationController,when I select a cell in the table view,it should slide to a UIWebView
//code in table view delegate(tableviewcontroller)
- (void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
NSURL *url = [NSURL URLWithString:#"https://www.baidu.com/"];
self.webViewController.title = #"UIWebViewTest";
self.webViewController.URL = url;
[self.navigationController
pushViewController:self.webViewController animated:YES];
}
self.webViewController was initialized by
- (void)viewDidLoad {
[super viewDidLoad];
//..
//other code
//..
YGGWebViewController *wvc = [self.storyboard
instantiateViewControllerWithIdentifier:#"wvc"];
self.webViewController = wvc;
}
in the YGGWebViewController, I have a NSURL *URL property and override the setter
- (void)setURL:(NSURL *)URL{
_URL = URL;
if (_URL) {
NSURLRequest *req = [NSURLRequest requestWithURL:_URL];
[self.webView loadRequest:req];
}
}
besides,YGGWebViewController also have a UIWebView *webView property linked to a UIWebView in storyboard.
what odd is the web view does not display anything when it slides to the screen in the first time,but it will show the website in the second time(go back to the table view by navigation bar,and select a cell again).
Environment
xcode-7.0
deployment target-7.0
OS X Yosemite-10.10.5
P.S
if I move the code
if (_URL) {
NSURLRequest *req = [NSURLRequest requestWithURL:_URL];
[self.webView loadRequest:req];
}
to viewDidLoad in YGGWebViewController,it works right.but why?
I have a UIViewController class named WebScreenViewController, which is having an UIWebView. Here is the class implementation file:
#implementation WebScreenViewController
#synthesize urlString;
- (void)viewDidLoad
{
[super viewDidLoad];
self.webView.delegate = self;
// Do any additional setup after loading the view.
[self gotoUrl:self.urlString];
//[self gotoUrl:#"http://www.google.com"];
}
#pragma mark - UIWebView Delegates
-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
NSURL* url = [request URL];
NSString* urlStr = url.absoluteString;
NSLog(#"shouldStartLoadWithRequest. URLStr = %#", urlStr);
return YES;
}
-(void)webViewDidStartLoad:(UIWebView *)webView
{
NSLog(#"webViewDidStartLoad");
}
-(void)webViewDidFinishLoad:(UIWebView *)webView
{
NSString* html = [webView stringByEvaluatingJavaScriptFromString:#"document.body"];
NSLog(#"webViewDidFinishLoad: HTML string = %#", html);
}
-(void) webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
NSLog(#"didFailLoadWithError. ERROR: %#", error);
}
#pragma mark - Member Methods
-(void)gotoUrl:(NSString*)urlStr
{
NSLog(#"Calling website %# in UIWebView...", urlStr);
NSURL* url = [NSURL URLWithString:urlStr];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
[self.webView loadRequest:urlRequest];
}
#end
The above code work well on one of my testing device but did not work on another. It worked on my iPod Touch with the latest iOS 8.1.2. But it didn't work on my iPad3 with iOS version 7.0. When I run it on my iPad3, it will call the gotoUrl: method and the log "Calling website %# in UIWebView..." will be displayed. And that's it. I receive no more logs from the delegates. To check the internet connections, I tried using Safari on the same device and it was working.
Check whether you've connected the web view's outlet correctly in iPad storyboard if you're using different storyboards for iPhone & iPad.
Also check that the web view is not nil.
Hope this helps..
NSString * strURL = [NSString stringWithFormat:#"http://www.google.com/"];
NSURL *loadURL=[NSURL URLWithString:strURL];
NSURLRequest *loadRequest = [NSURLRequest requestWithURL:loadURL];
[webView loadRequest:loadRequest];
webView.delegate=self;
Try this way in your view did load or if you have URL you can replace first line like this
NSString * strURL = [NSString stringWithFormat:self.urlString];
Hey guys, i'm trying to make the buttons on my project to open a different webview url. I'm new to iOS programming, but i've used andriod programming. Is this possible? I've a ready created another webview view that sits in the supporting files folder.
Here is my code below
Viewcontroller.m
#import "ViewController.h"
#implementation ViewController
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
-(void)viewDidLoad
{
NSString *urlString = #"http://www.athletic-profile.com/Application";
//Create a URL object.
NSURL *url = [NSURL URLWithString:urlString];
//URL Requst Object
NSURLRequest *webRequest = [NSURLRequest requestWithURL:url];
//Load the request in the UIWebView.
[webView loadRequest:webRequest];
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
}
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
}
- (void)viewDidDisappear:(BOOL)animated
{
[super viewDidDisappear:animated];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
#synthesize webView;
#end
Add a button to your View then give a connection to it. In the action method just write your code
-(IBAction*)yourButtonActionMethod:(id)sender
{
[UIWebView *aWebView =[[UIWebView alloc] initWithFrame:CGRectMake(x,y,width,height)];
aWebView.delegate=nil;
NSString *urlString = #"http://www.athletic-profile.com/Application";
//Create a URL object.
NSURL *url = [NSURL URLWithString:urlString];
//URL Requst Object
NSURLRequest *webRequest = [NSURLRequest requestWithURL:url];
//Load the request in the UIWebView.
[aWebView loadRequest:webRequest];
[self.view addSubView:aWebView];
}
Firstly you can drag & drop UIButton in First Class & create action of that Button then create a new class for UIWebView in which you want to open you url named your choice which is subclass of UIViewController then into your .Xib or Storyboard you just drag & drop the UIWebView then create outlet of UIWebView after you write this code in first class of that button's action :-
- (IBAction *) firstButton:(id) sender
{
NSString *urlString = #"http://www.athletic-profile.com/Application";
//Create a URL object.
NSURL *url = [NSURL URLWithString:urlString];
//URL Request Object
NSURLRequest *webRequest = [NSURLRequest requestWithURL:url];
//Load the request in the UIWebView.
[webView loadRequest:webRequest];
}
How can I make a "Pull to refresh" in iOS 5 for an UIWebView ? I can't find a tutorial.
I have got a WebView in a Tab Bar application, and I want to refresh the WebView with a "Pull to refresh" like Twitter.
Where can I find a complete tutorial ?
Thank you
I cannot provide a tutorial, maybe I'll write one but I think it was quite simple,
I did this slightly modifying SPPullView.
Here you can grab the files (SPPullView.m and SPPullView.h).
Relevant code in the main UIViewController is something like:
- (void)viewDidLoad
{
[super viewDidLoad];
self.pullView = [[SPPullView alloc] initWithScrollView:self.webView.scrollView];
[self.pullView setDelegate:self];
[self.webView.scrollView addSubview:self.pullView];
[self.webView.scrollView setDelegate:self];
for (UIView *subview in [self.webView.scrollView subviews] ) {
subview.userInteractionEnabled = NO; // used to disable copy/paste, etc inside the webview
}
//[self doYourStuff];
}
//Called when the user pulls to refresh (this is when you should update your data)
- (void) pullViewShouldRefresh: (SPPullView *) view {
[self updateYourStuff];
}
You can also add some logic or properties to prevent the user zooming, or deactivate the pull view temporarily while zooming.
This works very well: http://blog.gomiso.com/2012/03/22/yet-another-pull-to-refresh-library/
I wrote this code and it is working fine. This may help you.
- (void)viewDidLoad
{
[super viewDidLoad];
NSString *fullURL = URL you want to hit;
NSURL *url = [NSURL URLWithString:fullURL];
NSURLRequest *requestObject = [NSURLRequest requestWithURL:url];
webView.delegate = (id)self;
[webView loadRequest:requestObject];
UIRefreshControl *refreshControlOnPull = [[UIRefreshControl alloc] init];
[refreshControlOnPull addTarget:self action:#selector(handleRefresh:) forControlEvents:UIControlEventValueChanged];
[webView.scrollView refreshControlOnPull]; //<- this is point to use. Add "scrollView" property.
}
-(void)handleRefresh:(UIRefreshControl *)refresh {
// Reload my data
NSString *fullURL = The Url you want to hit;
NSURL *url = [NSURL URLWithString:fullURL];
NSURLRequest *requestObject = [NSURLRequest requestWithURL:url];
[webView loadRequest:requestObject];
[refresh endRefreshing];
}
Got a Problem that my activity indicator does not show up anyone a idea?
ViewController.h
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController
{
IBOutlet UIWebView *webview;
IBOutlet UIActivityIndicatorView *active;
UIAlertView *alert_start;
}
ViewController.m
// Do any additional setup after loading the view, typically from a nib.
[webview loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"home-de" ofType:#"html"]isDirectory:NO]]];
- (void)webViewDidStartLoad:(UIWebView *) webview
{
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
[active startAnimating];
}
- (void)webViewDidFinishLoad:(UIWebView *) webview
{
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
[active stopAnimating];
}
- (IBAction)tele_button:(id)sender
{
//Local HTML Call Button
NSURLRequest *theRequest = [NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"phone" ofType:#"html"]isDirectory:NO]];
[self->webview loadRequest:theRequest];
}
- (IBAction)mail_button:(id)sender
{
//Mail App Mail Button
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:#"mailto://info#optibelt.com"]];
}
- (IBAction)web_button:(id)sender
{
//Local HTML Button
NSURLRequest *theRequest = [NSURLRequest requestWithURL:[NSURL URLWithString: #"http://optibelt.com"]];
[self->webview loadRequest:theRequest];
}
- (IBAction)news_button:(id)sender
{
//local Home Button
NSURLRequest *theRequest = [NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"home-de" ofType:#"html"]isDirectory:NO]];
[self->webview loadRequest:theRequest];
}
The activity indicator is set as behavior animating (checked) and hides when stopped (checked)
Would be great if i get some help :)
You might want to check a couple of things. First, check to see if the activity indicator outlet is connected in IB. Second, check to see if the activity indicator is above the UIWebview. It could be that it is being covered up. You also might want to check that it isn't hidden in IB.
Good luck