Having a UITextField alter a Webview URL - ios

I have a viewcontroller with with a xib file. The file needs to have a TextField and a Button that leads to another viewcontroller with a web view. I want to have the URL be altered by whatever the user enters into the text field. So for example:
URL in WebView: http://www.google.com/
user enter football into text field
New Url in WebView: http://www.google.com/football
Any help would really be appreciated i already have my "main page" leading to a webview i just need to know what code needs to go were to alter the url.
Thanks in advance. If code is need i can post it

You could use string literals to pass the text of the box to the webview. Like this:
NSString *stringToPass = [NSString stringWithFormat:#"www.google.com/%#", self.textField.text];
Then, use a custom init method to pass your string to the new view:
self.viewController = [ViewController alloc]initWithAddress:stringToPass];
Make sure to declare -(id)initWithAddress:(NSString*)address; in the .h.

To carry on #CodaFi...
// WebViewController.m
#property (strong, nonatomic) NSURL *url;
#property (weak, nonatomic) UIWebView *webView;
//#synthesize, etc.
-(id)initWithAddress:(NSString*)address {
self = [super init];
if (self) {
self.url = [NSURL urlWithString:address];
}
return self;
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
NSURLRequest *request = [NSURLRequest requestWithURL:self.url];
[self.webView loadRequest:request];
}

Related

[iOS]How can I omit protocols?

