Calling UIWebView from IBAction ButtonPressed - objective-c

This is probably a simple question...
I have a UIView with a button that corresponds to a URL address http:// etc. I want to be able to click this web address button and have it load a UIWebView on a separate UIViewController and XIB that shows the website, allowing me to then hit a back button and go back to my first view that has the original button. Basically I want to be able to load the webpage but have it operate within the App rather than start up Safari. Is this possible?
So far I have build a dedicated XIB and ViewController for my webpage for which the viewDidLoad method looks like this:
- (void)viewDidLoad{
[super viewDidLoad];
NSString *urlAddress = pushURL; //pushURL is passed as parameter
NSURL *url = [NSURL URLWithString:urlAddress];
NSURLRequest *requestObject = [NSURLRequest requestWithURL:url];
[webView loadRequest:requestObject];
[addressBar setText:urlAddress];
}
Question is... how do I call the UIWebView from my button (which is in the first class/parent viewController)? All my other viewControllers in my app are using UITableView so it's a simple DidSelectRowAtIndex.
Thanks.

It sounds like you want to be using a UINavigationController. Then when the button is tapped just push the UIViewController with the web view to the UINavigationController.

Related

Objective-c Opening video in webview performs viewdidload in rootViewcontroller

I have a strange problem and I have no idea how to solve it. I have a root viewcontroller. Via button you can switch to the child viewcontroller. In the child view controller I open a link like this as a request to the webview:
myURL = [NSURL URLWithString:#"http://m.imdb.com/video/imdb/vi2284695065/"];
NSURLRequest *myRequest = [NSURLRequest requestWithURL:myURL];
[myWebView loadRequest:myRequest];
the video on the page is autoplaying and switching to fullscreen mode. In exactly this moment I get a log message which indicates that the viewdidload method on my rootviewcontroller is called. Which eventually makes my code not working anymore. But why? Its not the viewdidload of the current viewcontroller its the one of the rootviewcontroller?! I donĀ“t know what kind of code I should provide actually...
Edit
I switch to viewcontroller like this:
UIViewController *NVC = [self.storyboard instantiateViewControllerWithIdentifier:#"child"];
[self presentViewController:NVC animated:NO completion:nil];
The rootviewcontroller does not get a received memory warning.

Using UIWebView's shouldStartLoadWithRequest with Storyboard and prepareForSegue

I have a non storyboard app that has a UITableView with a bunch of cells, upon clicking a cell I push a view controller with a UIWebView, the local html file is loaded based on the cell tapped.
If this UIWebView then has other links in it, I use the UIWebView delegate method shouldStartLoadWithRequest along with this code:
if(navigationType == UIWebViewNavigationTypeLinkClicked) {
//When a html link is clicked in uiwebview
NSURL *url = request.URL;
NSString *urlString = url.absoluteString;
//Get components of url
NSArray *urlComponents = [urlString componentsSeparatedByString:#"/"];
//Get last component which should be wanted file name
NSString *wantedLink = [urlComponents objectAtIndex:[urlComponents count]-1];
//Get the url without the dot
NSArray *fileComponents = [wantedLink componentsSeparatedByString:#"."];
NSString *wantedFile = [fileComponents objectAtIndex:0];
ALSDetailViewController *superDetail = [[ALSDetailViewController alloc] initWithNibName:#"ALSProtocolDetailViewController" bundle:nil];
self.alsProtocolDetailViewController = superDetail;
//Check for each wanted file name to see if it matches another file
if ([wantedLink isEqualToString:#"VTachPulseStable.html"]) {
//Open different protocol xib with html file name as parameter, and push the view in
self.alsProtocolDetailViewController.title = #"Ventricular Tachycardia With Pulse";
self.alsProtocolDetailViewController.protocolURL = wantedFile;
self.alsProtocolDetailViewController.protocolSubTitle = #"Stable Without Decompensation";
[self.navigationController pushViewController:self.alsProtocolDetailViewController animated:YES];
return NO;
}
}
This code is from my working old non-storyboard app.
Basically its reloading the same view its currently in with a new loadRequest and pushing it on top of the stack, so the user can go back to the last loadRequest.
Now with storyboard I can not figure out how to accomplish this same function with segue's, I need to invoke these actions and push the controller onto the stack.
I tried performSegueWithIdentifier but had no luck.
If I use a NSURL load request for the webview without pushing a new instance of the view controller on the stack, it works.
But I need the user to be able to go back.
Any ideas how I can accomplish this?
You can use UIWebView's goBack method:
goBack
Loads the previous location in the back-forward list.
You could add some buttons to accomplish this, in the nav bar or an extra toolbar. This is certainly better design than pushing a new view controller instance.
BTW, you can push a view controller onto itself via segues by dragging the segue to the very same view controller in storyboard.
I fixed the issue by using:
UIStoryboard *sb = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
And getting the storyboard item by its ID, and then using the old pushViewController.
Achieved the same effect.

How do I add a UIWebView to my iOS app?

I have an app at the moment that when a button is pushed on the first screen does some work and makes a URL, and then does
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:currentURL]];
which launches Safari with my URL. I want instead to have a webview launch from here so the user can do some custom things with it. I am still having a hell of a time understanding MVC in iOS and need help. My project is minimal and consists of an AppDelegate.h/m and a ViewController.h/m, the.m of the view controller is where the function that does this Safari launch lives.
Can anyone help me understand how to do what I'm trying to d?
Thanks...
The simplest way is just to add a UIWebView when the button gets pressed. Add this method to your ViewController.m and have this be performed when the button gets pressed.
Programmatically:
//This method should get called when you want to add and load the web view
- (void)loadUIWebView
{
UIWebView *webView = [[UIWebView alloc] initWithFrame:self.view.bounds]; //Change self.view.bounds to a smaller CGRect if you don't want it to take up the whole screen
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:currentURL]]];
[self.view addSubview:webView];
[webView release];
}
Using Interface Builder:
1) Add a UIWebView object to your interface.
2) Set the "hidden" property to checked (in the "Attributes Inspector" window in Interface Builder). You'll keep it hidden until you want to show it.
3) Add the following code to your ViewController.h, below the other #property lines:
#property (nonatomic, retain) IBOutlet UIWebView *webView;
4) Add the following line below the #synthesize in your ViewController.m
#synthesize webView;
And add [webView release]; in the dealloc method.
5) Go back into IB and click on File's Owner, and connect the webView outlet to the webView you created.
6) Add the following method instead of the method I showed above (for the programmatic example):
//This method should get called when you want to add and load the web view
- (void)loadUIWebView
{
[self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:currentURL]]];
self.webView.hidden = NO;
}
You could set up an IBAction method to respond to the button press, but it sounds like you already have the button press working, so I wouldn't bother with that now.
As far as adding a button above the web view, you can either subclass web view and add the button there, or just have a separate button you define in your nib or programmatically and have that hide the webView to "get rid of it".
For adding UIWebView using Swift to your app just drag the WebView to your story board and then using an assistant editor connect WebView to ViewController.swift
And then Loading URL to WebView is very easy. Just create a WebView in your storyboard and then you can use the following code to load url.
let url = NSURL (string: "https://www.simplifiedios.net");
let request = NSURLRequest(URL: url!);
webView.loadRequest(request);
As simple as that only 3 lines of codes :)
Ref: UIWebView Example
Since this is the top result on Google, if you can use WKWebView instead of UIWebView, you should.
import WebKit
let webViewConfiguration = WKWebViewConfiguration()
let webView = WKWebView(frame: CGRect(), configuration: webViewConfiguration)

