GADInterstitial custom ads not working - iphone

i have a problem in getting the GADInterstitial custom ads i have tried this code
if(nil != m_pBannerView)
{
m_pBannerView.delegate = nil;
[m_pBannerView release];
m_pBannerView = nil;
}
m_pBannerView = [[GADBannerView alloc] initWithAdSize:kGADAdSizeBanner];
m_pBannerView.delegate = self;
m_pBannerView.rootViewController = self;
m_pBannerView.adUnitID = #"AdMob Publisher ID";
m_pBannerView.rootViewController = self;
[self.view addSubview:m_pBannerView];
GADRequest *request = [GADRequest request];
request.testing = YES;
[m_pBannerView loadRequest:request];
if(nil != m_pInterstitial)
{
[m_pInterstitial release];
m_pInterstitial = nil;
}
m_pInterstitial = [[GADInterstitial alloc] init];
m_pInterstitial.delegate = self;
m_pInterstitial.adUnitID = #"INTERSTITIAL_AD_UNIT_ID";
GADRequest *interstialRequest = [GADRequest request];
interstialRequest.testing = YES;
[m_pInterstitial loadRequest: interstialRequest];
}
And in GADInterstitial Delegates i am calling [ad presentFromRootViewController:self];
but still i am not able to get the custom ads please help me.

You have to use your own unique id for adUnitID property

GADInterstitial is an interesting way to show ads in your Apps, and kind of a tricky one too. Following this example, lets pursue the following steps:
First we need to set up the environment for them to show at all.
Download the GoogleAdMobAdsSdkiOS, preferably the latest. Add
the SDK to your project, but do remember to delete the example
Projects in the AddOns folder in the SDK.
Next, add the following frameworks in your Project>>Build
Phases>>Link Binary With Libraries:
AdSupport.framework (select Optional if catering for < iOS7)
StoreKit.framework
CoreData.framework
CoreAudio.framework
AVFoundation.framework
MessageUI.framework
AudioTool.framework
libGoogleAdMobAds.a (placed in the SDK folder)
The basics are complete. Now we need to select the ViewController we wish to see our Ads in. So here's some code, here we import the header for GADInterstitialDelegate and extend it with our MainViewController.h:
#import "GADInterstitialDelegate.h"
#define kSampleAdUnitID #"/6253334/dfp_example_ad/interstitial"
#class GADInterstitial;
#class GADRequest;
#interface MainViewController : UIViewController<GADInterstitialDelegate>
{
BOOL isLoaded_;
NSTimer *quickie_;
}
#property(nonatomic, strong) GADInterstitial *interstitial;
//Make sure the delegate is handled properly.
Now we need to move to the implementation i.e. in MainViewController.m:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
// FOLLOW THE CODE BELOW FOR ADMOD INTERESTIAL IMPLEMENTATION
[self initializeAds];
quickie_ = [[NSTimer alloc] init];
quickie_ = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:#selector(showAdd) userInfo:nil repeats:YES];
}
-(void)showAdd
{
if(isLoaded_ && [quickie_ isValid])
{
[quickie_ invalidate];
quickie_ = nil;
[self.interstitial presentFromRootViewController:self];
}
}
- (GADRequest *)request
{
GADRequest *request = [GADRequest request];
return request;
}
-(void)initializeAds
{
// Create a new GADInterstitial each time. A GADInterstitial will only show one request in its
// lifetime. The property will release the old one and set the new one.
self.interstitial = [[GADInterstitial alloc] init];
self.interstitial.delegate = self;
// Note: kSampleAdUnitId is a staticApId for example purposes. For personal Ads update kSampleAdUnitId with your interstitial ad unit id.
self.interstitial.adUnitID = kSampleAdUnitID;
[self.interstitial loadRequest:[self request]];
}
- (void)viewWillLayoutSubviews
{
[super viewWillLayoutSubviews];
self.loadingSpinner.center = CGPointMake(CGRectGetWidth(self.view.bounds) / 2, self.loadingSpinner.center.y);
}
- (void)interstitialDidReceiveAd:(GADInterstitial *)interstitial
{
isLoaded_ = YES;
}
- (void)interstitial:(GADInterstitial *)interstitial didFailToReceiveAdWithError:(GADRequestError *)error
{
isLoaded_ = NO;
}
//----ADS IMPLEMENTATION TILL HERE--->//
The timer "quickie_" here constantly checks if the Ad has successfully loaded and when it does, it shoots the Ad on the ViewController if the user still is on it. The static kSampleAdUnitID is a sampleId that always works. Thats it. Run your code and find your Interstitial Ad on your ViewController of choice.
Hope I helped. Cheers! :)

