Check if Local Notifications are enabled in IOS 8 - notifications

I've looked all over the internet for how to create local notifications with IOS 8. I found many articles, but none explained how to determine if the user has set "alerts" on or off. Could someone please help me!!! I would prefer to use Objective C over Swift.

You can check it by using UIApplication 's currentUserNotificationSettings
if ([[UIApplication sharedApplication] respondsToSelector:#selector(currentUserNotificationSettings)]){ // Check it's iOS 8 and above
UIUserNotificationSettings *grantedSettings = [[UIApplication sharedApplication] currentUserNotificationSettings];
if (grantedSettings.types == UIUserNotificationTypeNone) {
NSLog(#"No permiossion granted");
}
else if (grantedSettings.types & UIUserNotificationTypeSound & UIUserNotificationTypeAlert ){
NSLog(#"Sound and alert permissions ");
}
else if (grantedSettings.types & UIUserNotificationTypeAlert){
NSLog(#"Alert Permission Granted");
}
}
Hope this helps , Let me know if you need more info

To expand on Albert's answer, you are not required to use rawValue in Swift. Because UIUserNotificationType conforms to OptionSetType it is possible to do the following:
if let settings = UIApplication.shared.currentUserNotificationSettings {
if settings.types.contains([.alert, .sound]) {
//Have alert and sound permissions
} else if settings.types.contains(.alert) {
//Have alert permission
}
}
You use the bracket [] syntax to combine option types (similar to the bitwise-or | operator for combining option flags in other languages).

Swift with guard:
guard let settings = UIApplication.sharedApplication().currentUserNotificationSettings() where settings.types != .None else {
return
}

Here is a simple function in Swift 3 that checks whether at least one type of notification is enabled.
Enjoy!
static func areNotificationsEnabled() -> Bool {
guard let settings = UIApplication.shared.currentUserNotificationSettings else {
return false
}
return settings.types.intersection([.alert, .badge, .sound]).isEmpty != true
}
Thanks Michał Kałużny for the inspiration.

Edit: Take a look at #simeon's answer.
In Swift, you need to use rawValue:
let grantedSettings = UIApplication.sharedApplication().currentUserNotificationSettings()
if grantedSettings.types.rawValue & UIUserNotificationType.Alert.rawValue != 0 {
// Alert permission granted
}

Using the #simeon answer Xcode tells me that
'currentUserNotificationSettings' was deprecated in iOS 10.0: Use UserNotifications Framework's -[UNUserNotificationCenter getNotificationSettingsWithCompletionHandler:] and -[UNUserNotificationCenter getNotificationCategoriesWithCompletionHandler:]
so here is the solution using the UNUserNotificationCenter for Swift 4:
UNUserNotificationCenter.current().getNotificationSettings(){ (settings) in
switch settings.alertSetting{
case .enabled:
//Permissions are granted
case .disabled:
//Permissions are not granted
case .notSupported:
//The application does not support this notification type
}
}

I think this code is more precise :
if ([[UIApplication sharedApplication] respondsToSelector:#selector(currentUserNotificationSettings)]) {
UIUserNotificationType types = [[[UIApplication sharedApplication] currentUserNotificationSettings] types];
if (types & UIUserNotificationTypeBadge) {
NSLog(#"Badge permission");
}
if (types & UIUserNotificationTypeSound){
NSLog(#"Sound permission");
}
if (types & UIUserNotificationTypeAlert){
NSLog(#"Alert permission");
}
}

Objective C + iOS 10
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
[center getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings * _Nonnull settings) {
switch (settings.authorizationStatus) {
case UNAuthorizationStatusNotDetermined:
break;
case UNAuthorizationStatusDenied:
break;
case UNAuthorizationStatusAuthorized:
break;
default:
break;
}
}];

Related

How to detect whether device support FaceID or not?

Its bit early to ask but I'm planning to add feature specially for FaceID, so before that I need to validate either device support FaceID or not?
Need suggestion and help.
Thanks in advance.
Objective-C version
- (BOOL) isFaceIdSupported{
if (#available(iOS 11.0, *)) {
LAContext *context = [[LAContext alloc] init];
if ([context canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:nil]){
return ( context.biometryType == LABiometryTypeFaceID);
}
}
return NO;
}
I found that you have to call canEvaluatePolicy before you will properly get the biometry type. If you don't you'll always get 0 for the raw value.
So something like this in Swift 3, tested and working in Xcode 9.0 & beta 9.0.1.
class func canAuthenticateByFaceID () -> Bool {
//if iOS 11 doesn't exist then FaceID doesn't either
if #available(iOS 11.0, *) {
let context = LAContext.init()
var error: NSError?
if context.canEvaluatePolicy(LAPolicy.deviceOwnerAuthenticationWithBiometrics, error: &error) {
//As of 11.2 typeFaceID is now just faceID
if (context.biometryType == LABiometryType.typeFaceID) {
return true
}
}
}
return false
}
You could of course write that just to see if it's either biometric and return the type along with the bool but this should be more than enough for most to work off of.
Thanks Ashley Mills, I created a function to detect FaceID in Device.
- (BOOL)canAuthenticateByFaceID {
LAContext *context = [[LAContext alloc] init];
if context.canEvaluatePolicy(LAPolicy.deviceOwnerAuthenticationWithBiometrics, error: &error) {
if (context.biometryType == LABiometryTypeFaceID && #available(iOS 11.0, *)) {
return YES;
} else {
return NO;
}
}
}
Hope this will help other. Happy coding!!
Finally I wrote my own Library for detecting FaceID here you find
Swift 4 compatible version
var isFaceIDSupported: Bool {
if #available(iOS 11.0, *) {
let localAuthenticationContext = LAContext()
if localAuthenticationContext.canEvaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, error: nil) {
return localAuthenticationContext.biometryType == .faceID
}
}
return false
}
+(BOOL)supportFaceID
{
LAContext *myContext = [[LAContext alloc] init];
NSError *authError = nil;
// call this method only to get the biometryType and we don't care about the result!
[myContext canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:&authError];
NSLog(#"%#",authError.localizedDescription);
if (#available(iOS 11.0, *)) {
return myContext.biometryType == LABiometryTypeFaceID;
} else {
// Device is running on older iOS version and OFC doesn't have FaceID
return NO;
}
}

How would this be converted from Obj-C to Swift?

I am trying to re-write an app in swift which is currently in Objective-C. How would I change this single line into Swift, as my current attempt does not prove correct
Tabata *tabata = [notification object];
Here is the entire function:
- (void)stateChanged:(NSNotification*)notification
{
if (enabled)
{
Tabata *tabata = [notification object];
switch (tabata.getState) {
case EXERCISE:
case RELAXATION:
[player play];
break;
default:
break;
}
}
}
And here is what I've converted into Swift:
func stateChanged(notifcation: NSNotification) {
if enabled {
var tabata: Tabata! = notification.object //error "Use of unresolved identifier 'notification'"
switch tabata.getState() {
case .EXERCISE: fallthrough
case .RELAXATION:
player.play()
break
default:
break
}
}
}
In Swift you have to downcast objects of type AnyObject rather then declare the type
var tabata = notification.object as! Tabata
func stateChanged(notification: NSNotification) {
if(enabled) {
var tabata: Tabata = notification.object as! Tabata
switch tabata.getState() {
case .EXERCISE:
fallthrough
case .RELAXATION:
player.play()
break
default:
break
}
}
}
Hope this helps!

How do I open my Swift app with a URL and pass data?

I am trying to get my app to be able to capture the url that launched it, and parse the passed values.
Here is how it is done in ObjC
- (void)handleGetURLEvent:(NSAppleEventDescriptor *)event withReplyEvent:(NSAppleEventDescriptor *)replyEvent
{
[event paramDescriptorForKeyword:keyDirectObject] ;
NSString *urlStr = [[event paramDescriptorForKeyword:keyDirectObject] stringValue];
// Now you can parse the URL and perform whatever action is needed
NSLog(#"URL: %#", urlStr);
}
What I have so far is ..
func handleGetURLEvent(event: NSAppleEventDescriptor?, replyEvent: NSAppleEventDescriptor?) {
}
After a few days scouring the web ... I found this solution.
func applicationWillFinishLaunching(notification: NSNotification?) {
// -- launch the app with url
NSAppleEventManager.sharedAppleEventManager().setEventHandler(self, andSelector: Selector("handleGetURLEvent:withReplyEvent:"), forEventClass: AEEventClass(kInternetEventClass), andEventID: AEEventID(kAEGetURL))
}
func handleGetURLEvent(event: NSAppleEventDescriptor!, withReplyEvent: NSAppleEventDescriptor!) {
let urlPassed = NSURL(string: event.paramDescriptorForKeyword(AEKeyword(keyDirectObject))!.stringValue!)
println(urlPassed)
}
I had 2 issues in my code. Firstly my selector did not match the function name for getting the url (handleGetURLEvent). The second issue was the finding the correct placement of exclamation points for unwrapping the optionals.
Hopes this helps you out if you had the same issue.
I think this is what you need
func handleGetURLEvent(event: NSAppleEventDescriptor?, replyEvent: NSAppleEventDescriptor?) {
if let aeEventDescriptor = event?.paramDescriptorForKeyword(AEKeyword(keyDirectObject)) {
let urlStr = aeEventDescriptor.stringValue
println(urlStr)
}
}

How to programmatically open an NSComboBox's list?

I've been around this for a while.. I thought this should be an easy task, but it isn't =D
What I am trying to do, is to display the combobox's list when the user clicks the combobox but not specifically in the button.
Any Idea?
Thanks in advance!
This answer fits the title of the question, but not question itself. Omer wanted to touch a text field and have the box popup.
This solution shows the popup when the user enters text.
I found this answer on cocoabuilder from Jens Alfke. I reposted his code here. Thanks Jens.
original cocoabuilder post: (http://www.cocoabuilder.com/archive/cocoa)
#interface NSComboBox (MYExpansionAPI)
#property (getter=isExpanded) BOOL expanded;
#end
#implementation NSComboBox (MYExpansionAPI)
- (BOOL) isExpanded
{
id ax = NSAccessibilityUnignoredDescendant(self);
return [[ax accessibilityAttributeValue:
NSAccessibilityExpandedAttribute] boolValue];
}
- (void) setExpanded: (BOOL)expanded
{
id ax = NSAccessibilityUnignoredDescendant(self);
[ax accessibilitySetValue: [NSNumber numberWithBool: expanded]
forAttribute: NSAccessibilityExpandedAttribute];
}
I used this code in my controlTextDidChange: method.
- (void) controlTextDidChange:(NSNotification *) aNotification {
NSTextField *textField = [aNotification object];
NSString *value = [textField stringValue];
NSComboBox *box = [self comboBox];
if (value == nil || [value length] == 0) {
if ([box isExpanded]) { [box setExpanded:NO]; }
} else {
if (![box isExpanded]) { [box setExpanded:YES]; }
}
}
Returns true if the NSComboBox's list is expanded
comboBox.cell?.isAccessibilityExpanded() ?? false
Open the NSComboBox's list
comboBox.cell?.setAccessibilityExpanded(true)
Close the NSComboBox's list
comboBox.cell?.setAccessibilityExpanded(false)
Ref. jmoody’s answer.
You can use the following code line:
[(NSComboBoxCell*)self.acomboBox.cell performSelector:#selector(popUp:)];
Put
comboBoxCell.performSelector(Selector("popUp:"))
Into
override func controlTextDidChange(obj: NSNotification) {}
is what I ended up with. Thanks #Ahmed Lotfy
Here's the full code, it works for me on OSX 10.11
override func controlTextDidChange(obj: NSNotification) {
if let comboBoxCell = self.comboBox.cell as? NSComboBoxCell {
comboBoxCell.performSelector(Selector("popUp:"))
}
}
Thanks to jmoody and Jens Alfke mentioned above. Here is a SWIFT translation of the above solution.
import Cocoa
class CComboBoxEx: NSComboBox {
override func drawRect(dirtyRect: NSRect) {
super.drawRect(dirtyRect)
// Drawing code here.
}
func isExpanded() -> Bool{
if let ax:AnyObject? = NSAccessibilityUnignoredDescendant(self) {
if ax!.accessibilityAttributeValue(NSAccessibilityExpandedAttribute) != nil {
return true
}
}
return false
}
func setExpanded (bExpanded:Bool) {
if let ax:AnyObject? = NSAccessibilityUnignoredDescendant(self) {
ax!.accessibilitySetValue(NSNumber(bool: bExpanded), forAttribute: NSAccessibilityExpandedAttribute)
}
}
}
NSComboBox was not designed to work this way. Because the user may want to edit the text in the control, they'll need to be able to click it without unexpectedly popping up the choices.
You would need to subclass NSComboBoxCell and change this behavior ... but then you'd have a standard-looking control that does not behave in a standard way. If you're determined to do this, take a look at the open source version of NSComboBoxCell. The interesting methods appear to be -popUpForComboBoxCell: and friends.
Based on the other answers I wrote this solution (tested with Xcode 10.2.1, Swift 5). It uses the same ideas but it's a little shorter.
// Put this extension for NSComboBox somewhere in your project
import Cocoa
public extension NSComboBox {
var isExpanded: Bool{
set {
cell?.setAccessibilityExpanded(newValue)
}
get {
return cell?.isAccessibilityExpanded() ?? false
}
}
}
// Set your corresponding NSViewController as NSComboBoxDelegate
// in the storyboard and add this piece of code
// to expand the combobox when the user types
class MyViewController: NSViewController, NSComboBoxDelegate {
func controlTextDidChange(_ notification: Notification) {
guard let comboBox = notification.object as? NSComboBox else { return }
if comboBox.isExpanded == false {
comboBox.isExpanded = true
}
}
}

Make my Cocoa app respond to the keyboard play/pause key?

Is there a way to make my app respond to the play/pause button on Mac?
EDIT:
Using the suggested code,I get this console message:
Could not connect the action buttonPressed: to target of class NSApplication
Why would that be?
I accomplished this in my own application by subclassing NSApplication (and setting the app's principal class to this subclass). It catches seek and play/pause keys and translates them to specific actions in my app delegate.
Relevant lines:
#import <IOKit/hidsystem/ev_keymap.h>
- (void)sendEvent:(NSEvent *)event
{
// Catch media key events
if ([event type] == NSSystemDefined && [event subtype] == 8)
{
int keyCode = (([event data1] & 0xFFFF0000) >> 16);
int keyFlags = ([event data1] & 0x0000FFFF);
int keyState = (((keyFlags & 0xFF00) >> 8)) == 0xA;
// Process the media key event and return
[self mediaKeyEvent:keyCode state:keyState];
return;
}
// Continue on to super
[super sendEvent:event];
}
- (void)mediaKeyEvent:(int)key state:(BOOL)state
{
switch (key)
{
// Play pressed
case NX_KEYTYPE_PLAY:
if (state == NO)
[(TSAppController *)[self delegate] togglePlayPause:self];
break;
// Rewind
case NX_KEYTYPE_FAST:
if (state == YES)
[(TSAppController *)[self delegate] seekForward:self];
break;
// Previous
case NX_KEYTYPE_REWIND:
if (state == YES)
[(TSAppController *)[self delegate] seekBack:self];
break;
}
}
Edit:
Swift 4:
override func sendEvent(_ event: NSEvent)
{
if event.type == .systemDefined &&
event.subtype == .screenChanged
{
let keyCode : Int32 = (Int32((event.data1 & 0xFFFF0000) >> 16))
let keyFlags = (event.data1 & 0x0000FFFF)
let keyState = ((keyFlags & 0xFF00) >> 8) == 0xA
self.mediaKeyEvent(withKeyCode: keyCode, andState: keyState)
return
}
super.sendEvent(event)
}
private func mediaKeyEvent(withKeyCode keyCode : Int32, andState state : Bool)
{
guard let delegate = self.delegate as? AppDelegate else { return }
switch keyCode
{
// Play pressed
case NX_KEYTYPE_PLAY:
if state == false
{
delegate.musicPlayerWC.handleUserPressedPlayButton()
}
break
// Rewind
case NX_KEYTYPE_FAST:
if state == true
{
delegate.musicPlayerWC.handleUserPressedNextSongButton()
}
break
// Previous
case NX_KEYTYPE_REWIND:
if state == true
{
delegate.musicPlayerWC.handleUserPressedPreviousSongButton()
}
break
default:
break
}
}
Here's a great article on the subject: http://www.rogueamoeba.com/utm/2007/09/29/
Check this: https://gist.github.com/gauravk92/546311
Works perfectly.
As example, this repo uses it: https://github.com/onepill/PauseIt
In case anyone comes looking, there is sample code to do this here. This approach does allow you to "eat" the events they do not reach iTunes or other media-key-aware apps.
But be warned that event taps are not allowed by the sandbox, so this won't work in the App Store. If anyone has a workaround for that I'd love to hear it.