why does NOT UIWebView load the content in the first time - ios

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?

Related

Loading UIView from UITableViewController

I am trying to call a UIView inside of a UITableViewController.
JHWebViewController has been created in the storybaord.
JHWebViewController *myWebView = [[JHWebViewController alloc] init];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
myWebView.request = requestObj;
[self presentViewController:myWebView animated:YES completion:nil];
The error I get is Property 'request' not found on object of type 'JHWebViewController *'
You can write the relevant code in UITableView didSelectRowAtIndexPath method I hope it will work for you
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath`
{
[self.view addSubview:myWebView.view];
}
You try try this:
JHWebViewController *myWebView = [[JHWebViewController alloc] init];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[myWebView loadRequest:requestObj];
myWebView.scalesPageToFit = YES;
[self.view addSubview:myWebView.view];
Maybe try to embed the web view in a modal view controller and present the controller modally. Declare a public NSURLRequest property and set it from the table view delegate method.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath`
{
YourEmbeddingViewController *evc = [[YourEmbeddingViewController alloc] init]; // subclass of UIViewController
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
evc.request = requestObj;
[self presentViewController:evc animated:YES completion:nil];
}

using buttons to open webviews

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

Unable to display URL in WebView from another view controller

I have a UITableViewController that needs to open a web view.
In my function I have:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
LinkViewController *linkViewController = [[LinkViewController alloc] initWithNibName:#"LinkViewController_iPhone" bundle:nil];
[linkViewController.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:#"http://www.google.com"]]];
[self.navigationController pushViewController:linkViewController animated:YES];
}
I have connected all outlets and am not getting any warnings however I do not see the page pull up.
However if I go in the actual LinkViewController file and do something like:
- (void)viewDidLoad
{
[super viewDidLoad];
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 request in the UIWebView.
[self.webView loadRequest:requestObj];
}
everything seems fine. I do not understand why ?
You should add a URL property to your LinkViewController. Then in your table view controller you do this:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
LinkViewController *linkViewController = [[LinkViewController alloc] initWithNibName:#"LinkViewController_iPhone" bundle:nil];
linkViewController.URL = [NSURL URLWithString:#"http://www.google.com"];
[self.navigationController pushViewController:linkViewController animated:YES];
}
Now in your LinkViewController you do:
- (void)viewDidLoad {
[super viewDidLoad];
//URL Requst Object
NSURLRequest *requestObj = [NSURLRequest requestWithURL:self.URL];
//Load the request in the UIWebView.
[self.webView loadRequest:requestObj];
}
There are two problems with what you had originally:
In the table view controller, you were accessing the webView property before it was initialized. This is why nothing happened.
You were exposing far too much implementation detail of the LinkViewController to the outside world. If you use LinkViewController in many places in your app, you are requiring every user of the class to know to create a URL, create a request, and then tell the internal web view to start loading. All of that logic belongs inside the LinkViewController. Now each user of the class only needs to know to set a simple URL property.

How to use a pull to refresh in a WebView ( iOS 5 )?

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

Activity Indicator does not show

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

Resources