WKWebKit Implementation (Objective-C) - ios

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.

Related

WKWebView White Screen (Objective-C)

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

exc_bad_access code=1 when loading UIView

I have an app where I need to present an overlay for feedback. I started by simply creating the exact thing I wanted within the UIViewController I wanted. However this presents to 2 problems. 1) I can't reuse this in another view (As I need to now) and 2) Because it's an overlay, it covers the entire UIViewController on the storyboard so I can't see the controls beneath it.
I looked at moving to an external UIView .xib file and loading dynamically which worked great, except whatever I did, I couldn't never get a handle on the labels within the nib to update the text.
Then I decided that making it a class and creating a delegate method for it would probably be the best way forward.
I have created a very simply .xib and laid it out as well as a .h and .m file (overlayView) and wired it all in an it looks good, except when trying to present the overlayView I get a exc_bad_access on the line
[window addSubview:self];
And I can't work out why. Full code below:
overlayView.h
#import <UIKit/UIKit.h>
#class overlayView;
#protocol overlayDelegate;
#interface overlayView : UIView
#property (nonatomic, strong) id <overlayDelegate> delagate;
-(instancetype)initWithTitle:(NSString *)title
dateFrom:(NSString *)dateFrom
dateTo:(NSString *)dateTo
description:(NSString *)description;
#property (strong, nonatomic) IBOutlet UILabel *overlayTitleLbl;
#property (strong, nonatomic) IBOutlet UILabel *overlayDateFromLbl;
#property (strong, nonatomic) IBOutlet UILabel *overlayDateToLbl;
#property (strong, nonatomic) IBOutlet UILabel *overlayDescLbl;
#property (strong, nonatomic) IBOutlet UILabel *overlayIcon;
-(void)showOverlay;
-(void)dismissOverlay;
#end
#protocol overlayDelegate <NSObject>
#optional
#end
overlayView.m
#import "overlayView.h"
#import "NSString+FontAwesome.h"
#implementation overlayView
- (instancetype)initWithTitle:(NSString *)title dateFrom:(NSString *)dateFrom dateTo:(NSString *)dateTo description:(NSString *)description {
self.overlayViewTitleLbl.text = title;
self.overlayViewDateFromLbl.text = dateFrom;
self.overlayViewDateToLbl.text = dateTo;
self.overlayViewDescLbl.text = description;
self.overlayViewIcon.text = [NSString fontAwesomeIconStringForIconIdentifier:#"fa-calendar"];
return self;
}
-(void)showOverlay {
UIWindow *window = [[UIApplication sharedApplication] keyWindow];
[window addSubview:self]; <-- Code causing issue
[window makeKeyAndVisible];
}
-(void)dismissOverlay {
// Not wired in yet
}
#end
The being called in my main view controller like:
overlay = [[overlayView alloc] initWithTitle:[tmpDict objectForKeyedSubscript:#"Title"] dateFrom:startDate dateTo:stopDate description:[tmpDict objectForKeyedSubscript:#"Desc"]];
[overlay showOverlay];
Any ideas why this doesn't want to play ball? I have breakpointed the initWithTitle method and all information is being passed correctly so I think I am very close to what I am trying to achieve.
you need to initiate your view first, you're returning self without initiating it
- (instancetype)initWithTitle:(NSString *)title dateFrom:(NSString *)dateFrom dateTo:(NSString *)dateTo description:(NSString *)description {
self = [super init];
if (self) {
self.overlayViewTitleLbl.text = title;
self.overlayViewDateFromLbl.text = dateFrom;
self.overlayViewDateToLbl.text = dateTo;
self.overlayViewDescLbl.text = description;
self.overlayViewIcon.text = [NSString fontAwesomeIconStringForIconIdentifier:#"fa-calendar"];
}
return self;
}

FBAudienceNetwork: setting FBNativeAd into FBMediaView stacks the UI

The following line of code makes my UI stack
adMediaView.nativeAd = nativeAd
// adMediaView - FBMediaView
// nativeAd - FBNativeAd
I've tried putting it in the performing it asynchronously in background thread but it didn't help. Is there a way to fix it?
dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_DEFAULT, 0),{
adMediaView.nativeAd = nativeAd
});
I've installed FBAudienceNetwork via pods and updated it just now. The latest version I have is 4.7.0
pod 'FBAudienceNetwork'
NativeAdView uses FBMediaView for creating an ad.
Now, in your View Controller header file declare FBNativeAdDelegate protocol as well as declare and connect instance variables to your UI .XIB:
#import FBAudienceNetwork; // import Audience Network module
#interface MyViewController : UIViewController <FBNativeAdDelegate>
// Other code might go here...
#property (weak, nonatomic) IBOutlet UIImageView *adIconImageView;
#property (weak, nonatomic) IBOutlet UILabel *adTitleLabel;
#property (weak, nonatomic) IBOutlet UILabel *adBodyLabel;
#property (weak, nonatomic) IBOutlet UIButton *adCallToActionButton;
#property (weak, nonatomic) IBOutlet UILabel *adSocialContextLabel;
#property (weak, nonatomic) IBOutlet UILabel *sponsoredLabel;
#property (weak, nonatomic) FBMediaView *adCoverMediaView;
#property (weak, nonatomic) IBOutlet UIView *adView;
#end
Then, add a method in your View Controller's implementation file that initializes FBNativeAd and request an ad to load:
FBNativeAd *nativeAd;
FBAdchoicesView *adChoicesView;
- (void)showNativeAd
{
nativeAd = [[FBNativeAd alloc] initWithPlacementID:#"YOUR_PLACEMENT_ID"];
nativeAd.delegate = self;
[nativeAd loadAd];
}
Now that you have added the code to load the ad, add the following functions to handle loading failures and to construct the ad once it has loaded:
- (void)nativeAdDidLoad:(FBNativeAd *)nativeAd
{
[self.adTitleLabel setText:nativeAd.title];
[self.adBodyLabel setText:nativeAd.body];
[self.SocialContextLabel setText:nativeAd.socialContext];
[self.sponsoredLabel setText:#”Sponsored”];
[self.adCallToActionButton setTitle:nativeAd.callToAction];
[nativeAd.icon loadImageAsyncWithBlock:^(UIImage *image) {
[self.adIconImageView setImage:image];
}];
// Allocate a FBMediaView to contain the cover image or native video asset
adCoverMediaView = [[FBMediaView alloc] initWithFrame:coverFrame]];
[adCoverMediaView setNativeAd:nativeAd];
// Add adChoicesView
adChoicesView = [[FBAdChoicesView alloc] initWithNativeAd:nativeAd];
[adView addSubview:adChoicesView];
[adChoicesView updateFrameFromSuperview];
// Register the native ad view and its view controller with the native ad instance
[nativeAd registerViewForInteraction:adView withViewController:self];
}
- (void)nativeAd:(FBNativeAd *)nativeAd didFailWithError:(NSError *)error
{
NSLog(#"Ad failed to load with error: %#", error);
}
For displaying the native ad cover image, it is recommended to use the Facebook Audience Network MediaView which can display both image and video assets.
Reference : https://developers.facebook.com/docs/audience-network/ios/native-api

How can I test my class which uses UIWebView

I want to make test, but I do not know how to make it (I can use GHUnit).
This is the class I want to test:
Communicator.h
#interface Communicator : NSObject
-(void)loadURLWithString:(NSString*)urlString withWebView:(UIWebView*)webView;
#end
Communicator.m
#import "Communicator.h"
#implementation Communicator
##### I WANT TO EXCLUDE THE WEBVIEW BELOW #####
-(void)loadURLWithString:(NSString*)urlString withWebView:(UIWebView*)webView
{
NSURL *url = [[NSURL alloc] initWithString:urlString];
NSURLRequest *r = [[NSURLRequest alloc] initWithURL:url];
[webView loadRequest:r];
}
#end
And this is ViewController which control Communicator class.
ViewController.h
#import "Communicator.h"
#interface ViewController : UIViewController <UITextFieldDelegate>
{
Communicator *communicator;
}
#property (weak, nonatomic) IBOutlet UIWebView *webView;
#property (weak, nonatomic) IBOutlet UIButton *startButton;
#end
ViewController.m
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
communicator = [[Communicator alloc] init];
[_startButton addTarget:self action:#selector(startButtonPushed) forControlEvents:UIControlEventTouchDown];
}
- (void)startButtonPushed
{
[communicator loadURLWithString:#"http://www.apple.co.jp" withWebView:_webView];
}
#end
Thank you for your help.
What is it exactly you want to test? That pressing the button causes the webview to load a URL or that the webview actually brings the URL content?

View Controller Trouble

I am making a music streaming app but i can only seem to add the controls to the default view controller this is my code for ViewController.h
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController {
IBOutlet UIWebView *WebView;
}
-(IBAction)Play:(id)sender;
#end
and this is my code for viewcontrol.m
-(IBAction)Play:(id)sender{
NSString *stream = #"http://117.53.175.113:15008/listen.pls";
NSURL *url = [NSURL URLWithString:stream];
NSURLRequest *urlrequest = [NSURLRequest requestWithURL:url];
[WebView loadRequest:urlrequest];
}
#end
Please help
Drag you IBOutlet from interface builder and to outside class parentheses {}.
Something like:
#interface ViewController : UIViewController {
IBOutlet UIWebView *WebView; // remove this.
}
#property (unsafe_unretained, nonatomic) IBOutlet UIWebView * WebView;

Resources