How to open web browser with UIWebView

Hey guys,
On my Xcode project, I am trying to open a web browser at the click of one of my buttons, but here's what I'm really trying to do. First of all, I am opening a web browser using this example code:
NSURL *fanPageURL = [NSURL URLWithString:#"fb://profile/210227459693"];
if (![[UIApplication sharedApplication] openURL: fanPageURL]) {
NSURL *webURL = [NSURL URLWithString:#"http://www.facebook.com/pages/Blackout-Labs/210227459693"];
[[UIApplication sharedApplication] openURL: webURL];
}
[super viewDidLoad];
Now this code works properly, but the problem is that it's closing my application and opens the link in the Safari application, which is not what I want. I went the other way around by creating another view (after click the button), then inserted a UIWebView using Interface Builder, but it still didn't work. So I was hoping someone can help me out how to open this link within my app instead of closing my app and opening the link in the Safari app, thanks
Look at SVWebViewController. It's a ready made UIViewController subclass with a UIWebView in it and all you need to do to show it is
SVWebViewController *webViewController = [[SVWebViewController alloc] initWithAddress:#"http://google.com"];
[self.navigationController pushViewController:webViewController animated:YES];
There is also a modal version, see the Usage Section.
You'll need to create a UIViewController subclass that has UIWebView as a subview, and show that when the button in tapped.

Modal View Controller displays blank first time, works properly subsequently

I have a modal view controller that I am trying to present a web view in it. Tthe first time it appears, it shows up as blank. When I close it out and click it again, however, it displays fine. I'm thinking it may be an issue with the loading of the webView, but I've tried displaying it only when the webView finishes loading, but it never gets called then.
NSURL* newURL = [[NSURL alloc] initFileURLWithPath: fileString];
NSURLRequest *newURLRequest = [[NSURLRequest alloc] initWithURL: newURL];
[webViewController.webView loadRequest: newURLRequest];
[newURL release];
[newURLRequest release];
webViewController.modalPresentationStyle=UIModalPresentationPageSheet;
[self presentModalViewController:webViewController animated:YES];
The webView control won't be accessible the first time until the first present call causes controls to be loaded and initialized. Before the first present, webViewController.webView will be nil and so calling loadRequest on it will do nothing.
You could move the loadRequest call after the presentModalViewController call.
But instead of accessing controls in view controller's views directly, it'd be better to declare the url string as a NSString property (called say urlString) in WebViewController and set it to fileString before the presentModalViewController call (and don't create the NSURL, etc there):
webViewController.urlString = fileString;
[self presentModalViewController:webViewController animated:YES];
Finally, in WebViewController, in viewWillAppear: or viewDidAppear:, create the NSURL (using urlString), create the NSURLRequest, and call loadRequest.