CBPeripheralManagerState shows always state = off - objective-c

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?

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];

Receive one-time message when approaching iBeacon

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)

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];
}

UIAlertView showing several times

I've got a simple UIAlertView showing when user runs the app. It has this structure:
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(#"Welcome!", "")
message:NSLocalizedString(#"This is a welcome message.", "")
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles: nil];
[alert show];
[alert release];
The question is, How can I customize it to show every 5 runs for example?
Thanks in advance ;)
You can use NSUserDefaults to store app executions count for key AppRunCount (you can introduce your own key name):
int runCount = [[NSUserDefaults standardUserDefaults] integerForKey:#"AppRunCount"] + 1
[[NSUserDefaults standardUserDefaults] setInteger:runCount forKey:#"AppRunCount"];
if (runCount <= 5) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(#"Welcome!", "")
message:NSLocalizedString(#"This is a welcome message.", "")
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles: nil];
[alert show];
[alert release];
}
You can just add code above to viewDidLoad
There will be a method in AppDelegate class like
- (void)applicationDidBecomeActive:(UIApplication *)application
Now create one NSUSerdefaults in this method and make one integer and increment that integer and save it in NSUserdefaults
Now every time application starts that method will be called and integer will incremented
Now make if condition in that method like below
if(your integer which has nsuserdefaults >=5)
{
your alertview
again here make your nsinteger to Zero which is stored in nsuserdefaults
your integer which has nsuserdefaults =0
}
This is the answer of your second question,Every after 5 times app run. alert will be pop up
Let me know it is working or not..!!!!
Happy Coding!!!!

does using try catch block in xcode show error on real device?

my application is running fine in simulator...but not on real device....and i have jailbroken iphone so i am unable to debug through device...if i use try catch something like this
#try
{
Statements
}
#catch (NSException *ex) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Error" message:[NSString stringWithFormat:#"%#",ex]
delegate:self cancelButtonTitle:#"OK" otherButtonTitles: nil];
[alert show];
}
will my application show error on alertview before crashing?
if not how could i find where is the bug?
Catching the exception means you are doing something in response to this "error" coming about and it wont crash the application , thats the point of catching exceptions-to tell how to handle cases where errors arrise so your app wont crash, so yes the alert view will show...
That will work so long as your #catch block doesn't throw any exceptions while trying to build the UIAlertView. Make sure you release or autorelease it, and support the UIAlertViewDelegate protocol.
Try this:
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:[ex name]
message:[ex reason]
delegate:self
cancelButtonTitle:#"OK"
otherButtonTitles: nil];