WKWebView White Screen (Objective-C) - ios

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

Related

WKWebKit Implementation (Objective-C)

Trying to display a web page in iOS, and I am having trouble implementing WKWebkit.
ViewController.h
#import WebKit;
#interface ViewController : BaseViewController
#property (retain, nonatomic) WKWebView *webView;
#end
ViewController.m
#interface ViewController ()
#property (strong, nonatomic) IBOutlet UIView *containerView;
#property (strong, nonatomic) IBOutlet NSLayoutConstraint *fieldViewBottomConstraintY;
#property (strong, nonatomic) IBOutlet NSLayoutConstraint *centerCredentialsConstraintY;
#end
#implementation ViewController
#synthesize webView;
-(void) viewDidLoad {
[super viewDidLoad];
webView = [[WKWebView alloc] initWithFrame:[[self view] bounds]];
NSURL *url = [NSURL URLWithString:#"https://www.google.com"];
NSURLRequest *urlReq = [NSURLRequest requestWithURL:url];
[webView loadRequest:urlReq];
[self.view addSubview:webView];
[self setupConfiguration];
// Do any additional setup after loading the view.
}
#end
I'm getting this error:
Class Unavailable: WKWebView before iOS 11.0 (NSCoding support was broken in previous versions)
I understand that this question is not very focused, but I'm on a tight schedule, still getting the hang of objective-c and iOS, and I haven't found a lot of implementable solutions, so I need help in solving this issue. I appreciate any help offered.
This is from a project that requires a minimum iOS version of iOS 12
Set your minimum iOS Deployment Target here:
and, check that it auto-updates here (and change it if not):
Set it to the minimum version you need to support.

Hitting iOS assertion when dismissing ViewController containing WKWebView displaying a PDF

I'm hitting an iOS assertion when dismissing a UIViewController. The UIViewController has a UIView, to which I've added a WKWebView. Into that WKWebView I've loaded a PDF file. The PDF displays and functions fine. When I dismiss the window, Xcode reports an assertion:
-[PDFExtensionTopView _accessibilityUnregisterRemoteView]: unrecognized selector sent to instance
If I have my All Objective-C Exceptions breakpoint on, execution is stopped. I can continue, but this is rather disruptive. Users never see any issue, so this is purely a development-time problem. I work around the issue by disabling breakpoints when using this view, but would prefer to find a real fix. I'm on Xcode 11.3.1, using ObjC in this class.
I've created a simplified dummy project. The entire UIViewController's code is below. It is presented from a simple UIButton that passes nothing to this UIViewController.
Moving the displayPDF method call is in viewWillAppear, but no change if in DidAppear, DidLoad, etc. It isn't related to the specific PDF (I've tried files from multiple sources).
If I use displayHTML (which puts dummy HTML into the webView), I do not experience the problem.
I might just be rusty in ObjC and missing something obvious. Any thoughts?
#import "MyPDFViewController.h"
#import <WebKit/WebKit.h>
#interface MyPDFViewController ()
#property (strong, nonatomic) IBOutlet UIView *pdfContainerView;
- (IBAction)closeButtonPressed:(id)sender;
#property (nonatomic, strong) WKWebView *webView;
#end
#implementation MyPDFViewController
-(void)viewWillAppear:(BOOL)animated {
[super viewDidAppear:animated];
[self displayPDF];
}
-(void)displayPDF {
NSString *path = [[NSBundle mainBundle] pathForResource:#"MyPDFFile" ofType:#"pdf"];
NSData *data = [NSData dataWithContentsOfFile:path];
self.webView = [[WKWebView alloc] initWithFrame:self.pdfContainerView.frame configuration:[[WKWebViewConfiguration alloc] init]];
[self.webView loadData:data MIMEType:#"application/pdf" characterEncodingName:#"utf-8" baseURL:[NSURL URLWithString:#""]];
[self.pdfContainerView addSubview:self.webView];
}
-(void)displayHTML {
[self.webView loadHTMLString:#"<html><body>Hi.</body></html>" baseURL:nil];
}
- (IBAction)closeButtonPressed:(id)sender {
[self dismissViewControllerAnimated:YES completion:^{
//
}];
}
#end

WebView Xcode 7 blank error

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.

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!

Webview not loading page in iOS app

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

Resources