Multiple UIButtons for One WebView - ios

I am looking for a quicker method to build my application. As i have a series of buttons that are all going to open a UIWebView. But, I want to be able to use only One (1) WebView. I also want to be able to have the Buttons in a different View Controller.
So my question is, How would you get buttons to go from one view controller into another view controller that has a web viewer in it, and still be able to get different weblink's to load from the buttons?
I am still new to the programing community. I am doing this all as self teaching also.
Thank You guys!

Create one xib which will contain one UIWebView and buttons
and in target method of buttons, you assign the new url are reload the webView
As mentioned below
-(void)reloadWebVieWithUrl:(NSString *)strUrl
{
NSURL *url = [NSURL URLWithString:strUrl];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[self.wbView loadRequest:request];
}
- (IBAction)buttonAction:(UIButton *)sender {
NSString *url = #"";
switch (sender.tag) {
case 100:
{
url = #"https://www.google.co.in/";
break;
}
case 101:
{
url = #"https://in.yahoo.com/";
break;
}
case 102:
{
url = #"https://www.facebook.com/";
break;
}
case 103:
{
url = #"http://stackoverflow.com/";
break;
}
default:
break;
}
[self reloadWebVieWithUrl:url];
}
In Swift
import UIKit
import WebKit
class ViewController: UIViewController {
#IBOutlet var wbView: UIWebView!
var strUrl = ""
#IBAction func buttonAction(sender: UIButton) {
switch (sender.tag){
case 100:
strUrl = "https://www.google.co.in/"
case 101:
strUrl = "https://in.yahoo.com/"
case 102:
strUrl = "https://www.facebook.com/"
case 103:
strUrl = "http://stackoverflow.com/"
default:
break;
}
reloadWebViewWithUrl(strUrl);
}
func reloadWebViewWithUrl(strUrl: NSString){
var url = NSURL(string: strUrl);
var request = NSURLRequest(URL: url);
wbView.loadRequest(request);
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}

You don't need multiple webviews or VC's to do this.
You can just change the NSURL to reflect the website you want to navigate the user to.
So for example the action for button one could be :
NSString *webSite = "#http://google.com";
NSURL *url = [NSURL URLWithString:webSite];
NSURLRequest *request = [NSUrLRequest requestWithURL:url];
[webView loadRequest:request];
Then for button two - use the same code, using the same webView and just change the string to the website you want to navigate the user to.
Hope this helps.

Related

Present SWRevealViewController When WebView link is tapped