I'm newbie of iOS
I'm trying to make a simple browsers using UIWebView.
This is my Code.
"ViewController.h"
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController
#property (strong, nonatomic) IBOutlet UIWebView *webView;
#property (weak, nonatomic) IBOutlet UITextField *addrInputField;
-(IBAction)enterButton:(id)sender;
/*go back btn*/
-(IBAction)goBackBtn:(id)sender;
/*go front btn*/
-(IBAction)goForwardBtn:(id)sender;
#end
"ViewController.m"
/*url enter button*/
-(IBAction)enterButton:(id)sender{
NSString *str = [_addrInputField text];
NSURLRequest *rqst = [NSURLRequest requestWithURL:[NSURL URLWithString:str]];
[_webView loadRequest:rqst];
}
/*go back button*/
-(IBAction)goBackBtn:(id)sender{
if([self.webView canGoBack]){
[self.webView goBack];
}
}
/*go forward button*/
-(IBAction)goForwardBtn:(id)sender{
if([self.webView canGoForward]){
[self.webView goForward];
}
}
This simple things are all of my code.
Input url in Text Field and touch enterButton then webView is changed.
When I input url in textField like : "http://apple.com", then view is changed.
But input url : "apple.com", view didn't work.
how can I omit the protocol when I input urls?
p.s I wonder how to handling both http and https.
Thanks for reading.
******Add details.
when I pressed Enter..
The Chrome modify my inputs "apple.com" to "https://apple.com" automatically adding protocol "https://".
I want adding this function in my Apps. Thanks.
Once you get the string from your textfield, after creating NSURL, you can retrieve URLScheme from it. If it's nil, you can add http yourself.
NSURL *url = [NSURL URLWithString:str];
if url.scheme == nil {
[NSString stringWithFormat:#"http://%#",str];
url = [NSURL URLWithString:str];
}

How to access instance from another function in objective c?

I am trying to access a webView object from another class.
viewController.m
#property (strong, nonatomic) TOWebViewController *webViewController;
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.webViewController = [[TOWebViewController alloc] initWithURLString:[NSString stringWithFormat:#"http://www.%#/", url] ];
[self setViewControllers:#[_webViewController]];
}
- (void)request {
NSURL *url=[NSURL URLWithString:#"http://www.google.com"];
NSURLRequest *request=[NSURLRequest requestWithURL:url];
[_webViewController.webView loadRequest:request];
}
TOWebViewController has the webView property to access the webview. When (void)request is called, something starts loading but it does not load it into the webview that was created in viewDidLoad. How do I have it reference the correct webViewController in (void)request?
I just looked at TOWebviewController on github and it states the following in TOWebViewController.h:
/**
The web view used to display the HTML content. You can access it through this
read-only property if you need to anything specific, such as having it execute arbitrary JS code.
#warning Usage of the web view's delegate property is reserved by this view controller. Do not set it to another object.
*/
#property (nonatomic,readonly) UIWebView *webView;
So the class doesn't sound like you need to interact with it by accessing it directly.
You more than likely want to just set the url:
/**
Get/set the current URL being displayed. (Will automatically start loading)
*/
#property (nonatomic,strong) NSURL *url;
So try:
- (void)request {
NSURL *url=[NSURL URLWithString:#"http://www.google.com"]
_webViewController.url = url;
}
You should try to use childViewController.
The reference document UIViewController Class Reference
Here are the essential methods you might need to call:
addChildViewController:
removeFromParentViewController:
willMoveToParentViewController:
didMoveToParentViewController:
My best guess is what you really want is to display the UIWebView inside the "parent" view controller, and that there is no need for a "child" view controller.
Why not do it like this?
#interface ViewController : UIViewController
#property (weak) IBOutlet UIWebView *webView; // set outlet in IB
#end
#implementation ViewController
// ...
- (void)request {
NSURL *url=[NSURL URLWithString: #"http://www.google.com"];
NSURLRequest *request = [NSURLRequest requestWithURL: url];
[self.webView loadRequest:request];
}
#end

ios UIButton not loading url in UIWwebview

I have made an iOS app to load a simple UIWebView *mWeb.
Once the app starts it loads my originalURL.
At the bottom of the UI, I have a UIButton placed *btnHome.
When I click this UIButton I want it load another URL *loadNextURL.
However something's not working on my end... HELP!
Here's the code for my myViewController.h file.
#import "myViewController.h"
#interface myViewController ()
#property (weak, nonatomic) IBOutlet UIButton *btnHome;
#property (weak, nonatomic) IBOutlet UIWebView *mWeb;
#end
#implementation myViewController
- (void)viewDidLoad
{
[super viewDidLoad];
NSString *originalURL = #"http://myOriginalLink";
NSURL *url = [NSURL URLWithString:originalURL];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[_mWeb loadRequest:requestObj];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
- (IBAction)btnHome:(id)sender
{
NSString *loadNextURL = #"http://link2";
NSURL *url2 = [NSURL URLWithString:loadNextURL];
NSURLRequest *reqObj = [NSURLRequest requestWithURL:url2];
[_mWeb loadRequest:reqObj];
}
#end
Well there can be two problems, first check that your action method is bound to the button or not. Second the url in stringVariable loadNextUrl is valid or not.
Now how to check the url is valid or not, just copy the url and paste into the browser. If the link does not open then the url is not valid. So for that pass the valid url in the string variable and then try.
Check your Url it may be invalid. If it works fine in your browser then check your method. It may not be linked with your button with touchUpInside event. Then try using [yourWebView loadRequest:[NSURLRequest requestWithURL:#"yourLink"]]; . Also do check whether your web view is not nil. It won't be nil as you are able to load it in viewDidLoad. But do check it.

WebView shows only white although delegate is set and methods are being called

I have a viewcontroller that loads a webView in the view did load
#synthesize webView = _webView;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
self.webView = [[UIWebView alloc] init];
[self.webView setDelegate:self];
NSString *string = #"http://www.google.com";
NSURL *url = [NSURL URLWithString:string];
NSLog(#"The URL is %#", url);
NSURLRequest *request = [NSURLRequest requestWithURL:url];
NSLog(#"The request is %#", request);
NSLog(#"webview delegate is %#", self.webView.delegate);
NSLog(#"webview is %#", self.webView);
[self.webView loadRequest:request];
}
Everything is working fine when i debug meaning that nothing is returning nil. I declared the property in the .h file like this
#property (nonatomic, retain) IBOutlet UIWebView *webView;
I have also tried
#property (nonatomic, strong) IBOutlet UIWebView *webView;
and the WebView in IB is hooked up to delegate and this property. The problem is that no matter what url I try (I have tried them all in the browser), the web view only returns a white screen. All the delegate methods have been called, I know because I logged to the console. Does anyone know how to fix this or what I am doing wrong.
From your code -
#property (nonatomic, retain) IBOutlet UIWebView *webView;
Since you are connected webview to an outlet, the instance of webview is created after the unarchiving of your Nib.
So no need to instantiate webview again -
self.webView = [[UIWebView alloc] init];
Remove the above line from your code and test it. Should work!
Well, the fix for this issue seems to me pretty straightforward.
You're not setting any frame for your web view. Try the following:
self.webView = [[UIWebView alloc] initWithFrame:CGRectMake(0,0,500,500)];
Of course, the actual dimensions for your web view may vary. You'll have to adjust it yourself.
Update
I'm seeing that you're not adding the web view as a subview. Add the following line after the call to loadRequest:
[self.view addSubview:self.webView];
Hope this helps!

Example of WebView tool

I don't know how to create WebView control. I want to display any web page in a WebView, so how can I do this?
First create a view based application, in the View, add a UIWebView, and in the .plist add a Application Uses Wi-Fi key, and set it YES. In the ViewController class' .h file make the code look like this:
#import <UIKit/UIKit.h>
#interface ClassNameHere : UIViewController {
UIWebView *webView;
}
#property (nonatomic, retain) IBOutlet UIWebView *webView;
#end
In the .m file, add this code:
#synthesize webView;
- (void)viewDidLoad {
NSString *urlString = #"http://www.google.com";
//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];
}
Finally, in your nib file, connect the webView outlet to the UIWebView. When you compile, it should open Google.

Resources