FBAudienceNetwork: setting FBNativeAd into FBMediaView stacks the UI - ios

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

Related

How to have a single IB Outlet for 2 UIViews

I'm currently developing an application that displays ads. The main view controller has 2 UIViews that act as containers for the ad views that will be passed in when they are loaded. However, I'm using an API that only takes a single "adContainer" as a parameter, so tried using an IBOutlet Collection called adContainer that has each of these 2 UIViews (adContainer1 and adContainer2) linked to it. I tagged each of these so that on the main view controller when I try to load an ad, I can iterate through the adContainers in the IBOutlet Collection and request each adContainer individually based on the object's tag. I'm being thrown this error:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIView countByEnumeratingWithState:objects:count:]: unrecognized selector sent to instance
Here is the code for my viewController.h :
#import <UIKit/UIKit.h>
#import "MPAdView.h"
#import "MPViewController.h"
#class MPAdInfo;
#interface MPSmaatoScrollBannerAdDetailViewController :
UIViewController <MPAdViewDelegate>
#property (weak, nonatomic) IBOutlet UILabel *titleLabel;
#property (weak, nonatomic) IBOutlet UILabel *IDLabel;
#property (weak, nonatomic) IBOutlet UILabel *failLabel;
#property (weak, nonatomic) IBOutlet UIActivityIndicatorView *spinner;
#property (strong, nonatomic) IBOutletCollection(UIView) NSArray
*adViewContainer;
- (id)initWithAdInfo:(MPAdInfo *)info;
#end
And here is the function in my viewController.m that I suspect is throwing the error:
- (void)configureAd
{
for (UIView *ad in _adViewContainer) {
if (ad.tag == 1) {
self.adView1 = [[MPSampleAppInstanceProvider sharedProvider] buildMPAdViewWithAdUnitID:self.info.ID
size:ad.bounds.size];
self.adView1.delegate = self;
self.adView1.accessibilityLabel = #"smaato banner";
self.adView1.autoresizingMask = UIViewAutoresizingFlexibleWidth;
[ad addSubview:self.adView1];
[self.adView1 stopAutomaticallyRefreshingContents];
} else if (ad.tag == 2) {
self.adView2 = [[MPSampleAppInstanceProvider sharedProvider] buildMPAdViewWithAdUnitID:self.info.ID
size:ad.bounds.size];
self.adView2.delegate = self;
self.adView2.accessibilityLabel = #"smaato banner";
self.adView2.autoresizingMask = UIViewAutoresizingFlexibleWidth;
[ad addSubview:self.adView2];
[self.adView2 stopAutomaticallyRefreshingContents];
}
}
}
Please help! Thank you in advance!
According to the way you said, I did not report an error myself; it may be that your xib cable is not set correctly.
You can refer to this:
"countByEnumeratingWithState:objects:count:" Error

Deallocating an object's properties in Objective-C

I currently have a header that displays a name, time, and a couple of buttons. This header should only appear if an appointment is selected in a dashboard, which is irrelevant here. However, once i logout and log back in, with no patient selected, the header view is displayed. I think this is because I did not deallocate the appointment object, and i'm not sure how to do that (i'm new to iOS programming).
Here's my code:
So I have the interface
#interface DashboardVC : CommonVC <UIActionSheetDelegate, HeaderViewDelegate, PracticeServiceDelegate> {
IBOutlet HeaderView *_headerView;
}
And inside the HeaderView object i have these properties:
#property (strong, nonatomic) CCAppointment *appointment;
#property (strong, nonatomic) IBOutlet UIButton *backButton;
#property (strong, nonatomic) IBOutlet UIView *currentPatientView;
#property (strong, nonatomic) IBOutlet UIImageView *avatarImageView;
#property (strong, nonatomic) IBOutlet UILabel *patientNameLabel;
I then, in dashboard VC, want to deallocate, but i'm not sure how... this is what i have:
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
How do I deallocate the properties so that the headerVIew does not show up?
if you want to destroy _headerView try:
[_headerView removeFromSuperview];
_headerView = nil;

Custom UIView with UILabel iOS

