Webview not loading page in iOS app - ios

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];

Related

call functions on a uiwebview controller outside the Viewcontroller class in objective C

I have a uiwebview in my ios program and it is connected to a variable inside the ViewController interface
#interface ViewController() <UIWebViewDelegate>
#property(weak, nonatomic) IBOutlet UIWebView *uiwebview;
#end
I want to call functions on this uiwebview from outside the viewcontroller
For example, I can control the webview just fine if it is used between
#implementation ViewController
.
.
.
#end
But I want to use it somewhere outside. For example, I want to do something like this
[uiwebview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:#"about:blank"]]];
from outside #implementation ViewController
What are the options to achieve this?
But I want to use it somewhere outside. For example, I want to do something like this
[uiwebview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:#"about:blank"]]];
from outside #implementation ViewController
Stop wanting that. An IBOutlet should be private to its own view controller. If you want other view controllers to be able to do something to that web view, then arm your ViewController with public methods that they can call.
I think I understand what you are trying to do, I have an app that has various tabs some of which are web view. When my app starts I get it to pre-load the web views so that when the tabs are selected they are already loaded. Its important the the other tabs are all singleton classes so that there is only one instance of them.
In my main ViewController viewDidLoad I have this ..
//Pre load calculators and status page
EvolutionViewController *evoCalc = [EvolutionViewController sharedInstance];
[evoCalc.view layoutSubviews];
IVCalcViewController *ivCalc = [IVCalcViewController sharedInstance];
[ivCalc.view layoutSubviews];
StatusViewController *serverStatus = [StatusViewController sharedInstance];
[serverStatus.view layoutSubviews];
Those instantiate the classes for the other tabs which also loads up the web views associated with those classes. All the classes are similar this is the .h
#import <UIKit/UIKit.h>
#interface EvolutionViewController : UIViewController <UIWebViewDelegate>
+ (EvolutionViewController *) sharedInstance;
#property (nonatomic, retain) IBOutlet UIWebView *webView;
#end
And the .m
#import "EvolutionViewController.h"
#interface EvolutionViewController ()
#end
#implementation EvolutionViewController
#synthesize webView;
#pragma mark - Singleton Methods
static EvolutionViewController *_sharedInstance;
- (id) init
{
if (self = [super init])
{
// custom initialization
}
return self;
}
+ (EvolutionViewController *) sharedInstance
{
return _sharedInstance;
}
-(id)initWithCoder:(NSCoder *)decoder {
if (!_sharedInstance) {
if (self = [super initWithCoder:decoder]) {
_sharedInstance = self;
}
}
return _sharedInstance;
}
#pragma mark - View Controller Methods
-(void)viewWillLayoutSubviews {
self.webView = [[UIWebView alloc] init];
}
- (void)viewDidLoad {
[super viewDidLoad];
NSURL *url = [[NSBundle mainBundle] URLForResource: #"evo_calc" withExtension:#"html"];
NSURLRequest*request=[NSURLRequest requestWithURL:url];
[self.webView loadRequest:request];
}
#end
Mine uses local html resources but works the same with a proper URL as well.

How do I find an activity indicator in a UITableViewCell on completion of multiple UIWebView requests?

I am pretty new to Objective-C and iOS programming.
I have a SummaryUITableViewCell (a custom class inheriting from UITableViewCell) which contains an Activity Indicator (loadingSpinner) and a UIWebView (webView).
My app gets a list of URLs to load, then presents the table view, one cell per URL.
In cellForRowAtIndexPath I start the animation for the loading spinner and call cell.webView loadRequest:URL.
Everything works fine and webViewDidFinishLoad gets called once per URL (it has just an NSLog statement in it for now). What I can't figure out is how to find the appropriate loadingSpinner so that I can stop the animation and hide it.
You want each of your SummaryUITableViewCell to implement UIWebViewDelegate and handle the webViewDidFinishLoad call themselves. Then you can easily hide the spinners each time the UIWebView loads. Here is one way you could implement the SummaryUITableViewCell.
SummaryTableViewCell.h
#import <UIKit/UIKit.h>
#interface SummaryTableViewCell : UITableViewCell <UIWebViewDelegate>
#end
SummaryTableViewCell.m
#import "SummaryTableViewCell.h"
#interface SummaryTableViewCell ()
// Keep references to our spinner and webview here
#property (nonatomic, strong) UIActivityIndicatorView *spinner;
#property (nonatomic, strong) UIWebView *webView;
#end
#implementation SummaryTableViewCell
- (instancetype)initWithUrl:(NSString *)url {
self = [super init];
if (self) {
[self setup:url];
}
return self;
}
- (void)setup:(NSString *)url {
// Add Webview
self.webView = [[UIWebView alloc] initWithFrame:[self frame]];
[self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:url]]];
[self.webView setAlpha:0];
// Set the cell as the delegate of the webview
[self.webView setDelegate:self];
[self addSubview:self.webView];
// Add Spinner
self.spinner = [[UIActivityIndicatorView alloc] init];
[self addSubview:self.spinner];
}
- (void)webViewDidFinishLoad:(UIWebView *)webView {
// The web view loaded the url so we can now hide the spinner and show the web view
[self.spinner setAlpha:0];
[self.webView setAlpha:1];
}
#end

How to give UIWebView access to current gps location

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."

iOS 7 webview not working

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.

Memory warning after loading numerous pages, need to clear memory from webview

I have a gif I have embedded into a html style web page in the supporting files. Every view controller has a webview in it with this html page. The gif is a moving background which I want in all of my view controllers (45 total). I cant find a way to clear the memory after moving on to the next viewcontroller. After about 14 viewcontrollers back and forth, the app crashes. Here is what I have:
In the H:
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController
#property (nonatomic, strong) IBOutlet UIWebView *webView;
#end
In the M:
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
#synthesize webView;
- (void)viewDidLoad
{
NSString *pathImg = [[NSBundle mainBundle] pathForResource:#"background" ofType:#"gif"];
NSString* webViewContent = [NSString stringWithFormat:
#"<html><body><img style='width:760;height:1024;' src=\"file://%#\" /></body></html>", pathImg];
[webView loadHTMLString:webViewContent baseURL:nil];
webView.scrollView.bounces = NO;
webView.scrollView.scrollEnabled = NO;
webView.opaque=FALSE;
[webView setBackgroundColor:[UIColor clearColor]];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
#end
Thanks for any help!

Resources