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];
}
Related
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
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.
I am making a music streaming app but i can only seem to add the controls to the default view controller this is my code for ViewController.h
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController {
IBOutlet UIWebView *WebView;
}
-(IBAction)Play:(id)sender;
#end
and this is my code for viewcontrol.m
-(IBAction)Play:(id)sender{
NSString *stream = #"http://117.53.175.113:15008/listen.pls";
NSURL *url = [NSURL URLWithString:stream];
NSURLRequest *urlrequest = [NSURLRequest requestWithURL:url];
[WebView loadRequest:urlrequest];
}
#end
Please help
Drag you IBOutlet from interface builder and to outside class parentheses {}.
Something like:
#interface ViewController : UIViewController {
IBOutlet UIWebView *WebView; // remove this.
}
#property (unsafe_unretained, nonatomic) IBOutlet UIWebView * WebView;
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];
}
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.