I have a UIView with two UILabel that I want to reuse in more than one UIViewController. I use storyboard and I assign a custom class alertView:UIView that I declared.
file AlertRemote.h
#interface AlertRemote: UIView
#property (weak, nonatomic) IBOutlet UILabel *label1;
#property (weak, nonatomic) IBOutlet UILabel *label2;
-(void) setTextLabel;
file AlertRemote.m
-(void) setTextLabel{
[ _label1 setText:#"attention..."];
[_label2 setText:#"the program..."];
}
file Controllo.m
//the view present in the storyboard alertView linked to the uiview
#property (strong, nonatomic)IBOutlet AlertRemote *alertView;
#property AlertRemote *alRemView;
[super viewDidLoad];
_alertView=_alRemView; [_alertView setTextLabel];
[_alertView setTextLabel];
if I put some breakpoints inside setTextLabel the code don't works
thanks!!
In order for the custom alert to work, you need to initialise it.
AlertRemote *alRemView;
alRemView = [[AlertRemote alloc]init];
[alRemView setTextLabel];
I think you are not initializing the property alertview. If you are initializing the property alertview then try to set directly with out using alremView.

IOS - ZBarSDK - application crashes with -[UILabel length]: unrecognized selector sent to instance

When i open directly my camera's view to the QRCode, it crashes. But when i do not open my camera's view directly at start to the QRCode, it works fine. i already tried the same code with an empty app and it works perfectly. it just occurs that situation. All IBOutlets and IBActions are working, Here is my code:
SerialController.h:
#import <UIKit/UIKit.h>
#import "ZBarSDK.h"
#interface SerialController : UIViewController <ZBarReaderDelegate>
#property (weak, nonatomic) IBOutlet UIActivityIndicatorView *spinner;
#property (weak, nonatomic) IBOutlet UILabel *description;
#property (weak, nonatomic) IBOutlet UIImageView *key;
#property (weak, nonatomic) IBOutlet UILabel *resultText;
#property (weak, nonatomic) IBOutlet UIActivityIndicatorView *qrSearchButton;
#property (weak, nonatomic) IBOutlet UIView *contentHolder;
#property (weak, nonatomic) IBOutlet UIImageView *turkcellLogo;
#property (weak, nonatomic) IBOutlet UIImageView *backgroundImage;
#end
SerialController.m
- (IBAction)handleQrSearchButtonTap:(id)sender {
ZBarReaderViewController *codeReader = [ZBarReaderViewController new];
codeReader.readerDelegate=self;
codeReader.supportedOrientationsMask = ZBarOrientationMaskAll;
ZBarImageScanner *scanner = codeReader.scanner;
[scanner setSymbology: ZBAR_I25 config: ZBAR_CFG_ENABLE to: 0];
[self presentViewController:codeReader animated:YES completion:nil];
}
#pragma mark - ZBar's Delegate method
- (void) imagePickerController: (UIImagePickerController*) reader didFinishPickingMediaWithInfo: (NSDictionary*) info
{
// get the decode results
id<NSFastEnumeration> results = [info objectForKey: ZBarReaderControllerResults];
ZBarSymbol *symbol = nil;
for(symbol in results)
// just grab the first barcode
break;
// showing the result on textview
self.resultText.text = symbol.data;
// dismiss the controller
[reader dismissViewControllerAnimated:YES completion:nil];
}
You don't want to use property called description. I just spent couple hours trying to fix exactly same problem. description is standard method in NSObject and you're hiding it. Rename it to something else.

Use Of Undeclared Identifier in my function

I'm having a use of undeclared identifier even though that I declared it in the .h and synthesize in the .m.
I had another problem before, but I posted a question in stack overflow and they said that I shouldn't extern them and when I extered them the code gave me an error "ld: symbol(s) not found for architecture i386
clang: error: linker command failed with exit code 1 (use -v to see invocation)" and know I didn't and it's giving me "Use Of Undeclared Identifier" as you can see
.h
#import <UIKit/UIKit.h>
BOOL OFrameIsHidden, XFrameIsHidden;
NSString *topOne, *topTwo, *topThree;
NSString *midOne, *midTwo, *midThree;
NSString *botOne, *botTwo, *botThree;
void hideAll(void);
#interface ViewController : UIViewController
void hideAll(void);
#property (weak, nonatomic) IBOutlet UIImageView *XFrame;
#property (weak, nonatomic) IBOutlet UIImageView *OFrame;
#property (weak, nonatomic) IBOutlet UIImageView *frame;
#property (weak, nonatomic) IBOutlet UILabel *X;
#property (weak, nonatomic) IBOutlet UILabel *O;
#property (weak, nonatomic) IBOutlet UILabel *WhoWon;
#property (weak, nonatomic) IBOutlet UIButton *oneOne;
#property (weak, nonatomic) IBOutlet UIButton *oneTwo;
#property (weak, nonatomic) IBOutlet UIButton *oneThree;
#property (weak, nonatomic) IBOutlet UIButton *twoOne;
#property (weak, nonatomic) IBOutlet UIButton *twoTwo;
#property (weak, nonatomic) IBOutlet UIButton *twoThree;
#property (weak, nonatomic) IBOutlet UIButton *threeOne;
#property (weak, nonatomic) IBOutlet UIButton *threeTwo;
#property (weak, nonatomic) IBOutlet UIButton *threeThree;
#end
.m
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
#synthesize XFrame, OFrame, frame, X, O, WhoWon;
#synthesize oneOne, oneTwo, oneThree;
#synthesize twoOne, twoTwo, twoThree;
#synthesize threeOne, threeTwo, threeThree;
void hideAll(void){
[OFrame setHidden:YES];
[XFrame setHidden:YES];
[frame setHidden:YES];
[X setHidden:YES];
[O setHidden:YES];
[oneOne setHidden:YES];
[oneTwo setHidden:YES];
[oneThree setHidden:YES];
[oneOne setHidden:YES];
[twoTwo setHidden:YES];
[twoThree setHidden:YES];
[threeOne setHidden:YES];
[threeTwo setHidden:YES];
[threeThree setHidden:YES];
}
For your information there is some more code for the IBAcions, but I don't want to make this long.
Don't mix functions and methods like this. It doesn't really help you and all you have done is create visibility issues.
Methods work by passing a hidden parameter which gives access to self. All of your #property definitions are instance variables, so you need access to self to get to them. Functions, like your hideAll, don't have this access so they can't get to the instance variables (they don't know what an instance is).
You generally also don't want variables defined outside the class definition.
Bring your variables into the class or move them to another more appropriate class and use methods for your code, not functions.

Resources