Is there a way to get the iPhone's carrier, and/or the current signal strength, using Objective-C? I know how to determine if a data connection is present, and whether or not that connection is wi-fi vs. cellular. I also know that you can manually place the iPhone into "field test" mode by going to the phone app, and dialing #3001*12345*# and hitting Send.
You made me curious and I found out that it's actually *3001#12345#* (hashes and stars exchanged).
This probably won't pass Apple's review, but you can use CTTelephony notifications.
First, link against CTTelephony.
Now just use this:
static void callback(CFNotificationCenterRef center, void *observer, CFStringRef name, const void *object, CFDictionaryRef userInfo) {
CFShow(name)
NSString *sName = name;
if ([sName isEqualToString:#"kCTIndicatorsSignalStrengthNotification"]) {
if (userInfo) CFShow(userInfo);
}
}
And this to subscribe:
id ct = CTTelephonyCenterGetDefault();
CTTelephonyCenterAddObserver(
ct,
NULL,
callback,
NULL,
NULL,
NULL);
Related
I am trying to detect when a user locks the device (vs. pressing home button for instance).
Found this:
CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(), //center
NULL, // observer
lockStateChanged, // callback
CFSTR("com.apple.springboard.lockstate"), // event name
NULL, // object
CFNotificationSuspensionBehaviorDeliverImmediately);
static void lockStateChanged(CFNotificationCenterRef center, void *observer, CFStringRef name, const void *object, CFDictionaryRef userInfo) {
NSLog(#"event received!");
// you might try inspecting the `userInfo` dictionary, to see
// if it contains any useful info
if (userInfo != nil) {
CFShow(userInfo);
}
}
I can imagine that com.apple.springboard.lockstate is like calling private API? Or is this fine?
Assuming all the CF... functions are public you are probably OK, but in a murky area for sure. Next release of iOS could break your code if Apple changes that string.
What I did in a similar situation for an approved shipping app was to avoid using the string directly. Create an array of the strings, then use the NSString method to combine them with a period separator instead of using com.apple.springboard.lockstate directly.
YMMV
I want to get all the notifications of the BluetoothManager private framework. I've been searching but i only have found two (BluetoothAvailabilityChangedNotification and BluetoothDeviceDiscoveredNotification).
I'm interesting in a notification that reports if iphone connected/disconnected to a device. If anyone could get me a list of all notifications i will be appreciated.
I don't have a full list, but these are the ones you were interested in:
BluetoothDeviceConnectFailedNotification
BluetoothDeviceConnectSuccessNotification
BluetoothDeviceDisconnectFailedNotification // haven't confirmed this one
BluetoothDeviceDisconnectSuccessNotification
Here are some others:
BluetoothConnectabilityChangedNotification // fires when bluetooth is turned on/off
BluetoothAvailabilityChangedNotification // seems to fire once at app start)
BluetoothPowerChangedNotification
BluetoothDeviceDiscoveredNotification
BluetoothDeviceRemovedNotification
BluetoothPairingUserNumericComparisionNotification
BluetoothPairingPINResultSuccessNotification
Add before you call [BluetoothManager sharedInstance]:
CFNotificationCenterAddObserver(CFNotificationCenterGetLocalCenter(),
NULL,
bluetoothCallback,
NULL,
NULL,
CFNotificationSuspensionBehaviorDeliverImmediately);
and somewhere in this implementation the method void bluetoothCallback:
void bluetoothCallback (CFNotificationCenterRef center,
void *observer,
CFStringRef name,
const void *object,
CFDictionaryRef userInfo)
{
if (CFStringGetCharacterAtIndex(name, 0) == 'B') { // stupid way to filter for only 'B'luetooth notifications
NSLog(#"%#", name);
}
}
Your console log shows you all bluetooth notifications now.
Is there a way to detect if headphones are unplugged in Monotouch? I am trying to look for the AudioSessionAddPropertyListener method but don't see it. What this method ported over?
Here is Apple's docs: http://developer.apple.com/library/ios/#documentation/AudioToolbox/Reference/AudioSessionServicesReference/Reference/reference.html#//apple_ref/doc/constant_group/Audio_Session_Interruption_States
If anyone wants to see the code for how to do this, you can do the following:
AudioSession.PropertyListener p = delegate(AudioSessionProperty prop, int size, IntPtr data) {
NSDictionary propertyDictionary = new NSDictionary(data);
if (propertyDictionary.ContainsKey(NSObject.FromObject("OutputDeviceDidChange_OldRoute")))
{
string oldRoute = propertyDictionary.ValueForKey(new NSString("OutputDeviceDidChange_OldRoute")).ToString();
if (oldRoute == "Headphone")
{
if (audioPlayer != null)
{
audioPlayer.Pause();
}
}
}
};
AudioSession.AddListener(AudioSessionProperty.AudioRouteChange, p);
Is there a way to detect if headphones are unplugged in Monotouch?
I'm not sure but...
I am trying to look for the AudioSessionAddPropertyListener method but don't see it. What this method ported over?
The native call to AudioSessionAddPropertyListener maps to MonoTouch's AudioSession.AddListener static method.
I have a mailing application. If the user sends a mail successfully, then I need to notify that the mail was sent successfully. For that, I need to know if his phone is on silent mode
(in which case there will be a 'vibrate') or regular mode (in which case there will be a 'beep'). Can anyone help me with it?
Thanks in advance
If you use the correct audio session type, iOS will handle this for you:
http://developer.apple.com/library/ios/#documentation/Audio/Conceptual/AudioSessionProgrammingGuide/Introduction/Introduction.html
CFStringRef state;
UInt32 propertySize = sizeof(CFStringRef);
AudioSessionInitialize(NULL, NULL, NULL, NULL);
AudioSessionGetProperty(kAudioSessionProperty_AudioRoute, &propertySize, &state);
if(CFStringGetLength(state) == 0)
{
//SILENT
}
else
{
//NOT SILENT
}
If the state string is empty then the phone is on silent - otherwise the phone has an audio output
EDIT:
remember to add the AudioToolbox framework and import. – Thomas Clayson
answer taken from (http://iphone-dev-tips.alterplay.com/2009/12/iphone-silent-mode-detection.html)
I m creating an app that has to display CFUserNotificationDisplayAlert even if iPhone Screen is Locked, currently i am using this code
CFOptionFlags responseFlags = 0;
CFUserNotificationDisplayAlert(20.0, 3, NULL, NULL, NULL, CFSTR("Hello"), CFSTR("Hello World"), CFSTR("OK"), NULL, NULL, &responseFlags);
This works great on Home Screen but doesnt pop up if the screen is locked. Is there anything else i have to add to it to make it appear on the Lock Screen as well?
You need to use the kCFUserNotificationAlertTopMostKey key.
extern CFStringRef kCFUserNotificationAlertTopMostKey;
CFStringRef keys[] = {
kCFUserNotificationAlertTopMostKey,
kCFUserNotificationAlertHeaderKey,
kCFUserNotificationAlertMessageKey
};
CFStringRef values[] = {
kCFBooleanTrue,
CFSTR("Title"),
CFSTR("Message")
};
CFDictionaryRef dict = CFDictionaryCreate(NULL, keys, values,
sizeof(keys)/sizeof(*keys),
&kCFTypeDictionaryKeyCallBacks,
&kCFTypeDictionaryValueCallBacks);
SInt32 err = 0;
CFUserNotificationRef notif = CFUserNotificationCreate(NULL,
0, kCFUserNotificationPlainAlertLevel, &err, dict);
CFRelease(dict);
...
See http://iphonedevwiki.net/index.php/CFUserNotification for all dialog description keys for iPhone OS ≤ 3.1.
(Note that while it will show on the lock screen, the phone won't wake up by itself.)
CFUserNotification is not supported on the iPhone OS. Push Notifications are the iPhone equivalent.