Related

How to Use Speech-To-Text library in my Xcode project?

I want to use speech to text library in my Xcode project. I added successfully the 'Speech-To-Text' library to my Xcode project and i imported header files into my application. I use the following code but nothing will happen.I can't get any view.
#import "SineWaveViewController.h"
SineWaveViewController *sineWav = [[SineWaveViewController alloc]init];
[self.navigationController pushViewController:sineWav animated:YES];
- (id)init {
self = [super init];
if (self) {
NSArray *cmds = [NSArray arrayWithObjects:#"Sing", #"Jump", #"Roll over", nil];
recog = [[NSSpeechRecognizer alloc] init]; // recog is an ivar
[recog setCommands:cmds];
[recog setDelegate:self];
}
return self;
}

QR Reader for iPhone

So i need to write a QR reader for an iphone application. Usually frameworks like ZXING and ZBAr allow you to download an application and then read the barcode. I want to integrate it within the application itself. So basically i want to be able to tap a button and have it reading the QR code. Is this possible? If so are there any proper documentation on this? I tried using ZXing but when i link binaries libZXingWidget.a is unavailable. Also the documentation is not enough to know how to integrate it within the application. So let me know.
Here's code to setup ZBar, make sure to add the SDK to your project and link your library.
-
(void)viewDidLoad
{
[ZBarReaderView class];
readerView.readerDelegate = self;
readerView.tracksSymbols = NO;
//CHOOSE CAMERA
if (some setting isEqual to CameraRear) {
readerView.device = [self backFacingCameraIfAvailable];
}
else {
readerView.device = [self frontFacingCameraIfAvailable];
}
[self relocateReaderPopover:[self interfaceOrientation]];
[readerView start];
}
-(AVCaptureDevice *)frontFacingCameraIfAvailable
{
NSArray *videoDevices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo];
AVCaptureDevice *captureDevice = nil;
for (AVCaptureDevice *device in videoDevices)
{
if (device.position == AVCaptureDevicePositionFront)
{
captureDevice = device;
break;
}
}
// couldn't find one on the front, so just get the default video device.
if ( ! captureDevice)
{
captureDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
}
return captureDevice;
}
-(AVCaptureDevice *)backFacingCameraIfAvailable
{
NSArray *videoDevices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo];
AVCaptureDevice *captureDevice = nil;
for (AVCaptureDevice *device in videoDevices)
{
if (device.position == AVCaptureDevicePositionBack)
{
captureDevice = device;
break;
}
}
// couldn't find one on the front, so just get the default video device.
if ( ! captureDevice)
{
captureDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
}
return captureDevice;
}
-(void)relocateReaderPopover:(UIInterfaceOrientation)toInterfaceOrientation{
if (toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft) {
readerView.previewTransform = CGAffineTransformMakeRotation(M_PI_2);
} else if (toInterfaceOrientation == UIInterfaceOrientationLandscapeRight) {
readerView.previewTransform = CGAffineTransformMakeRotation(-M_PI_2);
} else if (toInterfaceOrientation== UIInterfaceOrientationPortraitUpsideDown) {
readerView.previewTransform = CGAffineTransformMakeRotation(M_PI);
} else {
readerView.previewTransform = CGAffineTransformIdentity;
}
}
This is a step-by-step tutorial on how to add an integrated QR code reader to your iphone app. Look at this example ZXing project if you need to know how to use it.
I have managed to integrate ZXING into my test project. I have done this a little time ago so I might not remember all the problems that I faced and solved.
Into my workspace folder I have copied ZXing-2.0 folder. Inside I have only left folders: cpp, docs and iphone. To my workspace (containing the test project) I have added ZXingWidget project from the folder ZXing-2.0. This allowed me to add to linking setting libZXingWidget.a.
When I build my test project, XCode detects dependency and builds the widget first and then builds the test project and links it against libZXingWidget.a.
Here's a simple view controller that I have implemented to display inside camera view able to detect QR code.
//
// MyVC.m
//
//
#import "MyVC.h"
#import "QRCodeReader.h"
#interface MyVC () {
ZXingWidgetController *_widController;
}
#end
#implementation MyVC
#synthesize labelResultString;
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
_widController = [[ZXingWidgetController alloc] initWithDelegate:(id<ZXingDelegate>)self showCancel:YES OneDMode:NO];
QRCodeReader* qrcodeReader = [[QRCodeReader alloc] init];
NSSet *readers = [[NSSet alloc ] initWithObjects:qrcodeReader,nil];
//[qrcodeReader release];
_widController.readers = readers;
//[readers release];
//NSBundle *mainBundle = [NSBundle mainBundle];
//_widController.soundToPlay = [NSURL fileURLWithPath:[mainBundle pathForResource:#"beep-beep" ofType:#"aiff"] isDirectory:NO];
_widController.overlayView.displayedMessage = #"";
_widController.wantsFullScreenLayout = NO;
//[self presentModalViewController:_widController animated:NO];
_widController.view.frame = CGRectMake(10, 10, 300, 300);//self.view.frame;
_widController.view.autoresizingMask = UIViewAutoresizingNone;
_widController.overlayView.frame = CGRectMake(0, 0, 300, 300);
_widController.overlayView.cropRect = CGRectMake(20, 20, 260, 260);
[self.view addSubview:_widController.view];
//[_widController release];
}
- (void)viewDidUnload {
[super viewDidUnload];
self.labelResultString = nil;
}
- (void)dealloc {
self.labelResultString = nil;
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[_widController viewWillAppear:animated];
}
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
[_widController viewDidAppear:animated];
[[UIApplication sharedApplication] setStatusBarHidden:NO];
}
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
[_widController viewWillDisappear:animated];
}
- (void)viewDidDisappear:(BOOL)animated {
[super viewDidDisappear:animated];
[_widController viewDidDisappear:animated];
}
#pragma mark - ZXingDelegateMethods
- (void)zxingController:(ZXingWidgetController*)controller didScanResult:(NSString *)result {
self.labelResultString.text = result;
}
- (void)zxingControllerDidCancel:(ZXingWidgetController *)controller {
[self dismissModalViewControllerAnimated:YES];
}
#end
Once you have ZXing set up correctly the code to get a QR reader is literally this easy:
Make sure to import these two guys for QR:
#import "ZXingWidgetController.h"
#import "QRCodeReader.h"
Then in your controller you will set up the code reader as follows:
ZXingWidgetController *widController = [[ZXingWidgetController alloc] initWithDelegate:self showCancel:YES OneDMode:NO];
QRCodeReader *qrcodeReader = [[QRCodeReader alloc] init];
NSSet *readers = [[NSSet alloc] initWithObjects:qrcodeReader,nil];
widController.readers = readers;
[self presentModalViewController:widController animated:YES];
You basically set up the controller that handles the code reading (ZXingWidgetController) and then give it a set of all the types of code readers you want (here I just used QRCodeReader.) Lastly, you just present it as a modal view controller.
Then you will need to implement the <ZXingDelegate> and the following delegate functions:
- (void)zxingController:(ZXingWidgetController*)controller didScanResult:(NSString *)result
{
[self dismissModalViewControllerAnimated:YES];
NSLog(#"%#",result); //Simple NSString result.
}
- (void)zxingControllerDidCancel:(ZXingWidgetController*)controller
{
[self dismissModalViewControllerAnimated:YES];
NSLog(#"Cancelled");
}
The hardest part is just setting it up. I found this tutorial to be the most helpful (Sorry that I am using a link): How to install ZXing in Xcode 4 The comments are also helpful if you have any problems.

Edit and Delete Existing EKEvent?

I am using the Kal calendar in my app, (hopefully that doesn't change too much) but I get an EKEvent object from that depending on the user selection on the calendar.
Anyway, how can I edit and delete an event which already exists? Namely the EKEvent that I receive?
I need to do this all programatically, I am not using any of Apple's pre-made EKEventViewController's.
I can successfully create and save new events, but Im unsure of how to edit or delete existing ones, any help would be appreciated, thanks.
A complete answer would almost require a demo project.
Other approach would be simply giving you a link to Event Kit Programming Guide.
Since you did not provide any code (what you have tried already) i hope this extract from a working app will push you to the right track.
Note that i sublassed EKEventViewController due to app's specifics - this is not neccessary. I had to sublass it simply because original EKEventViewController
didn't spawn with black navigationBar (this was reported as a bug also, don't now if it's
fixed already).
You know how to add an event to calendar, so there's no need to write about getting a reference to EKEventStore and EKCalendar.
You're also not asking about how to retreive an event from calendar so let's assume you already have some kind of mechanism to select (receive) the event and you want to edit it. Let's say it is:
EKEvent *selectedEvent = (EKEvent *)[events objectAtIndex: selectedIndex];
I create this viewController as a property of appDelegate - you probably have better solution. appDelegate also holds eventStore and defaultCalendar reference - your approach could differ.
appDelegate.detailViewController = [[MPEventViewController alloc] initWithNibName:nil bundle:nil];
appDelegate.detailViewController.event = selectedEvent;
appDelegate.detailViewController.eventStore = appDelegate.eventStore;
appDelegate.detailViewController.defaultCalendar = appDelegate.defaultCalendar;
appDelegate.detailViewController.allowsEditing = NO;
[appDelegate.navigationController pushViewController:appDelegate.detailViewController animated:YES];
Sublcassing (again, this is not neccessary but it might come useful) goes like this:
MPEventEditViewController.h
#import <Foundation/Foundation.h>
#import <EventKitUI/EventKitUI.h>
#interface MPEventViewController : EKEventViewController <EKEventEditViewDelegate>
#property (nonatomic, strong) EKEventStore *eventStore;
#property (nonatomic, strong) EKCalendar *defaultCalendar;
- (void)editEvent:(id)sender;
#end
MPEventEditViewController.m
#import "MPEventViewController.h"
#import "----------AppDelegate.h"
#implementation MPEventViewController
#synthesize eventStore;
#synthesize defaultCalendar;
- (void)viewDidLoad {
[super viewDidLoad];
[self setTitle: [self.event title]];
self.allowsEditing = NO;
self.navigationController.navigationBar.barStyle = UIBarStyleBlack;
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:
UIBarButtonSystemItemEdit target:self action:#selector(editEvent:)];
//this has nothing to do with the answer :)
//[[self.navigationController navigationBar] setTintColor: [UIColor colorWithHexString: NAVBAR_TINT_COLOR]];
}
- (void)editEvent:(id)sender {
EKEventEditViewController *addController = [[EKEventEditViewController alloc] initWithNibName:nil bundle:nil];
//this has nothing to do with the answer :)
//[addController.navigationBar setTintColor: [UIColor colorWithHexString: NAVBAR_TINT_COLOR]];
addController.eventStore = self.eventStore;
addController.event = self.event;
addController.navigationBar.barStyle = UIBarStyleBlack;
addController.editViewDelegate = self;
[self presentModalViewController:addController animated:YES];
}
- (void)eventEditViewController:(EKEventEditViewController *)controller
didCompleteWithAction:(EKEventEditViewAction)action {
NSError *error = nil;
EKEvent *thisEvent = controller.event;
switch (action) {
case EKEventEditViewActionCanceled:
break;
case EKEventEditViewActionSaved:
[controller.eventStore saveEvent:controller.event span: EKSpanFutureEvents error:&error];
break;
case EKEventEditViewActionDeleted:
[controller.eventStore removeEvent:thisEvent span: EKSpanFutureEvents error:&error];
break;
default:
break;
}
//here would be the place to reload data if you hold it in some kind of UITableView
[controller dismissModalViewControllerAnimated:YES];
}
- (EKCalendar *)eventEditViewControllerDefaultCalendarForNewEvents:(EKEventEditViewController *)controller {
EKCalendar *calendarForEdit = self.defaultCalendar;
return calendarForEdit;
}
- (void)dealloc {
eventStore = nil;
defaultCalendar = nil;
}
#end
Only after writing all this i remembered there is a great sample code SimpleEKDemo. In fact some of this posted code is probably originates from there.
EDIT:
After the question was edited the above answer became off-topic.
In that case you should take a look at EKCalendarItem and EKevent.
You can change allmost all properties programatically (most of them are inherited from EKCalendarItem).
Maybe you were distracted for example becaues hasNotes is readonly. That is because hasNotes is kind of a function and not a real property. Properties like notes, atendees, startDate, endDate etc. are perfectly editable.
For saving modified event you can still use:
NSError error = nil;
[self.eventStore saveEvent:event span: EKSpanFutureEvents error:&error];
And for deleting it:
NSError error = nil;
[self.eventStore removeEvent:event span: EKSpanFutureEvents error:&error];
EDIT2: for deleting all events try this:
//assuming self.eventStore is already properly set in code
//identifierArray would be your NSMutableArray holding event identifiers
//change the name according to your code
NSError error = nil;
for (NSString *eventIdentifier in removeAllObjects) {
EKEvent *event = [self.eventStore eventWithIdentifier:eventIdentifier];
[self.eventStore removeEvent:event span:EKSpanFutureEvents error:&error];
}
//now you can also clear identifiers
[removeAllObjects removeAllObjects];
Note: there's no guarantee you'll be able to delete all events - only events from
default calendar which is set by usert in settings app.

Intercept URL and opening URL in Safari

I have intercepted URL opening by doing the following:
- (BOOL)openURL:(NSURL *)url{
URLViewController * web = [[URLViewController alloc] init];
web.url = url;
UINavigationController * nav = [[UINavigationController alloc] initWithRootViewController:web];
[nav.navigationBar setTintColor:[UIColor blackColor]];
[nav setModalPresentationStyle:UIModalPresentationFormSheet];
[self.detailViewController presentModalViewController:nav animated:NO];
[web release];
[nav release];
return YES;
}
I have a UITextView in which detects URL and when clicking on the URL it opens up the link in a ModalViewController. Full detail on what's going on can be seen here. Now the issue is, what if I want to open a URL in safari, is it still possible?
You should add an override flag indicating whether you want to exercise control or not.
#interface MyApplication : UIApplication {
}
-(BOOL)openURL:(NSURL *)url withOverride:(BOOL)override;
#end
#implementation MyApplication
-(BOOL)openURL:(NSURL *)url withOverride:(BOOL)override {
if ( !override ) {
return [super openURL:url];
}
if ([self.delegate openURL:url]) {
return YES;
} else {
return [super openURL:url];
}
}
-(BOOL)openURL:(NSURL *)url{
return [self openURL:url withOverride:YES];
}
#end
So now all calls that you want to bypass can be sent like this.
[[MyApplication sharedApplication] openURL:url withOverride:NO];
Original Answer
This is what you should do. Put it before the return YES; statement.
if ( [super canOpenURL:aURL] ) {
return [super openURL:aURL];
}

How to post tweet from iPhone without PIN using SA_OAuthTwitterEngine?

I am developing a iPhone application which sends tweets to twitter. For this I am using SA_OAuthTwitterEngine + MGTwitterEngine classes.
I register applicaiton to www.twitter.com/apps and pass Consumer key and Consumer secret to controller my code is this.
if(!_engine){
_engine = [[SA_OAuthTwitterEngine alloc] initOAuthWithDelegate:self];
_engine.consumerKey = slPcFUjUh5y1hex0zvEhPg;
_engine.consumerSecret = u6ydovMdP9yeiVqDukVhIzZPgJR9XDPUwfxymzNs;
}
UIViewController *controller = [SA_OAuthTwitterController controllerToEnterCredentialsWithTwitterEngine:_engine delegate:self];
if (controller){
[self presentModalViewController: controller animated: YES];
intTwitterFlag = 1;
}
Previously on twitter.com/apps I select Application type = client and my application will generate PIN and accessToken. But when i change my Application type = Browser it cannot generate PIN and accessToken.
Previously when application type is client i am giving user name and password and then control return to my application from the webview but now after entering user name and password it cannot dismissModalViewController but showing Select and Copy the PIN.
Thank you for your time and any help you can give me!
Here it is:just replace the method in SA_OAuthTwitterController.m:
- (void) webViewDidFinishLoad: (UIWebView *) webView {
_loading = NO;
//[self performInjection];
if (_firstLoad) {
[_webView performSelector: #selector(stringByEvaluatingJavaScriptFromString:) withObject: #"window.scrollBy(0,200)" afterDelay: 0];
_firstLoad = NO;
} else {
/*
NSString *authPin = [self locateAuthPinInWebView: webView];
NSLog(#"authPin: %#", authPin);
if (authPin.length) {
[self gotPin: authPin];
return;
}
NSString *formCount = [webView stringByEvaluatingJavaScriptFromString: #"document.forms.length"];
if ([formCount isEqualToString: #"0"]) {
[self showPinCopyPrompt];
}*/
//*****************************************************
// This is to bypass the pin requirement
// in case the call back URL is set in Twitter settings
//*****************************************************
[_engine requestAccessToken];
if ([_delegate respondsToSelector: #selector(OAuthTwitterController:authenticatedWithUsername:)])
{
[_delegate OAuthTwitterController: self authenticatedWithUsername: _engine.username];
}
[self performSelector: #selector(dismissModalViewControllerAnimated:) withObject: (id) kCFBooleanTrue afterDelay: 1.0];
//[self dismissModalViewControllerAnimated:YES];
}
[UIView beginAnimations: nil context: nil];
_blockerView.alpha = 0.0;
[UIView commitAnimations];
if ([_webView isLoading]) {
_webView.alpha = 0.0;
} else {
_webView.alpha = 1.0;
}
}
I too faced the same issue, and i just removed callback url from the twitter app settings. For my surprise login proceeds without any issue.
The replacement for this method
(void) webViewDidFinishLoad: (UIWebView *) webView
in the class SA_OAuthTwitterController.m works well.
Its better to use xAuth route for mobile apps
http://dev.twitter.com/pages/xauth
check XAuthTwitterEngine which implements xauth for the MGTwitterEngine