i am trying to launch a URL with WebView on xCode 7 and my issue is that it always appears a blank white page when i hit the Build button.
Below is the code:
ViewConntroller.h
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController
#property (weak, nonatomic) IBOutlet UIWebView *webView;
#property (weak, nonatomic) IBOutlet UIActivityIndicatorView *activityIndicator;
#end
ViewController.m
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
NSString *fullURL= #"http://www.google.com";
NSURL *url = [NSURL URLWithString:fullURL];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[_webView loadRequest:requestObj];
}
- (void)webViewDidStartLoad:(UIWebView *)webView
{
[_activityIndicator startAnimating];
}
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
[_activityIndicator stopAnimating]; _activityIndicator.hidden = TRUE;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
#end
Xcode 7 and iOS 9 introduced a system known as App Transport Security (ATS).
This will prevent the app from loading certain web resources and in a WebView this will lead to a blank screen.
The gist of this is you must choose from one of the following:
a) Only use HTTPS resources -> Perhaps try https://www.google.com (not tested this myself).
b) Selectively choose which NON-HTTPS resources to use -> See here for explanation.
c) Disable ATS all together -> Not advised, but unavoidable sometimes. To do this, find your Info.plist file and add the following:
NSAppTransportSecurity (Dictionary)
|
----> NSAllowsArbitraryLoads(Bool) = YES
OR in source code view (right click on file > Open As > Source Code):
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
See here for more.
Check the console of Xcode when running the app to see if you get an error message that says you must temporarily white list this site.
Related
Still having some trouble with Webkit. After sorting out my error from yesterday morning, I have encountered a whole new slue of them ranging from 113 to straight up crashes (really new to iOS dev, formally trained in C++ and very rusty haha).
I've finally got some code that doesn't crash and I feel like I'm definitely getting a better grasp on Objective-C/iOS Dev in general - there's just one issue... It doesn't load.
WebView.h
#ifndef WebView_h
#define WebView_h
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#import <Webkit/Webkit.h>
#interface ViewController: UIViewController;
#property (nonatomic, strong) IBOutlet WKWebView *webView;
#property (nonatomic, strong) IBOutlet UIView *view;
#end
#endif WebView_h
WebView.m
#implementation ViewController
#synthesize webView;
-(void) viewDidLoad {
[super viewDidLoad];
webView = [[WKWebView alloc] initWithFrame:[[self view] bounds]];
NSURL *url = [NSURL URLWithString:#"http://www.penelopeperu.com/"];
NSURLRequest *urlReq = [NSURLRequest requestWithURL:url];
[webView loadRequest:urlReq];
self.view = webView;
}
#end
I suspect it has something to do with the view / UIView and loading? I'm just not sure how to pinpoint what I'm doing wrong exactly.
Your url is not using "https" security protocol, so you need to add following key in Info.plist file to allow load your url in web view.
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
Also you are doing one more wrong thing with webview.
This doesen't proper way to add webview in self.view.
self.view = webView;
updated this line with;
[self.view addSubview:webView];
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 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'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];
I'm new for iOS develop. I tried to make a very simple app just show a UIWebView which display a web page:
ViewController.m:
#import "ViewController.h"
#implementation ViewController
#synthesize webView = _webView;
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
NSString *urlAddress = #"http://jsfiddle.net/emj365/qaQnF/embedded/result/";
NSURL *url = [NSURL URLWithString:urlAddress];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[self.webView loadRequest:requestObj];
}
...
ViewController.h
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController {
UIWebView * _webView;
}
#property (nonatomic, retain) IBOutlet UIWebView * webView;
#end
UIWebView show blank. What's wrong?
There are a few different things that could go wrong. A few options you should check:
Did you check to see if the outlet was actually connected to the webview in the xib?
Implement the UIWebView delegate and check for (networking) errors