I created a simple webview that loads
a pre-defined website in iOS app.
NSURL *url =[NSURL URLWithString:#"http://jihadkawas.com"];
NSURLRequest *req = [NSURLRequest requestWithURL:url];
[Webview loadRequest:req];
how can i add a URL BAR, so that user can access any url? like normal browsers?
Thank you!
You need to have a Textfield (the URL-Bar where Users can type in the website's address.
UITextField *urlBarTextField = [[UITextField alloc] initWithFrame:CGRectMake(0,0,320,40)];
[self.view addSubview:urlBarTextField];
And you need a go button or wait until the user pressed return on the Keyboard.
In the called method (either the Button target method or -(void)textField: shouldReturn:)
You need to load the new URL and display it in the UIWebView
NSURL *url = [NSURL URLWithString:urlBarTextField.text];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
[webView loadRequest:urlRequest];
That's it ;)
Just create a text field and a go button,
when the url presses the go button---
NSURL *url=[NSURL URLWithString:textField.text];
NSURLRequest *req = [NSURLRequest requestWithURL:url];
[Webview loadRequest:req];
There you go
Related
Same url in run in lower then iOS10 (ex. iOS8,iOS9) work properly.
-(void) loadUrl
{
NSString *urlAddress = #"http://myurl.com";
//Create a URL object.
NSURL *url = [NSURL URLWithString:urlAddress];
//URL Requst Object
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
//Load the request in the UIWebView.
[detailWebView loadRequest:requestObj];
}
I tried your code and it works fine in ios 10 and xCode 8.
I used this,
-(void) loadUrl
{
NSString *urlAddress = #"http://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];
}
And call this method where you want using [self loadUrl];
Add URLSchemes in your plist file for your url.
I am getting webview like,
Your code is not wrong.
Hope this will help you.
In apps that run in iOS 8 and later, use the WKWebView class instead of using UIWebView.import webkit.h and use this code
#import <WebKit/WebKit.h>
WKWebViewConfiguration *theConfiguration = [[WKWebViewConfiguration alloc] init];
WKWebView *webView = [[WKWebView alloc] initWithFrame:self.view.frame configuration:theConfiguration];
NSURL *nsurl=[NSURL URLWithString:#"http://www.apple.com"];
NSURLRequest *nsrequest=[NSURLRequest requestWithURL:nsurl];
[webView loadRequest:nsrequest];
[self.view addSubview:webView];
For a online radio streaming app, I have the code below so far. The problem that I have is when I press play, a webview opens up and when I close it the stream stops. How do I make this webview not appear on the screen/do it in the background?
ViewController.h
#interface ViewController : UIViewController
{
IBOutlet UIWebView *webView;
}
-(IBAction)play:(id)sender;
ViewController.m
-(IBAction)play:(id)sender
{
NSString *stream = #"STREAMING LINK";
NSURL *url = [NSURL URLWithString:stream];
NSURLRequest *urlrequest = [NSURLRequest requestWithURL:url];
[webView loadRequest:urlrequest];
}
Also while using webview, would I be able to make a pause/stop button and a volume switch as well?
Forget Interface Builder, and do it programmatically.
UIWebView *webView = [[UIWebView alloc]init];
NSURL *url =[NSURL URLWithString:#"STREAMING LINK"];
NSURLRequest *req = [NSURLRequest requestWithURL:url];
[webView loadRequest:req];
I'm using a UIWebView to display a mobile website on my iPhone. The UiWebView is smaller than the display of the iPhone.
My Problem is that the content size of the website (a mobile website based on JQuery mobile) doen't fit the size of the web view.
Just an example with a small web view trying to display the google mobile website:
NSString *urlAdress = #"http://google.de";
NSURL *url = [NSURL URLWithString:urlAdress];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[webView loadRequest:request];
[webView sizeToFit];
Primarily : set the delegate to the WebView
In .h file
#interface WebViewViewController : UIViewController<UIWebViewDelegate>
In .m file
webView.delegate =self;
webView.scalesPageToFit = NO;
NSString *urlAdress = #"http://google.de";
NSURL *url = [NSURL URLWithString:urlAdress];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[webView loadRequest:request];
add these to your code for your webview :
[webView setScalesPageToFit:YES];
[webView setContentMode:UIViewContentModeScaleAspectFit];
NSURL *urlString = [NSURL URLWithString:urlAddress];
//URL Requst Object
NSURLRequest *requestObj = [NSURLRequest requestWithURL:urlString];
//Load the request in the UIWebView.
[self.webview loadRequest:requestObj];
The UIWebView is having trouble with redirects. How can I make sure the uiwebview handles redirects properly?
In order to do a redirect from one site to another, just load your website, but add a BOOL var for checking if the right page is loaded.
BOOL page1Loaded;
then, if you need to change to another site, just create a global NSString var like so
// for site name
NSString *urlAddress;
and just update upon needing to
if(page1Loaded) {
// as seen in #Lithu T.V.'s comment, DO NOT FORGET the http:// or https://
// name of address
urlAddress = #"http://..." or #"https://..."
NSString *urlString = [NSURL URLWithString:urlAddress];
//URL Requst Object
NSURLRequest *requestObj = [NSURLRequest requestWithURL:urlString];
//Load the request in the UIWebView.
[self.webview loadRequest:requestObj];
}
else {
// as seen in #Lithu T.V.'s comment, DO NOT FORGET the http:// or https://
// name of redirect
urlAddress = #"http://..." or #"https://..."
NSString *urlString = [NSURL URLWithString:urlAddress];
//URL Requst Object
NSURLRequest *requestObj = [NSURLRequest requestWithURL:urlString];
//Load the request in the UIWebView.
[self.webview loadRequest:requestObj];
}
hope this helps!
I have the following code that allows me to load a set URL each time, placed in my viewDidLoad method:
NSString *urlAddress = #"http://google.com";
NSURL *url = [NSURL URLWithString:urlAddress];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[_webView loadRequest:requestObj];
Now the user can of course go to any webpage from here and if the user decides to dismiss the view I want for them to be able to return to it.
Now I have this code, through reading various forums and searches:
again the viewDidLoad - now looks like this:
NSURLRequest *currentRequest = [_webView request];
NSURL *currentURL = [currentRequest URL];
if(currentURL != nil)
{
NSString *urlAddress = currentRequest;
NSURL *url = [NSURL URLWithString:urlAddress];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[_webView loadRequest:requestObj];
}
else {
NSString *urlAddress = #"http://google.com";
NSURL *url = [NSURL URLWithString:urlAddress];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[_webView loadRequest:requestObj];
}
and in my viewDidUnLoad I have this:
-(void)viewDidUnload {
NSURLRequest *currentRequest = [_webView request];
NSURL *currentURL = [currentRequest URL];
NSLog(#"Current URL is %#", currentURL.absoluteString);
[NSURLConnection connectionWithRequest:currentRequest delegate:self];
}
a) Am I on the right track for achieving this?
b) I have a warning created on this line : `NSString *urlAddress = currentRequest;
which is:
Incompatible pointer types initializing 'NSString *__strong' with an expression of type 'NSURLRequest *__strong'
Any help is appreciated:-)
Thank you:-)
PS I started this problem out in this question, but felt it was worth a new one, so forgive me if you do not think the same.
`
NSString *urlAddress = currentRequest;
NSURL *url = [NSURL URLWithString:urlAddress];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[_webView loadRequest:requestObj];
Could just be
[_webView loadRequest:currentRequest];
You might want to experiment with not unloading the view/view controller when the user dismisses it then there won't be a time delay when the user brings it back. (Maybe you could instead make it hidden instead of unloading it for example when the user dismisses it, or send it to the back of the navigation controller stack, assuming you have one as the root view controller).
P.S.
Your warning is because you're trying to assign an NSURLRequest object to an NSString object.