Receive one-time message when approaching iBeacon - objective-c

I playing a little around with iBeaconss and now set up my code so I receive a pop when I enter a beacon (with code below). Problem I run into now however that it keeps popping up.
Does anybody now how I have to set up my code so I only receive it once when I enter a region?
Regards,
Marc
if ([[NSString stringWithFormat:#"%#", beacon.minor] isEqualToString:#"51447"])
//hiermee kun je een pop-op geven met of hij wel of niet naar tweede scherm wil.
{
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:nil message:#"Open second screen?" delegate:self cancelButtonTitle:#"No" otherButtonTitles:#"Yes", nil];
[alertView show];
}

just remember it using a BOOL or a dictionary (for multiple
if ([[NSString stringWithFormat:#"%#", beacon.minor] isEqualToString:#"51447"])
//hiermee kun je een pop-op geven met of hij wel of niet naar tweede scherm wil.
{
if(![_shownRanges[#"51447"] boolValue]) {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:nil message:#"Open second screen?" delegate:self cancelButtonTitle:#"No" otherButtonTitles:#"Yes", nil];
[alertView show];
if(!_shownRanges) {
_shownRanges = [NSMutableDictionary dictionary];
}
_shownRanges[#"51447"] = #YES;
}
}
(_shownRanges is a NSMutableDictionary that you define in your interface)

Related

No visible #interface for 'UIAlertView' declares the selector :NSString stringWithUTF8String

i have an error which i donno how to fix it it's making me crazy. i have searched about the solution a lot but didn't find anything which works here .
here is the code and you can see the error in the picture .
thanks
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:[NSString stringWithUTF8String:title]
message:[NSString stringWithUTF8String:body]
delegate:nil
cancleButtonTitle:[NSString stringWithUTF8String:cancleLable]
otherButtonTitles:[NSString stringWithUTF8String:firstLable],
[NSString stringWithUTF8String:secondLable], nil];
[alert show];

Objective-c function

I'm trying to create a function in order to save some code, however I have no idea how I should do it.
if (count > 100 && count < 120)
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Time is up!"
message:[NSString stringWithFormat:#"You're really good at this! You scored %i points", count - 1]
delegate:self
cancelButtonTitle:#"Play Again"
otherButtonTitles:nil];
[alert show];
}
else if(count > 120)
{
UIAlertView *alert2 = [[UIAlertView alloc] initWithTitle:#"Time is up!"
message:[NSString stringWithFormat:#"TEACH ME MASTER, YOU'RE A GOD! You scored %i points", count - 1]
delegate:self
cancelButtonTitle:#"Play Again"
otherButtonTitles:nil];
[alert2 show];
}
else
{
UIAlertView *alert3 = [[UIAlertView alloc] initWithTitle:#"Time is up!"
message:[NSString stringWithFormat:#"Not very good, you scored %i points", count - 1]
delegate:self
cancelButtonTitle:#"Play Again"
otherButtonTitles:nil];
[alert3 show];
}
As you can see the code is very repetitive and I would like to create a function where I can change the value of the "alert" variable and the message string. So when I call the function I only enter the functions name and the value the alert variable should have, in this case; 1, 2 and 3 and the message text depending on what condition we are looking at.
So is there some way of doing this? There has to be, because repetitive code like this is never good looking! I would appreciate if someone could help me out, I've tried looking at some references but none of them made me get any closer to solving the problem.
Simply look at the duplicated functionality and examine the values that are different. In this case it's just the message:
NSString *message = nil;
if (count > 100 && count < 120)
{
message = [NSString stringWithFormat:#"You're really good at this! You scored %i points", count - 1];
}
else if(count >= 120)
{
message = [NSString stringWithFormat:#"TEACH ME MASTER, YOU'RE A GOD! You scored %i points", count - 1];
}
else
{
message = [NSString stringWithFormat:#"Not very good, you scored %i points", count - 1];
}
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Time is up!"
message:message
delegate:self
cancelButtonTitle:#"Play Again"
otherButtonTitles:nil];
[alert show];
(Edit: You missed the value 120 which would have slipped through the cracks).
I am create a new function that creates a new alert with given message :
-(void)showAlertWithMsg:(NSString *)msg tag:(NSInteger)tag
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Time is up!"
message:msg
delegate:self
cancelButtonTitle:#"Play Again"
otherButtonTitles:nil];
alert.tag = tag;
[alert show];
}
To call this function write the below code:
if (count > 100 && count < 120)
{
[self showAlertWithMsg:[NSString stringWithFormat:#"You're really good at this! You scored %i points", count - 1] tag:1];
}
else if(count > 120)
{
[self showAlertWithMsg:[NSString stringWithFormat:#"TEACH ME MASTER, YOU'RE A GOD! You scored %i points", count - 1] tag:2];
}
else
{
[self showAlertWithMsg:[NSString stringWithFormat:#"Not very good, you scored %i points", count - 1] tag:3];
}
One more thing I am also pass the tag.So if you want to perform different operation for different alert.
You can set "title" property exactly for existing UIAlertView instance:
UIAlertView * alertView = [[UIAlertView allow] init...];
alertView.title = #"Some message here.";

CBPeripheralManagerState shows always state = off

So I have this peripheralManager. I want to check, if the State of it is on or not:
if (peripheralManager.state < CBPeripheralManagerStatePoweredOn) {
UIAlertView *errorAlert = [[UIAlertView alloc] initWithTitle:#"Bluetooth must be enabled" message:#"To search for pics, you have to enable bluetooth." delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[errorAlert show];
}
I'm testing it on my iPhone 5 and the Bluetooth is clearly on. But it shows always, that it is off.
UPDATE
How can I fix it?

How to directly return to rootViewController? UIStoryboard (or update tableview)

There are some questions I have found but the answers don't work with my code, therefore I ask a question of my own.
My objective is reloading a table view from a detailviewcontroller. I tap a cell, go to the detail and when I return to the table I want it updated. The thing is it doesn't update. So I decided it would be better to go back to the rootviewcontroller when certain thing happens on the detailviewcontroller but it still didn't work.
I am open to suggestions and advice feel free to comment!!
I download a video and when the video is downloaded I update the tableView.
Here is the code I am using:
I use MKNetworkKit btw.
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *cachesDirectory = [paths objectAtIndex:0];
NSString *downloadPath = [cachesDirectory stringByAppendingPathComponent:videoName];
self.downloadOperation = [ApplicationDelegate.mainDownloader downloadVideoFrom:finalAddress
toFile:downloadPath];
[self.downloadOperation onDownloadProgressChanged:^(double progress) {
//DLog(#"%.2f", progress*100.0);
//self.downloadProgressBar.progress = progress;
}];
[self.downloadOperation onCompletion:^(MKNetworkOperation* completedRequest) {
//THIS DOES NOT WORK, dismissModalViewControllerAnimated.
[[ApplicationDelegate.window rootViewController] dismissModalViewControllerAnimated:YES];
DLog(#"COMPLETED REQUEST: %#", completedRequest);
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Download completed"
message:#"The file is in your photo and video Library"
delegate:nil
cancelButtonTitle:NSLocalizedString(#"Thank you WebSurg!!", #"")
otherButtonTitles:nil];
[alert show];
// THIS IS WHAT I PREVIOUSLY TRIED.
// NSDictionary *tableSecData = ApplicationDelegate.videoLibraryController.tableSectionData;
// NSMutableArray *tempValuesDownloaded = [tableSecData objectForKey:#"Downloaded videos"];
// NSMutableArray *tempValuesUndownloaded = [tableSecData objectForKey:#"Undownloaded videos"];
// for (NSArray *videoArray in tempValuesUndownloaded) {
// if ([[videoArray objectAtIndex:0] isEqualToString:self.videoDetailTitle.text]) {
// [tempValuesUndownloaded removeObject:videoArray];
// [tempValuesDownloaded addObject:videoArray];
// }
// }
// [ApplicationDelegate.videoLibraryController.tableSectionData removeAllObjects];
// ApplicationDelegate.videoLibraryController.tableSectionData = [NSMutableDictionary dictionaryWithObjectsAndKeys:tempValuesDownloaded, #"Downloaded videos", tempValuesUndownloaded, #"Undownloaded videos", nil];
// [ApplicationDelegate.videoLibraryController.mainTableView reloadData];
}
onError:^(NSError* error) {
DLog(#"%#", error);
[[[UIAlertView alloc] initWithTitle:#"Download failed" message:#"The download failed because of a connection error please try again" delegate:nil cancelButtonTitle:NSLocalizedString(#"Dismiss", #"") otherButtonTitles:nil] show];
}];
} else {
UIAlertView *failureAlert=[[UIAlertView alloc] initWithTitle:#"Download status" message:#"Download failed, not enough free space." delegate:self cancelButtonTitle:#"Ok" otherButtonTitles:nil, nil, nil];
[failureAlert show];
}

Macro compiler error: "Parse Issue" when an argument name appears elsewhere

I tried making a straightforward macro for creating and showing a simple "ok" dialog box in iOS:
#define ALERT_DIALOG(title,message) \
do\
{\
UIAlertView *alert_Dialog = [[UIAlertView alloc] initWithTitle:(title) message:(message) delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];\
[alert_Dialog show];\
} while ( 0 )
If I try to use it in my code:
ALERT_DIALOG(#"Warning", #"Message");
I get the error:
Parse Issue. Expected ']'
And the error seems to be pointing at the second # right before "Message".
However, if I simply copy paste the macro I do not get this error:
NSString *title = #"Warning";
NSString *message = #"Message";
do
{
UIAlertView *alert_Dialog = [[UIAlertView alloc] initWithTitle:(title) message:(message) delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert_Dialog show];
} while ( 0 );
Is there something against using Objective-c constructs in macros? Or is that something else?
The problem with your macro is that both occurrences of message in
... [[UIAlertView alloc] initWithTitle:(title) message:(message) ...
are replaced by #"Message", resulting in
.... [[UIAlertView alloc] initWithTitle:(#"Warning") #"Message":(#"Message") ...
and that causes the syntax error.
I don't think that it is really worth defining this as a macro, but if you do, you have to use macro arguments which do not occur at places where they should not be expanded, e.g.
#define ALERT_DIALOG(__title__,__message__) \
do\
{\
UIAlertView *alert_Dialog = [[UIAlertView alloc] initWithTitle:(__title__) message:(__message__) delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];\
[alert_Dialog show];\
} while ( 0 )
or similar.
Instead of declaring it as a macro, you can declare it as a C function instead:
void ALERT_DIALOG(NSString *title, NSString *message) {
UIAlertView *alert_Dialog = [[UIAlertView alloc] initWithTitle:(title) message:(message) delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];\
[alert_Dialog show];
}