when i build my apps with one page the code works fine, but when i use more than one view the app will not display the webpage, evan tough i am using identical code
in view controller.h i have this code
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController{
IBOutlet UIWebView *webview;
}
#end
in view controlled.m i have this code
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
[webview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:#"http://www.twitter.com/GeekyLemon"]]];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
now this code works fine and is the same in the other project yet the one with multiple views won't work.
Have you checked these points :
have you set the controller as a WebViewDelegate ?
Did you linked the webview from storyboard to your IBOutlet in controller ?
When you're debugging, is the instance of your webview different from nil ?
Its hard to fix your issue without a detailed description, but try to add this method into .m file. :
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[webview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:#"http://www.twitter.com/GeekyLemon"]]];
}
and from viewDidLoad remove this line : [webview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:#"http://www.twitter.com/GeekyLemon"]]];
viewDidLoad is invoking only once I suppose that this issue apper when you go to other ViewController and then go back.
Related
Does anyone know how to leave pdf with webview
this app is done with xcode,i learn it from internet
but I got a issue, when I click a download link
it would open the file in app , but I can't go back to
previous page , I can't do anything but close app & restart
So what should I add to make app go back to previous page
here is my app ,
viewcontroller.h
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController
#property (weak, nonatomic) IBOutlet UIWebView *webview;
#end
viewcontroller.m
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
NSURL *url = [NSURL URLWithString:#"http://mywebsite/index.php"];
[self.webview loadRequest:[NSURLRequest requestWithURL:url]];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
I have developed a website in Twitter Bootstrap, I am now using a UIWebView to display that site inside of an IOS app. I am getting it to display fine, but the screen is off and it is running off of the side of the screen... I feel like I might have my UIWebView configured wrong, the site looks like if I go to it in Safari on my mobile device, so I know the site is working properly.
It even displays Google wrong in the UIWebView.... I will display my code and 2 screen shots below(The first screenshot is what it currently looks like, the second is what it should look like and what it looks like in safari mobile :
ViewController.h
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController
#property (strong, nonatomic) IBOutlet UIWebView *webView;
#end
ViewController.m
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
NSURL *url = [NSURL URLWithString:#"http://www.google.com"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[_webView loadRequest:request];
_webView.scalesPageToFit = YES;
_webView.frame=self.view.bounds;
[_webView loadRequest:request];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
AS-IS Screen Shot
TO-BE Screen Shot
No need to use loadRequest: two time, Use:
- (void)viewDidAppear:(BOOL)animated{
[super viewDidAppear:animated];
[_webView reload];
}
To me it looks like the UIWebView is not set up correctly in the Storyboard or Interface Builder. Do you use Auto Layout? If so, are the constraints set correctly?
A simple way to make sure the UIWebView is displayed full screen would be to add 4 constraints that align it with the top, bottom, leading and trailing edges of its superview, e.g. like so:
EDIT: Here are some rough instructions on how to add those constraints.
You should add the following code after [_webView loadRequest:request]; :
_webView.scalesPageToFit = YES;
and If your UIWebView is not located properly, add the following code too :
_webView.frame=self.view.bounds;
In the Utilities pane, select "Scales Pages to Fit":
In the view controller in which the web view resides, make sure you can view the boxes (or bounds) of the view (note: the bottom doesn't appear because I couldn't fit it within the image):
Code in view controller:
#import "SomeViewController.h"
#interface SomeViewController ()
#end
#implementation SomeViewController
#synthesize webView;
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
NSString *urlLink = #"www.google.com";
NSURL *url = [NSURL URLWithString:urlLink];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
[self.webview loadRequest:urlRequest];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
Photo of the other settings in the view controller:
I have an important question. I'm creating an iPhone app.
I have an UIWebView (https://www.google.de/) with goBack, goForward and reload.
Now, I want a Button e.g. Bar Button Item named Home or Homepage, to open an URL (https://www.google.de/) in this WebView, to go back to the homepage.
How can I do this? How can I "say" the button to open the link in my WebView? I ask for answer.
// ViewController.h
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController
#property (nonatomic, strong) IBOutlet UIWebView *site ;
#end
// ViewController.m
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
-(IBAction)refresh:(id)sender; {
NSURL *url=[NSURL URLWithString: #"https://www.google.de/"]; NSURLRequest * requestURL=[NSURLRequest requestWithURL:url]; [_site loadRequest:requestURL];
}
- (void)viewDidLoad {
[self refresh:self];
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
UIWebView has methods, goBack and goForward. You can make actions and bind it with your bar buttons to go back and forward whenever you want.
- (IBAction)goBack:(id)sender {
[_site goBack];
}
- (IBAction)goForward:(id)sender {
[_site goForward];
}
- (IBAction)goHomepage:(id)sender {
NSURL *url=[NSURL URLWithString: #"https://www.google.de/"];
NSURLRequest *requestURL=[NSURLRequest requestWithURL:url];
[_site loadRequest:requestURL];
}
To help you more, here's a tutorial on this, and if you don't want to read that tutorial, you can check their source code from here !
I have a UIWebView inside my app, this loads a website which is trying to get the current gps location of the user. On Safari it presents a popup which asks if you want to give your current location to the website, on my UIWebView no such thing happens and my website is not able to get the gps location of the user. How can I accomplish this? I am new to ios developing so bear with me if this is something very easy to do, but I couldnt find an example so far.
Here is what I have so far:
ViewController.h
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController {
IBOutlet UIWebView *_webView;
}
#end
ViewController.m
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[_webView loadRequest:[[NSURLRequest alloc] initWithURL:[[NSURL alloc] initWithString:#"http://google.com"]]];
// Do any additional setup after loading the view, typically from a nib.
}
- (IBAction)goBack:(id)sender {
if ([_webView canGoBack]) {
[_webView goBack];
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
Edit your app's info.plist and set
NSLocationWhenInUseUsageDescription "String you want to explain why you need location."
I'm relatively new to programming in general, but I've attempted to find the answer to this and have failed to no end.
So basically I am attempting to create an application that has a map view scene, and that works fine. I then have another scene that is for web view. So when you go to the mapview scene there is a button that takes you to the webview scene.
The webview scene, however is not loading the web page at all, although when I create the webview part of the application in a different project with no other coding involved, it works just fine.
Here is the coding I have so far:
#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
#interface ViewController : UIViewController {
MKMapView * mapview; IBOutlet UIWebView *webview;
}
#property (nonatomic, retain) IBOutlet UIWebView*webview;
#property (nonatomic, retain) IBOutlet MKMapView *mapview;
-(IBAction)setMap:(id)sender;
-(IBAction)getlocation;
#end
--------
#implementation ViewController
#synthesize webview;
#synthesize mapview;
-(IBAction) getlocation {
mapview.showsUserLocation= YES;
}
-(IBAction)setMap:(id)sender {
switch(((UISegmentedControl *) sender).selectedSegmentIndex) {
case 0:
mapview.mapType= MKMapTypeStandard;
break;
case 1:
mapview.mapType= MKMapTypeSatellite;
break;
case 2:
mapview.mapType= MKMapTypeHybrid;
break;
default:
break;
}
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
[webview loadRequest:[NSURLRequest requestWithURL: [NSURL URLWithString: #"http://www.google.com"]]];
// Do any additional setup after loading the view, typically from a nib.
}
If you could help, would be greatly appreciated. Let me know if elaboration is required.
First make sure viewDidLoad is executing. There are some situations where it will not be executed.
Then check if webview is nil inside viewDidLoad. Most likely this is your problem. You probably aren't setting it properly, or you're setting it after viewDidLoad executes.
Best way to debug this is just to stick a breakpoint on the loadRequest line in your code, and have a look to see if:
it gets to your breakpoint at all
that the webview variable is not 0x00000000000
PS: you should delete these lines of code:
MKMapView * mapview; IBOutlet UIWebView *webview;
-(IBAction)setMap:(id)sender;
#synthesize webview;
#synthesize mapview;
They are not needed anymore in the latest version of Xcode and wherever you learned it from is outdated.
Also, use self.webview instead of just webview. Accessing it directly is dangerous and can cause bugs.
Create webView programmatically:
add in viewDidLoad:
UIWebView *webView = [[UIWebView alloc] initWithFrame:self.view.bounds];
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:#"http://google.co.uk"]]];
[self.view addSubview:webView];
please check web view delegate method
[webview setDelegate:self];