I have a MainViewController with webViewMain.
The following functions checks if a link is ready to send a user to a certain directory in my webview.
- (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType {
NSURL *url = request.URL;
NSString *path = [url path];
if ([path isEqualToString:#"/mypath/"]){
NSLog(path);
return NO; }
return YES;
}
That code is all working fine. However, when the link is tapped I want to show my SWReveal Sidebar (sw_rear) and display the URL in that ViewController.
How can I do this?
I'm not understanding your scenario 100%, but with luck these steps will get you where you need to be.
Fetch the revealer and the rear view controller that holds your target webview:
SWRevealViewController *revealer = self.revealViewController; // 'self' here should be a view controller
UIViewController *rearVC = revealer.rearViewController;
UIWebView *webview = rearVC.webView;
Load up your URL:
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[webView loadRequest:request];

iOS 7 UIWebView inconsistent Loading

In my project I have 2 UIWebViews that are IBOutlets and created in the main.storyboard.
One I load by calling a method on ViewDidAppear. It appears perfectly and hits all it's delegate methods. We will call this ViewA
The other is supposed to be Hidden and appear and load when the user taps a button. We will call this ViewB. But once I submit the request, nothing happens, none of the delegate methods get called.
ViewA loads perfectly. If I try to load ViewB on ViewDidAppear, it also loads.
EDIT: Moving the ViewA and ViewB load method calls to the ButtonPress method calls both successfully.
EDIT: Code
-(void)loadCarouselWebView //ViewA
{
NSString *urlString = [NSString stringWithFormat:#"%#%#",[[JSONReader sharedInstance] baseURLToLoad:environmentURLPublicKey],[[JSONReader sharedInstance] URLToLoad:kCarouselURLkey]];
NSURL *url = [NSURL URLWithString: urlString];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
self.carouselWebView.delegate = self;
[self.carouselWebView loadRequest: requestObj];
}
-(void)loadFullWebView:(int)index //ViewB
{
NSString *gameURL;
switch (index) {
case 1:
gameURL = game1Constant;
break;
case 2:
gameURL = game2Constant;
break;
case 3:
gameURL = game3Constant;
default:
break;
}
NSString *urlString = [NSString stringWithFormat:#"%#%#",[[JSONReader sharedInstance] baseURLToLoad:environmentURLKey],[[JSONReader sharedInstance] URLToLoad:gameURL]];
NSURL *url = [NSURL URLWithString:urlString];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
self.fullWebView.delegate = self;
[self.fullWebView loadRequest: requestObj];
}
- (IBAction)gameButtonPushed:(id)sender {
[[self fullWebView] setHidden:NO];
[[self backNavButton] setHidden:NO];
[self loadFullWebView:[sender tag]];
[self loadCarouselWebView];//Added for debugging
}
EDIT: ONLY Loading ViewB on button tap works. If I add Load ViewA on ViewDidAppear then ViewB will not load.
In a surprise twist, there is nothing wrong with the UIWebView.
It was my fault for not reading the code carefully enough.
Someone coded a one time counter into the shouldStartLoadingRequest delegate method that I missed. So only one request was allowed to load.
Thats like 2 days burned there, great stuff! >:E

Can not open webpage using UIWebView

I'm writing an app which lets users open up youtube exercise/fitness videos from a table view. So, I want to pass in a given website address for each cell in the table view. The code executes without errors but in the simulator does not open any webpage when I select a cell. Here is the code of the implementation file:
- (void)viewDidLoad
{
[super viewDidLoad];
//Create a URL object
NSURL *url = [NSURL URLWithString:urlAddress];
//Switch values based on exerciseNumber
switch (exerciseNumber) {
case 0:{
self.title = #"Straight Leg Raise";
urlAddress = #"http://www.youtube.com/watch?v=sb_2OVC4TuM";
}
break;
case 1:{
self.title = #"Supine Hip Abduction with Elastic Band";
urlAddress = #"http://www.youtube.com/watch?v=9gnmbjrjEuM";
break;
}
default:{
self.title = #"No Exercise Selected";
urlAddress = #"http://www.rehabview.com";
break;
}
}
//Create a URL request object
NSURLRequest *request = [NSURLRequest requestWithURL:url];
//load the request in the UIWebView
[webView loadRequest:request];
}
Any idea what I am doing wrong? Thanks in advance.
Create your NSURL object after your switch statement that sets the urlAddress string.

Focus on TextBox and Show the Keyboard Automatically for Page Inside UIWebView Using iOS

I have a very simple page which is loaded inside the UIWebView using the following code:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
NSURL *url = [NSURL URLWithString:#"http://www.highoncoding.com/home/test"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[self.webView loadRequest:request];
}
And I am trying to focus on the TextBox "txtName" using the following code:
- (void)webViewDidFinishLoad:(UIWebView *)wv
{
NSLog(#"webViewDidFinished");
[wv stringByEvaluatingJavaScriptFromString:#"if(document.getElementById('txtName') != null) { document.getElementById('txtName').focus() }"];
}
Although it does find the TextBox but it never focus on the TextBox and the keyboard never pops up.

UIWebView loadRequest reuse gives flickering effect

I have multiple pdf files. Based on user input i am loading the pdf using UIWebView. On first loadRequest it loads the pdf properly. From second call to LoadRequest onwards its showing some flickering effect while loading the new pdf.. Meaning that starts blur display of content and slowly display content properly in few secs.
Code snippet below:
- (void) loadDocument: (NSString *) documentName
{
NSString * path = [[NSBundle mainBundle] pathForResource: documentName ofType: self.docType];
NSURL * url = [NSURL fileURLWithPath: path];
request = [NSURLRequest requestWithURL: url];
[PdfWebView loadRequest: request];
}
- (void) loadNewDoc:(int)segIndex
{
switch (mPageIndex)
{
case 0:
[self loadDocument:#"PDF_0"];
break;
case 1:
[self loadDocument:#"PDF_1"];
break;
case 2:
[self loadDocument:#"PDF_2"];
break;
default:
break;
}
}
You can clear the web view before starting the new request by using
[yourwebview loadHTMLString:#"<html><head></head><body></body></html>" baseURL:nil];
Or even
[yourwebview stringByEvaluatingJavaScriptFromString:#"document.open();document.close();"];
This may remove the flickering effect
for me, this helped (its robovm java code):
UIView.transition(_wv, 0.5, UIViewAnimationOptions.TransitionCrossDissolve, new Runnable() {
#Override
public void run() {
_wv.loadData(new NSData(pdf), "application/pdf", "utf-8", null);
}
}, null);

Resources