Objective-C view pop-up after random time - ios

Let's say John has been using my app for 3-6 minutes.
Then I'd like a view to pop up that would in my case, include an advertisement.
Something like this,
AdViewController *adViewController = [[AdViewController alloc] init];
[self presentViewController:adViewController animated:YES completion:nil];
But how can I let it pop up after some random time? I guess I have to work with the delegate files and use the arc4random function.
After John has viewed the advertisement, he'd then have to close it, but that's not the problem..
Can someone give me a code example?

The simple solution is to
Create a NSTimer and let it fire every 300 secs (5 mins)
NSTimer will fire an action that shows your pop-up.
I don't get it why was this so difficult to understand?
//use arc4random() if you need random time
NSTimer *timer2 = [NSTimer scheduledTimerWithTimeInterval:300.0 target:self selector:#selector(rateThisApp) userInfo:nil repeats:YES];
// *********
// ********* RATE APP ***********
// *********
- (IBAction)rateThisApp
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Rate this App"
message:#"Are you enjoying this app? Please leave a rating at the app store and tell us what you think of this app and its features. We would love to hear from you!"
delegate:self cancelButtonTitle:#"Not Now"
otherButtonTitles:#"Rate Now", nil];
[alert show];
alert.tag = 400;
}
-(void)alertView:(UIAlertView *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (actionSheet.tag == 400)
{
if (buttonIndex == 0)
{
//dont do anything, user hit cancel
}
else if (buttonIndex == 1)
{
[[UIApplication sharedApplication]
openURL:[NSURL URLWithString:#"itms-apps://ax.itunes.apple.com/WebObjects/MZStore.woa/wa/viewContentsUserReviews?type=Purple+Software&id=1234567"]];
}
}
}

Related

Animations stopped working

I know it sounds a bit general, but this is exactly the issue. After roaming a bit in my app, something I do (don't know what) causes the app to lose all its animations.
Animations like: keyboard popping up/down, view controllers push animations... Or even my coded animations. Nothing works. Everything just pops in/out instantly.
I'm not even sure which code of mine causes this...
Could it be a bug in my code? or a bug in my device? or a bug in iOS8?
Thanks
Animations will stop working when performing them in a background queue. Ideally, you would find and fix such instances in your code. Here is a library that helps track them down:
https://github.com/Cocoanetics/DTFoundation/blob/develop/Core/Source/iOS/Debug/UIView%2BDTDebug.m
This is a good workaround (from the App developer forums) to get animations to work again:
[UIView setAnimationsEnabled:YES]
OK... Found it.
I was loading a new view controller. In the viewDidLoad I have put a block to see if the app has permissions to create a reminder. The loading of the page was postponed after the completion of the block and that's what caused the bug.
This is my BAD code:
- (void)viewDidLoad {
[super viewDidLoad];
self.shouldAlert = YES;
[[EventStoreManager sharedManager] connectRemindersPermissionWithCompletion:^(BOOL granted) {
if (granted) {
[self initData];
[self initGraphics];
[self pageLoaded];
} else {
dispatch_async(dispatch_get_main_queue(), ^(void) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"\"APP_NAME\" Would Like to Access Your Reminders" message:#"Settings -> Privacy -> Reminders" delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil, nil];
[alert showWithCompletion:^(UIAlertView *alertView, NSInteger buttonIndex) {
[self closeAddTaskButtonPressed:nil];
}];
});
}
}];
}
This is my correction:
- (void)viewDidLoad {
[super viewDidLoad];
self.shouldAlert = YES;
[self initData];
[self initGraphics];
[self pageLoaded];
[[EventStoreManager sharedManager] connectRemindersPermissionWithCompletion:^(BOOL granted) {
if (granted) {
} else {
dispatch_async(dispatch_get_main_queue(), ^(void) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"\"APP_NAME\" Would Like to Access Your Reminders" message:#"Settings -> Privacy -> Reminders" delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil, nil];
[alert showWithCompletion:^(UIAlertView *alertView, NSInteger buttonIndex) {
[self closeAddTaskButtonPressed:nil];
}];
});
}
}];
}
Bug solved... :)
Unlikely but: try searching for "speed" in your code base. In case you
[UIApplication sharedApplication].keyWindow.layer.speed = 99.0;
or something similar.

iOS: Decrease delay in UIAlertView show when app enters foreground

I have created a password protected app. The app is allowed to run in background.
When it returns to foreground, I display an alert to prompt the user for password, by overriding the applicationWillEnterForeground: method in appdelegate like so-
- (void)applicationWillEnterForeground:(UIApplication *)application
{
if (/*password is enabled*/) {
alertview = [[UIAlertView alloc] initWithTitle:#"LOGIN"
message:#"Enter app password"
delegate:self
cancelButtonTitle:nil
otherButtonTitles:nil];
alertview.alertViewStyle = UIAlertViewStyleSecureTextInput;
pwdTF = [alertview textFieldAtIndex:0];
[pwdTF setDelegate:self];
[alertview show];
}
}
However, the alert takes a little time to appear. During this time, the view remains vulnerable.
Is there a way to make uialertview show instantly?
dispatch_async(dispatch_get_main_queue(), ^{
<# Write UI related code to be executed on main queue #>
});

