How to toggle admob view from MainViewController using Cordova - iphone

I have implemented admob in my iPhone app, but the view which was created should be toggled based on my javascript's condition. So, i need to toggle that view using cordova plugins. Is there any possibility of toggling the admob view using phonegap?

I'm going to assume that by toggle you mean you want to hide the view. You could also mean you want to request a new ad but regardless I think the logic would be the same.
If you've set up your AdMob code as a plugin, you can write some js that calls into that (you might be able to do this even if you haven't). So the javascript method might look like:
AdMob.prototype.hideAd =
function(options, successCallback, failureCallback) {
var defaults = {
'isHidden': false
};
for (var key in defaults) {
if (typeof options[key] !== 'undefined') {
defaults[key] = options[key];
}
}
cordova.exec(
successCallback,
failureCallback,
'AdMobPlugin',
'hideAd',
new Array(defaults)
);
};
Then in your native code that handles the AdMob view, you can do something like this:
- (void)hidAd:(NSMutableArray *)arguments
withDict:(NSMutableDictionary *)options {
CDVPluginResult *pluginResult;
NSString *callbackId = [arguments pop];
if (!self.bannerView) {
// Try to prevent requestAd from being called without createBannerView first
// being called.
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR
messageAsString:#"AdMobPlugin:"
#"No ad view exists"];
[self writeJavascript:[pluginResult toErrorCallbackString:callbackId]];
return;
}
BOOL isHidden = (BOOL)[[options objectForKey:#"isHidden"] boolValue];
self.bannerView.hidden = isHidden;
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
[self writeJavascript:[pluginResult toSuccessCallbackString:callbackId]];
}

Related

How to handle silent push notification in ionic cordova application?

I'm following ionic documentation to create ionic push notifications. This works fine when the app is in active state. I need to run a function when the app receive a push notification while the app is in background.
$ionicPush.register({
canShowAlert: false, //Should new pushes show an alert on your screen?
canSetBadge: true, //Should new pushes be allowed to update app icon badges?
canPlaySound: false, //Should notifications be allowed to play a sound?
canRunActionsOnWake: true, // Whether to run auto actions outside the app,
onNotification: function(notification) {
// Called for each notification.
}
});
The issue i'm facing is onNotification callback function does not firing when the app is in background. How do I achieve that using ionic push notification API?
In this case, onNotification will be only fired when you click on notification in tray, which open applications. If notification is in android tray, it means notification has not been seen, because of which it will never reach application unless you click on it.
I suspect this patch may solve your problem:
https://github.com/phonegap-build/PushPlugin/issues/288
(quoted here in case it disappears)
We have managed to get this plugin to respond to silent push notifications which in turn call javascript functions, all while running in background mode. I thought we would share our solution as it seems like a lot of people are wanting this feature.
In AppDelegate+notification.m, change didReceiveRemoteNotification:
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))handler
{
NSLog(#"didReceiveNotification");
// Get application state for iOS4.x+ devices, otherwise assume active
UIApplicationState appState = UIApplicationStateActive;
if ([application respondsToSelector:#selector(applicationState)]) {
appState = application.applicationState;
}
PushPlugin *pushHandler = [self getCommandInstance:#"PushPlugin"];
pushHandler.notificationMessage = userInfo;
if (appState == UIApplicationStateActive)
pushHandler.isInline = YES;
else
pushHandler.isInline = NO;
[pushHandler notificationReceived];
handler(UIBackgroundFetchResultNewData);
}
In Pushplugin.m, change notificationReceived:
- (void)notificationReceived {
NSLog(#"Notification received");
if (notificationMessage && self.callback)
{
NSMutableString *jsonStr = [NSMutableString stringWithString:#"{"];
[self parseDictionary:notificationMessage intoJSON:jsonStr];
if (isInline)
{
[jsonStr appendFormat:#"foreground:\"%d\"", 1];
isInline = NO;
}
else
[jsonStr appendFormat:#"foreground:\"%d\"", 0];
[jsonStr appendString:#"}"];
NSLog(#"Msg: %#", jsonStr);
NSString * jsCallBack = [NSString stringWithFormat:#"%#(%#);", self.callback, jsonStr];
[self.webView stringByEvaluatingJavaScriptFromString:jsCallBack];
//get javascript function to fire in background mode
CDVPluginResult *commandResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:jsonStr];
[self.commandDelegate sendPluginResult:commandResult callbackId:self.callbackId];
self.notificationMessage = nil;
}
}
Assuming you are willing to adapt it to work with the
current non-deprecated plugin:
https://github.com/phonegap/phonegap-plugin-push

CocoaLibSpotify Create playlists ending

I'm creating an IOS App based on cocoaLibSpotify.
In some point, I'm creating a Spotify playlist from an array of songs. I have previously obtained all the Spotify URIs and then I create the playlist and add all the tracks one by one.
The code below is in the logic class of the app and then I have a controller to show the results.
The problem is that I call this logic from the controller but I dont know the way to get back when the adding process has finished. I have tried to implement a delegate but im not sure how to do it...
Which is the right way to add tracks to a playlist? I have been searching on the documentation and in the GitHub repository but I have only found an example with two nested track addings... :S
Thanks in advance! (and sorry for my english)
- (void) createPlaylist:(NSArray*)spotifyURIs withName:(NSString*)name {
int songsRead = 0;
[container createPlaylistWithName:name callback:^(SPPlaylist *createdPlaylist) {
[SPAsyncLoading waitUntilLoaded:createdPlaylist timeout:kSPAsyncLoadingDefaultTimeout then:^(NSArray *loadedPlaylist, NSArray *notLoadedPlaylist) {
for (int i=[spotifyURIs count]-1; i>=0; i--) {
NSString *trackURI = spotifyURIs[i];
if (trackURI != nil){
[[SPSession sharedSession] trackForURL:[NSURL URLWithString:trackURI] callback:^(SPTrack *track) {
if (track != nil) {
[createdPlaylist addItems:[NSArray arrayWithObject: track] atIndex:[[createdPlaylist items] count] callback:nil];
}
}];
}
songsRead++;
// If I have read the whole tracklist, end of the process, returning to controller...
if (songsRead == [spotifyURIs count]){
// ...
}
}
}];
}];
}
What you need is to use the KVO system on your new playlist or using BLOCKS
you can implement something like this
typedef void (^spotifycompletionWithData)(id data);
-(void)addTrack:(NSString *)trackURI withCompletionBlock:(spotifycompletionWithData)CompletionBlock;
then whenever you call the function you should return the loaded SPTRACK within the completion block i hope its helping

UIWebView shouldStartLoadWithRequest only fires once when calling a modal view from inside

I have part of my app written in JS and running inside of a WebView. I'm using the UIWebView shouldStartLoadWithRequest method to capture http requests as a means of communicating between JS and obj-c. This works great until I attempt to load a Modal View Controller over my webview from inside the shouldStartLoadWithRequest method. Once this happens, shouldStartLoadWithRequest is no longer called. Sometimes I need to dismiss this modal view controller and go back to the webview and do some things and then re-present the modal controller. The modal controller comes up the first time just fine, then I dismiss it and attempt to present it again by navigating to a URL from javascript and it no longer will present itself. NSLogs inside shouldStartLoadWithRequest are never run.
In my javascript I do something like this:
window.location='myapp:whateverMethod';
objective c code looks like this:
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
NSString *requestString = [[request URL] absoluteString];
NSLog(#"REQUEST URL: %#", requestString);
if([requestString hasPrefix:#"myapp:"]) {
NSArray *components = [requestString componentsSeparatedByString:#":"];
NSString *function = [components objectAtIndex:1];
if([self respondsToSelector:NSSelectorFromString(function)]) {
[self performSelector:NSSelectorFromString(function)];
}
return NO;
}
return YES;
}
-(void) whateverMethod {
NSLog(#"whateverMethod called!");
// This is a quick way to grab my view controller from the storyboard, so assume it exists
UIViewController *splash = [self.storyboard instantiateViewControllerWithIdentifier:#"splashViewController"];
[self presentModalViewController:splash animated:NO];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 2 * NSEC_PER_SEC), dispatch_get_current_queue(), ^{
[self dismissModalViewController:splash animated:NO];
});
}
At this point my webview is still visible. I navigate from page to page in my webapp and all javascript works great in it but the "shouldStartLoadWithRequest" delegate method of the webview no longer is called. I cannot figure out why. Does anyone have any ideas?
I noticed that Cordova doesn't set the window.location property. Instead it has two options: it either creates an iframe and sets the src of the iframe to that url, or it creates an XMLHttpRequest object e.g. in the iOSExec() function:
if (bridgeMode) {
execXhr = execXhr || new XMLHttpRequest();
// Changeing this to a GET will make the XHR reach the URIProtocol on 4.2.
// For some reason it still doesn't work though...
execXhr.open('HEAD', "file:///!gap_exec", true);
execXhr.setRequestHeader('vc', cordova.iOSVCAddr);
if (shouldBundleCommandJson()) {
execXhr.setRequestHeader('cmds', nativecomm());
}
execXhr.send(null);
} else {
execIframe = execIframe || createExecIframe();
execIframe.src = "gap://ready";
}
That being said, it may be beneficial to use something like Cordova instead of trying to roll it yourself (even if it's just embedding their view controller), since they handle a lot of the headaches that come up with webview delegates.
I've just had the same problem, but related to using a href="#" anchor.
This Stack Overflow answer sorted it
There are more answers on that thread that deal with widow.location, so you may have luck with them.
Checked out Cordova and they have their own queuing system, not really a help. But...
Disobedient Media's answer gave me an idea. Instead of window.location, why not try window.location.hash.
Now some JS code for logging is:
function sendMsgToNative(msg)
{
window.location.hash = '~cmd~' + msg;
}
console.log = function (msg) { sendMsgToNative('log~js ' + msg); };
and the Objective-C code is:
NSString *req = [request.URL absoluteString];
NSArray *components = [req componentsSeparatedByString:#"~"];
// Check for your protocol
if ([components count] > 1 && [(NSString *)[components objectAtIndex:1] isEqualToString:#"cmd"])
{
// Look for specific actions
if ([(NSString *)[components objectAtIndex:2] isEqualToString:#"log"])
{
NSString *logStr = [(NSString *)[components objectAtIndex:3] stringByReplacingPercentEscapesUsingEncoding: NSUTF8StringEncoding];
LOGI("%#", logStr);
}
}
You get the full URL including 'http:...' so I chose tilde instead of colon, and incremented the indices.
Now you can log all willy-nilly and send whatever amount of commands you want and they will all get through :-)
I (embarrassingly) spent a couple of hours working on this today, and realised that in my viewDidDisappear: I was setting the UIWebViewDelegate to nil!
All I needed to do to fix was once the modal was dismissed, re-set the UIWebViewDelegate and everything worked again.

Programmatically show soft keyboard on iPhone in a PhoneGap application?

I've been searching far and long, and to this moment, I did not come across a working solution for PhoneGap / Cordova applications that would show soft keyboard programmatically.
Scenario:
We have a PhoneGap application - a website created in jQuery Mobile - that at one point shows a dialog to the user. This dialog is also a web page and has one single INPUT text box where user should enter a code.
Problem:
When the code dialog is shown, the input box is focused using JavaScript. However, due to restrictions placed on iPhone's internal browser, the soft keyboard does not come up until the user actually really clicks inside the input text box.
What we tried:
creating a hidden text box and making it first responder
making the actual webview a first responder once the input receives focus via JavaScript
using sendActionsForControlEvents to try and delive Touch events to the webview (although if anyone has a working code for a PhoneGap application, I would appreciate if they could share it, since I'm no professional in iOS coding)
Any ideas?
EDIT: The restriction mentioned in this question is for built-in browsers only... if you're aiming Opera, you will be successful by using the following code:
var e = jQuery.Event("keydown", { keyCode: 37 });
$('#element').focus().trigger(e);
EDIT2: This is a final working PhoneGap code that can be used in a plugin:
keyboardhelper.h
//
// keyboardHelper.h
// soft keyboard displaying plugin for PhoneGap
//
// Copyright 2012 Martin Ambrus.
//
#import <Foundation/Foundation.h>
#ifdef CORDOVA_FRAMEWORK
#import <Cordova/CDVPlugin.h>
#else
#import "CDVPlugin.h"
#endif
#interface keyboardHelper : CDVPlugin {
NSString *callbackID;
}
#property (nonatomic, copy) NSString *callbackID;
- (void)showKeyboard:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options;
#end
keyboardhelper.m
//
// keyboardHelper.m
// soft keyboard displaying plugin for PhoneGap
//
// Copyright 2012 Martin Ambrus.
//
#import "keyboardHelper.h"
#import "AppDelegate.h"
#implementation keyboardHelper
#synthesize callbackID;
-(void)showKeyboard:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options {
self.callbackID = [arguments pop];
//Get text field coordinate from webview. - You should do this after the webview gets loaded
//myCustomDiv is a div in the html that contains the textField.
int textFieldContainerHeightOutput = [[((AppDelegate *)[[UIApplication sharedApplication] delegate]).viewController.webView stringByEvaluatingJavaScriptFromString:#"document.getElementById(\"myCustomDiv\").offsetHeight;"] intValue];
int textFieldContainerWidthOutput = [[((AppDelegate *)[[UIApplication sharedApplication] delegate]).viewController.webView stringByEvaluatingJavaScriptFromString:#"document.getElementById(\"myCustomDiv\").offsetWidth;"] intValue];
int textFieldContainerYOffset = [[((AppDelegate *)[[UIApplication sharedApplication] delegate]).viewController.webView stringByEvaluatingJavaScriptFromString:#"document.getElementById(\"myCustomDiv\").offsetTop;"] intValue];
int textFieldContainerXOffset = [[((AppDelegate *)[[UIApplication sharedApplication] delegate]).viewController.webView stringByEvaluatingJavaScriptFromString:#"document.getElementById(\"myCustomDiv\").offsetLeft;"] intValue];
UITextField *myTextField = [[UITextField alloc] initWithFrame: CGRectMake(textFieldContainerXOffset, textFieldContainerYOffset, textFieldContainerWidthOutput, textFieldContainerHeightOutput)];
[((AppDelegate *)[[UIApplication sharedApplication] delegate]).viewController.webView addSubview:myTextField];
myTextField.delegate = self;
CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString: #"ok"];
[self writeJavascript:[pluginResult toSuccessCallbackString:self.callbackID]];
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
//here you create your request to the server
return NO;
}
-(BOOL)textFieldDidEndEditing:(UITextField *)textField
{
//here you create your request to the server
return NO;
}
#end
javascript
var keyboardHelper = {
showKeyboard: function(types, success, fail) {
return Cordova.exec(success, fail, "keyboardHelper", "showKeyboard", types);
}
};
You can solve the issue with a config.xml entry these days, add:
<preference name="keyboardDisplayRequiresUserAction" value="false" />
You can get the coordinates for the input field using javascript on the webView. Then, place your own textField right on top of it and in it's delegate method textFieldShouldReturn send a request to the server with the code the user typed in.
//Get text field coordinate from webview. - You should do this after the webview gets loaded
//myCustomDiv is a div in the html that contains the textField.
int textFieldContainerHeightOutput = [[webView stringByEvaluatingJavaScriptFromString:#"document.getElementById(\"myCustomDiv\").offsetHeight;"] intValue];
int textFieldContainerWidthOutput = [[webView stringByEvaluatingJavaScriptFromString:#"document.getElementById(\"myCustomDiv\").offsetWidth;"] intValue];
int textFieldContainerYOffset = [[webView stringByEvaluatingJavaScriptFromString:#"document.getElementById(\"myCustomDiv\").offsetTop;"] intValue];
int textFieldContainerXOffset = [[webView stringByEvaluatingJavaScriptFromString:#"document.getElementById(\"myCustomDiv\").offsetLeft;"] intValue];
myTextField.frame = CGRectMake(textFieldContainerXOffset, textFieldContainerYOffset, textFieldContainerWidthOutput, textFieldContainerHeightOutput);
[webView addSubview:myTextField];
myTextField.delegate = self;
Then you implement textFieldShouldReturn and create your request to the server there.
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
//here you create your request to the server
return NO;
}
This is done in existing project, however without using PhoneGap. I hope you can adapt it to suit your needs.
To remove the text field, you can hide it
myTextField.hidden = YES;
or
myTextField = nil;
or
[myTextField removeFromSuperView];
Prior to iOS 6, Apple only allowed the keyboard to be brought up following a user interaction. In iOS 6 they've introduced the following property for UIWebView which you merely need to set to NO.
"yourWebView".keyboardDisplayRequiresUserAction = NO;
Apple sets this by default to "Yes". Now you can call focus() in your JS and the keyboard will appear.
Have you tried using Native Controls and calling them from Javascript?
Here you have some code that shows the usage of Native Controls on a Phonegap Cordova application (Phonegap 1.5)
https://gist.github.com/1384250
Hope it helps to solve the problem :)
I admit this is private, but it might help you out:
#class UIKeyboard;
void showKeyboard()
{
static UIKeyboard *kb = nil;
if ([[UIKeyboard class] respondsToSelector:#selector(automaticKeyboard)])
kb = [UIKeyboard automaticKeyboard];
else
kb = [UIKeyboard activeKeyboard];
if (kb == nil) {
kb = [[[UIKeyboard alloc] initWithDefaultSize] autorelease];
[[[UIApplication sharedApplication] keyWindow] addSubview:kb];
}
if ([kb respondsToSelector:#selector(orderInWithAnimation:)]) {
[kb orderInWithAnimation:YES];
} else {
[kb activate];
[kb minimize];
[kb maximize];
}
}
And call it like this:
showKeyboard();
I actually just found a solution to this.
Like horak says and as described in this article, with or without soft keyboard, it's now possible to achieve this with iOS 6.0 by using: KeyboardDisplayRequiresUserAction = NO;
As of Cordova 2.2, the iOS property mentioned above can simply be added to the Cordova.plist file:
KeyboardDisplayRequiresUserAction, boolean, NO
This solves it all for everyone using Cordova 2.2. Now just call input.focus() as previously discussed, and the keyboard will automatically appear. For those of us who haven't yet updated our Cordova to the current latest version (2.2), it's fortunately still possible.
Programmatically show keyboard on iPhone using cordova v. < 2.2
Step 1:
Adding property to Cordova.plist
Go to your project > Resources > Cordova.plist. Add: KeyboardDisplayRequiresUserAction, boolean, NO
Step 2:
Adding below code snippet to CDVViewController.m
Search for "#interface CDVViewController" (or similar to locate above file). For Cordova 2.1, go to line 240 (everyone else go to a line after a "IsAtLeastiOSVersion" if statement, sorry can't be more precise than that.) Add this code snippet to your CDVViewController.m on the above mentioned line:
if (IsAtLeastiOSVersion(#"6.0")) {
BOOL keyboardDisplayRequiresUserAction = YES; // KeyboardDisplayRequiresUserAction - defaults to YES
if ([self.settings objectForKey:#"KeyboardDisplayRequiresUserAction"] != nil) {
if ([self.settings objectForKey:#"KeyboardDisplayRequiresUserAction"]) {
keyboardDisplayRequiresUserAction = [(NSNumber*)[self.settings objectForKey:#"KeyboardDisplayRequiresUserAction"] boolValue]; //JB-VAL121212
}
}
// property check for compiling under iOS < 6
if ([self.webView respondsToSelector:#selector(setKeyboardDisplayRequiresUserAction:)]) {
[self.webView setValue:[NSNumber numberWithBool:keyboardDisplayRequiresUserAction] forKey:#"keyboardDisplayRequiresUserAction"];
}
}
Disclaimer: This has been tested on Cordova 2.1 only, it is, however, very likely that it still works with 2.0 or any other earlier version that comes with the CDVViewController.m file)
You can use the FocusOut event on the input field and this will be fired when the Done key is pressed. I could use this on IOS 6 and above. I believe it will also work on previous versions.

UISwitch invokes a Restful API call which fails, would like to revert UISwitch value

My application is a VOIP telephony toolbox.
I have a series of UISwitch controls, which the user can use to change their settings, for example if they want to alter their caller id settings.
When the user changes the setting I need to make a call to the Telephony platform over its Restful API. If the Restful call fails, then I would like to reset the switch back to its previous setting. eg If the user turns caller ID on, and it fails because of a connection failure, I would like the switch to revert back to off.
I implemented this in my switchChangedValue method, however it creates a nasty loop. When a failure happens I set the UISwitch to its previous setting, but it in turn calls the switchChangedValue method again, which fails and so on looping
Here is part of my switchChangedValue method, any ideas welcome.
//Check if its a valid response from the XSI server
if ([bs getHTTPResponseCode] >= 200 && [bs getHTTPResponseCode] < 300) {
//This is the successful case
}
else
{
// I throw an alert here
//Id really like to change the UISwitch back if it goes wrong but it causes a bad loop.
if (buttonstate == false){
[switchbutton setOn:YES animated:YES];
//This invokes my switchChangedValue again
}
else if (buttonstate == true){
[switchbutton setOn:NO animated:YES];
//This invokes my switchChangedValue again
} else{
NSLog(#"Something went bad");
}
[bs release];
You might try something like this:
Declare this in your header:
BOOL _fireAPICall;
Set it to YES whenever the particular class you're in is initialized:
- (id)init {
if (self = [super init]) {
...
_fireAPICall = YES;
...
}
return self;
}
Then:
if (_fireAPICall) {
if ([bs getHTTPResponseCode] >= 200 && [bs getHTTPResponseCode] < 300) {
// success
} else {
// failure
_fireAPICall = NO;
[switchbutton setOn:!buttonstate animated:YES];
}
} else {
_fireAPICall = YES;
// handle case where switch is turned off if necessary
}
This is assuming that you're not making an API call when the user manually turns the switch off, though - is that the case?
Updated above!