Sending messages from the application to twitter in certain conditions - ios

Can application send a message in twitter without user intervention under certain conditions? For example when charging the battery less 10%?
please, sorry for bad english!

You can monitor battery level like so:
UIDevice *device = [UIDevice currentDevice];
device.batteryMonitoringEnabled = YES;
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(batteryChanged:) name:#"UIDeviceBatteryLevelDidChangeNotification" object:device];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(batteryChanged:) name:#"UIDeviceBatteryStateDidChangeNotification" object:device];
// Your notification handler
- (void)batteryChanged:(NSNotification *)notification
{
UIDevice *device = [UIDevice currentDevice];
if(device.batteryLevel <= 10)
[self twittBatteryLevel: device.batteryLevel];
}
And then implement -(void)twittBatteryLevel:(float)batteryLevel as you would using twitter API

Related

The difference between these two scheduled method calls - iOS

I have seen this around a few times but can't seem to find what is the difference between the two ...
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(loginViewFetchedUserInfo:)
name:FBSDKProfileDidChangeNotification
object:nil];
- (void)loginViewFetchedUserInfo:(NSNotification *)notification
and
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(loginViewFetchedUserInfo)
name:FBSDKProfileDidChangeNotification
object:nil];
- (void)loginViewFetchedUserInfo
I know that (void)methodname:(TYPE *)newName can pass in a value to the method but I don't know what the difference is in the two above and why you would do the first one (which is used in the Facebook SDK example) over the second one.
The first method passes the NSNotification object to the method. This way allows you to access information about the notification.
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(loginViewFetchedUserInfo:)
name:FBSDKProfileDidChangeNotification
nil];
For example, if the notification was posted with a userInfo dictionary
NSDictionary *userInfo = #{#"Blah" : #"foo"};
[[NSNotificationCenter defaultCenter] postNotificationName:FBSDKProfileDidChangeNotification object:self userInfo:userInfo];
and you wanted to access userInfo in the method. You can also access the sender of the notification, which would be the notification's object.
- (void)loginViewFetchedUserInfo:(NSNotification *)notification
{
NSDictionary *userInfo = notification.userInfo;
NSObject *sender = notification.object;
}

NSNotificationCenter defaultCenter iOS 8. Notification is not being delivered to one of observers