Having Problems Adding "Call" Button To iOS App

Below is the action and alert view. Why Won't This Work When The User Taps The Button?
Alert/Action
-(IBAction)myButton
{
UIAlertView *alert=[[UIAlertView alloc]initWithTitle:#"" message:#"Call (804) 378-7120?"
delegate:self cancelButtonTitle:#"NO" otherButtonTitles:#"YES",nil];
[alert show];
[alert release];
}
What To Do With User Input
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if(buttonIndex==1)
{
[[UIApplication sharedApplication]
openURL:[NSURL URLWithString:#"tel://(804) 378-7120"]];
}
else
{
//Do whatever you want
}
}
As far as I remember, the index of the 1st other button is 0. So you say
if(buttonIndex==0)
{...open you URL...}
else
{
NSLog(#\"cancel\");
}
Per Apple documentation for Phone Links, the format is:
tel:1-408-555-5555
Similar to an email link, there is no //, and notice the use of hyphens, not parenthesis or space.
use this method when the user hit the YES option.
**-(void)phoneCall
{
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:#"tel:099123456"]];
}**
Change the number 099123456 to the number what you want to call.
Hope this result useful.

How to respond to UIImagePickerController Generated UIAlertView

I am using UIImagePickerController to capture video from my app and i have set video maximum duration to 30 seconds. When that 30 seconds limit is reached. I get an alert with a message "maximum video recording limit reached" produced by UIImagePickerController and it stops capturing video.
What I want is that I want to respond to that alert that is generated automatically when 30 seconds limit is reached. I want to perform some action when "OK" button of that alert is pressed. I have implemented all the delegate methods of UIAlertView but it does come in any method when I press OK button.
Please help me how I can respond to that alert?
You can't use all those delegate methods because you didn't initiate the UIAlertView so you can't set his delegate...
The only thing I can think about is to do somethong like listening to the UIWindowDidBecomeVisibleNotification to detect when an alert is shown and to the UIWindowDidBecomeHiddenNotification notification to detect when it disappears.
You should know that those notification will fire for all kind of components that uses their own UIWindow such as UIActionSheet or the keyboard, so you need to make sure this is the right one (maybe check to see if there is a UIAlertView in one of the subviews..)
Set yourself as a delegate of your UIImagePickerController, and implement the UIImagePickerControllerDelegate protocol. Specifically, the following method:
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)
Use UIAlertViewDelegateProtocol
Form docs
alertView:clickedButtonAtIndex:
Sent to the delegate when the user
clicks a button on an alert view.
(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex Parameters alertView The
alert view containing the button. buttonIndex The index of the button
that was clicked. The button indices start at 0. Discussion The
receiver is automatically dismissed after this method is invoked.
Availability Available in iOS 2.0 and later. Declared In UIAlertView.h
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex == 0)
{
// do stuff for button index 0,ie cancel button and sthe same for other button indeces
}
}
Please refer this tutorials : http://mobile.tutsplus.com/tutorials/iphone/uialertview/ you can get more ideal about this :
UIAlertView *message = [[UIAlertView alloc] initWithTitle:#"Hello World!"
message:#"This is your first UIAlertview message."
delegate:self
cancelButtonTitle:#"Button 1"
otherButtonTitles:#"Button 2", #"Button 3", nil];
[message show];
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSString *title = [alertView buttonTitleAtIndex:buttonIndex];
if([title isEqualToString:#"Button 1"])
{
NSLog(#"Button 1 was selected.");
}
else if([title isEqualToString:#"Button 2"])
{
NSLog(#"Button 2 was selected.");
}
else if([title isEqualToString:#"Button 3"])
{
NSLog(#"Button 3 was selected.");
}
}

Show fullscreen Flurry ad after a click in an AlertView

I've got a problem,
I added Flurry SDK in my iOS app and want to show a full screen add each time when the user click in the "Ok" button of an uiAlertView. The problem is that it doesn't work.
So, I tried try to do it without the AlertView and it works perfectly.
Someone knows why it doesn't work after clicking on an AlertView.
// The call of the alertview
UIAlertView *myAlert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(#"TitlePopupNewGame", nil) message:nil delegate:self cancelButtonTitle:NSLocalizedString(#"CancelPopupNewGame", nil) otherButtonTitles:NSLocalizedString(#"YesPopupNewGame", nil), nil];
myAlert.tag = 1;
myAlert.delegate = self;
[myAlert show];
// Alertview method
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
if (alertView.tag == 1 && buttonIndex == 0){
[self showInterstitial];
}
}
// ShowInterstitial method
-(void) showInterstitial{
if ([FlurryAds adReadyForSpace: #"Full screen banner"]) {
[FlurryAds displayAdForSpace: #"Full screen banner"
onView:mv];
[FlurryAds fetchAdForSpace:#"Full screen banner"
frame:mv.frame size:FULLSCREEN];
} else {
// fetch an ad
[FlurryAds fetchAdForSpace:#"Full screen banner"
frame:mv.frame size:FULLSCREEN];
}
}
Thanks

Resources