I am trying to load a URL when my app starts.
I dragged an UIWebView Object , on my .nib , i created a property outlet , i connected it to the webview and in my view controller i place the following code :
- (void)viewDidLoad
{
[super viewDidLoad];
NSString *fullURL = #"http://google.com";
NSURL *url = [NSURL URLWithString:fullURL];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[webView loadRequest:requestObj];
}
However i just get a black screen on the device , like theres no view at all. Any idea?
This is the code I'm using in my application
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
NSURL *websiteUrl = [NSURL URLWithString:#"http://www.google.com"];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:websiteUrl];
[myWebView loadRequest:urlRequest];
}
This is short one
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
[myWebView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:#"http://www.google.com"]]];
}
Restart your IOS simulator.
It is really not evident, but check, at first, any site in Safari at IOS simulator.
After restart of IOS simulator, my webView opened successfully both at simulator and device.
By Amit
Related
I'm trying to override a url with the following code:
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
NSString *urlString = #"http://www.google.com/";
NSURL *url = [NSURL URLWithString:urlString];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
[_udazzWebView loadRequest:urlRequest];
NSLog(#"log");
}
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest: (NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
NSLog(#"log2");
//use NSURLRequest object request , to manage the request.
NSURL *urL=request.URL;
NSString *urlStr=[urL absoluteString];
NSLog(#"URLL %#",urlStr);
if([urlStr isEqualToString:#"PostPicPopUp"]){
NSLog(#"log3");
}
return YES;
}
Log2 doesn't appear in the console. I'm guessing it has something to do with replacing NSURLRequest in the viewDidLoad, but I don't know how to do it.
It seems to me you are just missing UIWebView delegates init like
- (void)initWebViewWithRect:(CGRect)rect {
self.webView = [[UIWebView alloc] initWithFrame:rect];
self.webView.backgroundColor = [UIColor whiteColor];
self.webView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
self.webView.opaque=NO;
self.webView.userInteractionEnabled=YES;
self.webView.delegate=self;
[self cleanSubViews];
}
You UIWebView should be defined like
#interface MXMBaseWebView()<UIWebViewDelegate, UIScrollViewDelegate, MXMWebViewProgressDelegate> {
CAGradientLayer *shadowLayer;
MXMWebViewProgressView *_progressView;
MXMWebViewProgress *_progressProxy;
UIRefreshControl *_refreshControl;
}
#property(nonatomic, strong) UIWebView *webView;
#end
so you will do calls like
[self.webView loadRequest: [NSURLRequest requestWithURL:url]];
At this point everything should work properly.
I create app to use UIWebview and show log to url if touch in content in UIWebview.
this is my code
- (void)viewDidLoad {
[super viewDidLoad];
NSURL *url1 =[NSURL URLWithString:#"MyWebSite"];
NSURLRequest *request1 = [NSURLRequest requestWithURL:url1];
[_webView1 loadRequest:request1];
NSURL *url2 =[NSURL URLWithString:#"MyWebsite2"];
NSURLRequest *request2 = [NSURLRequest requestWithURL:url2];
[_webView2 loadRequest:request2];//url menu 2
NSString *currentURL = [_webView1 stringByEvaluatingJavaScriptFromString:#"document.title"];
NSLog(#"%#",currentURL);
}
but I touch the content log is not print and change webview log is print (null)
sorry for my poor English.
You can not get title of a website before you receive data from the URL.
So
Set your webview delegate to self
Then in
- webViewDidFinishLoad: to get title
I have the one webview which load the this url
https://sketchfab.com/models/9fb581d7cb414e0fbec4108099a73619/embed.
After loading this url my web view start showing the memory warning and after some time it crash. I am using the arc in my application
Here is my code:
(void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
NSURL *sUrl = [NSURL URLWithString:[NSString stringWithFormat:#"%#/embed",m_Data.m_sModelUrl]];
NSMutableURLRequest *sRequest = [NSMutableURLRequest requestWithURL:sUrl];
[sRequest setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
[m_webView loadRequest:sRequest];
}
Please help me out.
You can try autoreleasepool to solve memory warning.
#autoreleasepool
{
NSURL *sUrl = [NSURL URLWithString:[NSString stringWithFormat:#"%#/embed",m_Data.m_sModelUrl]];
NSMutableURLRequest *sRequest = [NSMutableURLRequest requestWithURL:sUrl];
[sRequest setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
}
[m_webView loadRequest:sRequest];
I am learing WebView.
when I do something like
NSString *url = #"www.google.com";
[_webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:url]]];
it fails.
But when I type www.google.com (or even google.com) in standard browser, it works fine.
I also noticed that after loading the page, the url text field in standard browser changes the link from www.google.com to https:// www. google.co.in/?gws_rd=ssl
In above code when I set NSString *url = #"https://www.google.co.in/?gws_rd=ssl" it works fine
So how do I implement my WebView view so that if should work like a standard browser it terms of above context
UIWebView always needs http or https when starting request, if you click on links inside the webView it will handle it itself. So here is how to handle it:
- (void)viewDidLoad
{
[super viewDidLoad];
UITextField *addressBar = [[UITextField alloc] init];
[addressBar setDelegate:self];
[self loadRequestFromString:#"www.google.com"];
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[self loadRequestFromString:textField.text];
[textField resignFirstResponder];
return YES;
}
- (void)loadRequestFromString:(NSString *)urlString
{
NSURL *webpageUrl;
if ([urlString hasPrefix:#"http://"] || [urlString hasPrefix:#"https://"]) {
webpageUrl = [NSURL URLWithString:urlString];
} else if ([urlString containsString:#" "] || ![urlString containsString:#"."]) {
webpageUrl = [NSURL URLWithString:[NSString stringWithFormat:#"http://www.google.com/search?q=%#", [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]];
} else {
webpageUrl = [NSURL URLWithString:[NSString stringWithFormat:#"http://%#", urlString]];
}
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:webpageUrl];
[_webView loadRequest:urlRequest];
}
to open any URL in UIWebView you should add url with protocol
eg #"http://www.google.com".
[NSURL URLWithString:#"http://www.google.com"];
then it will work.
I am trying to programmatically declare a modal view controller from a view controller launched using storyboard. I would expect to see a blank view coming up but instead I only see the webview from the first controller.
- (void)viewDidLoad
{
[super viewDidLoad];
NSString *fullURL = #"http://google.com";
NSURL *url = [NSURL URLWithString:fullURL];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
_viewWeb.delegate=self;
[_viewWeb loadRequest:requestObj];
modalViewController=[[UIViewController alloc] init];
[self presentViewController:modalViewController animated:YES completion:nil];
}
You're doing this too soon. There's no interface yet, in viewDidLoad. Put that code into viewDidAppear: instead, and see what happens.