Prior iOS 8 all works fine. The problem is:
I have two observers in different classes:
class1:
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(didFinishParseUser:)
name:USERS_LOADED_NOTIFICATION_ID object:nil];
class2:
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(didFinishParseUser:)
name:USERS_LOADED_NOTIFICATION_ID object:nil];
and notification is posted in some other place:
[FBRequestConnection startWithGraphPath:#"me/friends?fields=id,first_name,last_name,picture.type(small)" completionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
if (!error) {
[[NSNotificationCenter defaultCenter] postNotificationName:USERS_LOADED_NOTIFICATION_ID object:nil userInfo:[NSDictionary dictionaryWithObjectsAndKeys: currentUser, #"user", friends, #"friends", nil]];
} else {
// An error occurred, we need to handle the error
// See: https://developers.facebook.com/docs/ios/errors
}
}];
addObserver method is called for both of mentioned classes, however notification is being delivered just to one observer. If I delete this observer(which receives the notification), then another one receives the notification.
Prior to iOS 8 both observers receive the notification.
Can you, please, help me with this issue?
Found answer.
There is another way in iOS 8 to register for receiving remote notifications. I get nil for device token and app break on line:
NSDictionary *item = #{UID_ID : sCurrentUserId, #"deviceToken": appDelegate.deviceToken, #"handle": #"", #"friends": friends};
and second observer never receives notification.
You need to register your NSNotifications to work on iOS 8 and later but don't need to register if your iOS version is less than iOS 8.
Use Following Code
NSArray *vComp = [[UIDevice currentDevice].systemVersion componentsSeparatedByString:#"."];
if ([[vComp objectAtIndex:0] intValue] >= 8) {
[[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|UIUserNotificationTypeSound|UIUserNotificationActivationModeBackground categories:nil]];
}
happy Coding

Method getting called multiple times

I am using MPMoviePlayer in my project. I have registered for the movie player finish notifications and it is working good. I am displaying an error alert whenever a notification is received for movie player error. But the problem is that the error alert displays multiple times. It happens because more than one notifications are received for same error and that too at the same time. I have tried using boolean variables to control the alert display but since the notifications are received at the same time, it is not working. What approach should I apply, please suggest.
My code for notification method:
MPMovieFinishReason reason = [[[notification userInfo] objectForKey:MPMoviePlayerPlaybackDidFinishReasonUserInfoKey] intValue];
NSError *errorMsg = [[notification userInfo] valueForKey:#"error"];
NSString *errmsg = [errorMsg localizedDescription];
if (reason == 1 && !errorReceived){
NSError *errorMsg = [[notification userInfo] valueForKey:#"error"];
NSString *errmsg = [errorMsg localizedDescription];
[self showErrorAlert];
}
For registering notification:
NSNotificationCenter* center = [NSNotificationCenter defaultCenter];
[center addObserver:self selector:#selector(moviePlayerDidFinish:) name:MPMoviePlayerPlaybackDidFinishNotification object:self.player];
for removing observer, in viewWillDisappear
[[NSNotificationCenter defaultCenter]removeObserver:self];
Remove the observer once you get the error
[[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:player];
and add the observer once you click to play .

Not getting EA notification with bluetooth

I am able to get EA notification when connecting to an external device (MFi compliant) via USB but not via Bluetooth. Why is EA notification not being fired for Bluetooth connection, contrary to what the documentation suggests?
1) Did you set the Protocol String correctly in your InfoPlist?
2) Did you set the Protocol String correctly in your bluetooth device?
3) Did you register for incoming connection event? like this way:
-(void) <someMethod> {
[[EAAccessoryManager sharedAccessoryManager] registerForLocalNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(accessoryConnected:)
name:EAAccessoryDidConnectNotification
object:nil];
}
-(void)accessoryConnected: (NSNotification *)notification {
EAAccessory *accessory = [[notification userInfo] objectForKey:EAAccessoryKey];
NSLog(#"%# connected", accessory.name);
}

Getting MAC address of a bluetooth using BluetoothManager private framework

I'm trying to implement device discovery using bluetooth in IOS 5.0.1 iPhone 4S.
I'm using the private framework BluetoothManager.
My code is:
- (IBAction)searchForDevices:(id)sender
{
[self.indicator setHidden:NO];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(bluetoothAvailabilityChanged:) name:#"BluetoothAvailabilityChangedNotification" object:nil];
btCont = [BluetoothManager sharedInstance];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(deviceDiscovered:) name:#"BluetoothDeviceDiscoveredNotification" object:nil];
}
- (void)bluetoothAvailabilityChanged:(NSNotification *)notification
{
self.label.text = #"Availability changed!";
[btCont setDeviceScanningEnabled:YES];
}
- (void)deviceDiscovered:(BluetoothDevice *)device
{
[self.indicator setHidden:YES];
self.label.text = device.address;
My bluetooth headset is discovered.
deviceDiscovered callback function is called,
but device.address does NOT contain the MAC address of the bluetooth device. The app is crashing.
Also, device.name return the name of the notification (BluetoothDeviceDiscoveredNotification) instead of the name of the device discovered.
Any suggestions how can I retrieve the MAC address of my bluetooth headset this way?
use this code:
- (void)deviceDiscovered:(NSNotification *) notification {
BluetoothDevice *bt = [notification object];
NSLog(#"name: %# address: %#",bt.name, bt.address);
If this is a jailbreak app, you can use the key kLockdownBluetoothAddressKey via liblockdown.dylib

Resources