I am trying to do this for a few days, and still cannot make it. And I tried to find some samples but they are all on Android. Did anyone succeed to integrate admob on iOS?
I had some problems with AdMob bindings from monotouch-bindings repository. But then I switched to AlexTouch.GoogleAdMobAds bindings and them works just great. You can find sample of using AlexTouch.GoogleAdMobAds in README on Github. Is is quite simple, but if you'll need some help - feel free to ask more detailed question.
// code for admob "using AlexTouch.GoogleAdMobAds"
UIViewController vc = new UIViewController ();
UIViewController controller = UIApplication.SharedApplication.Windows [0].RootViewController;
var ad = new GADBannerView (GADAdSizeCons.SmartBannerLandscape, new PointF (0, 0))
{
AdUnitID = "ADMOB_ID",
RootViewController = vc
};
ad.Hidden = false;
ad.DidReceiveAd += delegate {
ad.Hidden = false;
ad.Frame = new System.Drawing.RectangleF (0, (int) 0, (int) (ad.Bounds.Width), (int) (ad.Bounds.Height));
Console.WriteLine ("AD Received");
};
ad.DidFailToReceiveAdWithError += delegate(object sender, GADBannerViewDidFailWithErrorEventArgs e) {
ad.Hidden = true;
Console.WriteLine (e.Error);
};
ad.WillPresentScreen += delegate {
Console.WriteLine ("showing new screen");
};
ad.WillLeaveApplication += delegate {
Console.WriteLine ("I will leave application");
};
ad.WillDismissScreen += delegate {
Console.WriteLine ("Dismissing opened screen");
};
ad.UserInteractionEnabled = true;
vc.View.AddSubview(ad);
vc.View.Frame = new System.Drawing.RectangleF(0f, 0f, (int)(ad.Bounds.Width), (int)(ad.Bounds.Height));
controller.View.AddSubview(vc.View);
Task.Factory.StartNew(() => {
while (true)
{
Console.WriteLine("Requesting Ad");
InvokeOnMainThread (delegate {
GADRequest r = new GADRequest();
ad.LoadRequest(r);
});
System.Threading.Thread.Sleep(30000);
}
});
Related
I created an app for iOS in VS with Xamarin. It works well both on iOS Simulator and real devices, but once I get a message from the user, that app crashes after a tap on UITextField. This is default iOS UITextField on the login screen. Strange, that this crash appears only on iPhone 6s with iOS v10.2.1. On iPhone with iOS >= v10.3 this crash doesn't appear.
This UITextField has only one event overriding
loginInput.EditingChanged += (s, e) =>
{
UITextField textInput = (UITextField)s;
if (textInput.Text.Length == 10)
{
textInput.ResignFirstResponder();
}
else if (textInput.Text.Length > 10)
{
char[] originalText = textInput.Text.ToCharArray();
char[] maskedText = new char[10];
for (int i = 0; i < 10; i++)
{
maskedText[i] = originalText[i];
}
textInput.Text = new string(maskedText);
textInput.ResignFirstResponder();
}
};
Also, I use UIKeyboard.Notifications.ObserveWillShow and UIKeyboard.Notifications.ObserveWillHide in ViewWillAppear overriding to translate the whole view depending on keyboard height. It's needed because otherwise, keyboard hides UITextField and user can't see, what he types.
public override void ViewWillAppear(bool animated)
{
base.ViewWillAppear(animated);
UIKeyboard.Notifications.ObserveWillShow((s, e) =>
{
var r = UIKeyboard.FrameEndFromNotification(e.Notification);
keyboardHeight = r.Height;
_originalPosition = mainPageView.Center.Y;
if (keyboardHeight == 0)
{
keyboardHeight = r.Height;
}
Action keyboardShift = () => {
var xpos = mainPageView.Center.X;
var ypos = mainPageView.Center.Y - keyboardHeight;
mainPageView.Center = new CGPoint(xpos, ypos);
};
UIViewPropertyAnimator propertyAnimator = new UIViewPropertyAnimator(0.25, UIViewAnimationCurve.EaseInOut, keyboardShift);
propertyAnimator.StartAnimation();
});
UIKeyboard.Notifications.ObserveWillHide((s, e) =>
{
var r = UIKeyboard.FrameEndFromNotification(e.Notification);
keyboardHeight = r.Height;
if (keyboardHeight == 0)
{
keyboardHeight = r.Height;
}
Action keyboardUnShift = () => {
var xpos = mainPageView.Center.X;
var ypos = _originalPosition;
mainPageView.Center = new CGPoint(xpos, ypos);
};
UIViewPropertyAnimator propertyAnimator = new UIViewPropertyAnimator(0.25, UIViewAnimationCurve.EaseInOut, keyboardUnShift);
propertyAnimator.StartAnimation();
});
}
Hope somebody help me with this.
how to create small task bar (Reply, Copy, Forword and Delete) in uitableviewcell like whatsapp when the message on press?
"UIMenuController" with "menuItems:" will be useful for your custom Actions.
Apple Documentation Link : https://developer.apple.com/reference/uikit/uimenucontroller
thank Ios Developer for your direction.. i already use "UIMenuController", but this menu is not shown.
my code:
[Export("LongPressMethod:")]
public void LongPressMethod(UILongPressGestureRecognizer gestureRecognizer)
{
if (gestureRecognizer.State == UIGestureRecognizerState.Began)
{
var menuController = UIMenuController.SharedMenuController;
var copyMenuItem = new UIMenuItem("copy", new ObjCRuntime.Selector("CopyRow"));
var pasteMenuItem = new UIMenuItem("paste", new ObjCRuntime.Selector("PasteRow"));
var location = gestureRecognizer.LocationInView(bc);
bc.BecomeFirstResponder();
menuController.MenuItems = new[] { copyMenuItem, pasteMenuItem };
menuController.SetTargetRect(new CGRect(location.X, location.Y, 100, 100), bc);
menuController.SetMenuVisible(true, true);
}
}
[Export("CopyRow:")]
void Row(UIMenuController controller)
{
// do something
}
[Export("PasteRow:")]
void PasteRow(UIMenuController controller)
{
// do something
}
In my project, when I launch camera first time it works fine. when I launch camera second time, I see image last clicked in view finder. I am not sure what's causing this.
Can anyone please help me here?
following is code block to launch camera:
UIImagePickerController imagePicker = new UIImagePickerController();
// Handle media selected.
var documentsDirectory = Environment.GetFolderPath (Environment.SpecialFolder.MyDocuments);
imagePicker.FinishedPickingMedia += (sender, e) => {
UIImage image = (UIImage)e.Info.ObjectForKey(
new NSString("UIImagePickerControllerOriginalImage"));
if (image != null)
{
this.InvokeOnMainThread(() => {
this.clickedImage.Image = image;
image.SaveToPhotosAlbum(delegate(UIImage img, NSError err){
});
string pngFilename = System.IO.Path.Combine (documentsDirectory, "Photo.png"); // hardcoded filename, overwrites each time
NSData imgData = image.AsPNG();
NSError SaveErr = null;
if (imgData.Save(pngFilename, false, out SaveErr))
{
Console.WriteLine("saved as " + pngFilename);
} else {
Console.WriteLine("NOT saved as" + pngFilename + " because" + SaveErr.LocalizedDescription);
}
});
}
DismissViewController(true,null);
};
// Handle cancellation of picker.
imagePicker.Canceled += (sender, e) => {
DismissViewController(true,null);
};
btnCameraDisplay1.SetTitle("Take Picture", UIControlState.Normal);
btnCameraDisplay1.Font = UIFont.SystemFontOfSize(19);
btnCameraDisplay1.SetTitleColor(UIColor.Black, UIControlState.Normal);
btnCameraDisplay1.TouchUpInside += delegate(object sender, EventArgs e)
{
if(UIImagePickerController.IsSourceTypeAvailable(UIImagePickerControllerSourceType.Camera))
{
imagePicker.SourceType = UIImagePickerControllerSourceType.Camera;
imagePicker.AllowsEditing = false;
this.PresentViewController(imagePicker, true,null);
}
else{
alertView = new UIAlertView();
alertView.AddButton("OK");
alertView.Message = "No camera available in this device.";
alertView.AlertViewStyle = UIAlertViewStyle.Default;
alertView.Show();
}
};
I ran into the same Problem. Some searching here on Stackoverflow helped:
It might be a problem with Xamarins implementation of the FinishedPickingMedia Event. (from here https://stackoverflow.com/a/20503817/383658)
Solution:
Switch from the Event System to Delegates as explained here:
https://stackoverflow.com/a/20035698/383658
So basically move your code from:
imagePicker.FinishedPickingMedia += (sender, e) => {}
to your new UIImagePickerControllerDelegate delegates method:
public override void FinishedPickingMedia (UIImagePickerController picker, NSDictionary info)
{}
I am trying to load Admob interstitial ads using a static class, so I can show it in a convient time.
I have created a method for loading the Interstitial:
public static void GetInterstitial()
{
interstitial = new GADInterstitial ();
interstitial.AdUnitID = "my_ad_unit_id";
interstitial.LoadRequest (GADRequest.Request);
interstitial.DidReceiveAd += (sender, args) => {
didInterstitialLoad = true;
} ;
interstitial.DidFailToReceiveAd += (object sender, GADInterstitialDidFailToReceiveAdWithErrorEventArgs e) => {
didLoadInterFailed = true;
};
interstitial.DidDismissScreen += (sender, e) => {
//TODO - Handle interstitial dismiss ();
};
}
and then in my ViewController:
if (isInterstitialLoaded) {
interstitial.PresentFromRootViewController(this.NavigationController);
}
This works for the first time, but after that I am getting the following error in interstitial.DidFailToReceiveAd:
Will not send request because interstitial object has been used
What can be done to fix it?
I got this error when I tried to interstitial.PresentFromRootViewController(this.NavigationController);
The solution was to recreate the entire interstitial ad after dismissing the last one. So inside your DidDismissScreen handler, just create it again:
didInterstitialLoad = false;
interstitial = new GADInterstitial ();
interstitial.AdUnitID = "my_ad_unit_id";
interstitial.LoadRequest (GADRequest.Request);
interstitial.DidReceiveAd += (sender, args) => {
didInterstitialLoad = true;
};
HIH :)
I have just updated my application to iOS 6, and the SetInitialText function of the TWTweetComposeViewController has stopped working. The composer just comes up empty. I am using MonoTouch, has anyone else seen this issue? The same code still works fine on my iOS 5 device.
This works as expected for me with the following code:
var button2 = UIButton.FromType (UIButtonType.RoundedRect);
button2.Frame = new RectangleF (0f, 0f, 300f, 40f);
button2.SetTitle ("Tweet!", UIControlState.Normal);
button2.TouchUpInside += (sender, e) => {
var tvc = new TWTweetComposeViewController ();
tvc.SetInitialText ("Here is a tweet...");
tvc.SetCompletionHandler ((result) => {
if (result == TWTweetComposeViewControllerResult.Cancelled) {
Console.WriteLine ("Cancelled sending the tweet!");
} else {
Console.WriteLine ("Tweet sent! hurrah!");
}
this.DismissModalViewControllerAnimated (true);
});
this.PresentModalViewController (tvc, true);
};
View.AddSubview (button2);
Even though PresentModal and DismissModal are deprecated, do you have any example code of the problem?