I am trying to open google.com with my webview but although the code has no errors page dont open just blank white screen.I tried the sample codes out there and followed tutorials but always blank white page comes up EVEN with sample codes and I cant solve it I worked with iad's before and they didnt cause me any problem so why is this simple webvies is causing problem please help me !
The webview object is connected to viewcontroller and I am using Xcode 4.3
#interface ViewController : UIViewController <UIWebViewDelegate>
{
IBOutlet UIWebView *webview;
}
#property (nonatomic, retain) UIWebView *webView;
#end
kk
#synthesize webView ;
- (void)viewDidLoad
{
[super viewDidLoad];
webview.delegate = self;
NSString *fullURL = #"http://google.com";
NSURL *url = [NSURL URLWithString:fullURL];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[webView loadRequest:requestObj];
}
Have you created it in loadView? Since you don't use the interfacebuilder...at least that is what I can tell from the posted code.
Related
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];
}
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 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!
I wanna to scroll(down) on a UIWebView(iPad Application), where I will display a local pdf file, programmatically.
I have added a UIWebView with the interface builder and linked it.
#interface ViewController : UIViewController <UIWebViewDelegate>
#property (weak, nonatomic) IBOutlet UIWebView *webView;
#end
This is my code for getting the pdf file and scrolling. The webViewDidFinishLoad function gets called.
#synthesize webView = _webView;
- (void)viewDidLoad
{
[super viewDidLoad];
NSString *path = [[NSBundle mainBundle] pathForResource:#"test" ofType:#"pdf"];
NSURL *targetURL = [NSURL fileURLWithPath:path];
NSURLRequest *request = [NSURLRequest requestWithURL:targetURL];
self.webView.delegate = self;
[self.webView loadRequest:request];
}
#pragma mark - UIWebViewDelegate
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
NSLog(#"didLoad");
self.webView.scrollView.contentOffset = CGPointMake(0, 500);
}
I am using the iOS SDK 6.1, the iOS deployment target is also 6.1.
Heres the log:
2013-04-23 09:23:08.742 PDFViewer[3834:c07] DiskImageCache: Could not resolve the absolute path of the old directory.
2013-04-23 09:23:08.913 PDFViewer[3834:c07] didLoad
The UIWebView isn't scrolling. The pdf is still at the first page.
already answered here --- how to set scroll position on uiwebview , try calling content offset code snippet after a delay
I don't think you need to set the webView.scrollView.contentOffset.
try this
[webView setScalesPageToFit:YES];
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.