How to reset badge count when app after app is opened? - notifications

In my notification function I set the badge count to one for each notification. How do I reset it to 0 when the app is opened?

Call this:
UIApplication.shared.applicationIconBadgeNumber = 0;

Add below code in AppDelegate.swift file.
UIApplication.shared.applicationIconBadgeNumber = 0
In the Method
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool

Related

2 app delegates, swift and objective-c

So, I have an app mixed with objective-c and swift, (originally objective-c) And I need to figure out how to have 2 app delegates (one for swift, and the other for objective-c). I've done some research, but found nothing. Please help!
EDIT:
So, I succesfully switched, but now in my delegate, i have this code:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
let splitViewController = window!.rootViewController as! UISplitViewController
let navigationController = splitViewController.viewControllers[splitViewController.viewControllers.count-1] as! UINavigationController
navigationController.topViewController!.navigationItem.leftBarButtonItem = splitViewController.displayModeButtonItem
splitViewController.delegate = self
return true
}
But, splitViewController isn't the first vc, so how would I go around fixing it? (I don't know swift very well)
You only need one delegate, do it in Swift. If you create a new app Xcode will use some default code, which as of 8.3.1 is the following:
import UIKit
#UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
return true
}
func applicationWillResignActive(_ application: UIApplication) {
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
}
func applicationDidEnterBackground(_ application: UIApplication) {
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}
func applicationWillEnterForeground(_ application: UIApplication) {
// Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
}
func applicationDidBecomeActive(_ application: UIApplication) {
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}
func applicationWillTerminate(_ application: UIApplication) {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}
}
To answer the comment: it doesn't mess up your code. Remove previous Objective C delegate if needed.

How to turn off the automatic swipe to go back in navigation controller? Swift

I need to turn off the automatic gesture swiping to go back in navigation controller. I using Swift.
I have already searched but didn't found a solution for me.
You can disable the interactivePopGestureRecognizer of your navigationController:
self.navigationController?.interactivePopGestureRecognizer.enabled = false
self.navigationController.interactivePopGestureRecognizer.delegate = self
func gestureRecognizerShouldBegin(gestureRecognizer: UIGestureRecognizer!) -> Bool {
return false;
}
I found this after a couple little searches. Not sure if its correct or not.
Swift version:
Add this delegate in your UIViewController: UIGestureRecognizerDelegate
Then in your viewDidLoad() method add the next line:
self.navigationController!.interactivePopGestureRecognizer.delegate = self
and finally add this delegate method:
func gestureRecognizerShouldBegin(gestureRecognizer: UIGestureRecognizer) -> Bool {
return false
}
Good luck!

UISplitViewController in Swift navigation from DetailView

I'm trying to create a MasterDetail Application in Swift and it's running well on iOS8 Simulator. However, when I tried it on my iOS 7.1 iPad I got this error:
**Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '- [UISplitViewController displayModeButtonItem]: unrecognized selector sent to instance**
This is in my AppDelegate.swift file(generated by Xcode, I didn't add anything):
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Override point for customization after application launch.
let splitViewController = self.window!.rootViewController as UISplitViewController
let navigationController = splitViewController.viewControllers[splitViewController.viewControllers.count-1] as UINavigationController
navigationController.topViewController.navigationItem.leftBarButtonItem = splitViewController.displayModeButtonItem()
splitViewController.delegate = self
let masterNavigationController = splitViewController.viewControllers[0] as UINavigationController
let controller = masterNavigationController.topViewController as MasterViewController
controller.managedObjectContext = self.managedObjectContext
return true
}
The problem comes from this line where the left button is created:
navigationController.topViewController.navigationItem.leftBarButtonItem = splitViewController.displayModeButtonItem()
When I remove this line, it runs on iOS 7 but displays only the DetailView. When I swipe from the left edge the MasterView doesn't appear(on iOS 8 simulator it does) and basically there is no navigation to the MasterView
Had anyone have the same problem?
Thank you!
You can still use deprecated callback function in UISplitViewControllerDelegate to add and remove UIBarButtonItem to your detail view for IOS7 platform. Implement as below in your UISplitViewControllerDelegate
func splitViewController(svc: UISplitViewController, willHideViewController aViewController: UIViewController, withBarButtonItem barButtonItem: UIBarButtonItem, forPopoverController pc: UIPopoverController) {
if !self.respondsToSelector(Selector("displayModeButtonItem")) {
let navigationController = self.viewControllers.last as! UINavigationController
let detailViewController: UIViewController? = navigationController.viewControllers?.last as? UIViewController
barButtonItem.image = UIImage(named: "IC_BackArrow")
detailViewController?.navigationItem.leftBarButtonItem = barButtonItem
} else {
// This callback function is depreciated in IOS8. We use displayModeButtonItem.
}
}
func splitViewController(svc: UISplitViewController, willShowViewController aViewController: UIViewController, invalidatingBarButtonItem barButtonItem: UIBarButtonItem) {
if !self.respondsToSelector(Selector("displayModeButtonItem")) {
let navigationController = self.viewControllers.last as! UINavigationController
let detailViewController: UIViewController? = navigationController.viewControllers?.last as? UIViewController
detailViewController?.navigationItem.leftBarButtonItem = nil
} else {
// This callback function is depreciated in IOS8. We use displayModeButtonItem.
}
}
I had the same issue. displayModeButtonItem is not supported by IOS versions prior to IOS 8. I suspect that you are compiling against IOS 8 but when deploying you are deploying onto an Ipad app with IOS 7.1 installed. I have remedied this situation in my situation (because I can) by installing IOS 8 on the target Ipad. It then worked without issue.

How to prevent NSWindow from closing on the windowWillClose method?

How can I prevent my NSWindow from being closed when the windowWillClose method is called?
You can't. By the time windowWillClose: is called, it is too late. You need to stop it before this point with windowShouldClose:.
Swift example:
You need to set the delegate of the NSWindow and implement NSWindowDelegate
func windowShouldClose(_ sender: NSWindow) -> Bool {
return false
}

How to re-open the Mac Desktop App after closing it by using X?

I have created an App for Mac Desktop, it is working, but Apple rejected it because when we run the App and close it by using "X", the we can not re-open it from the dock though the App icon is still there but it doesn't open the App again and the main issue for that I am struggling is that "If we close the App then in the menu bar there is no option to open it" other App which I have seen does that.
What should I do?
Here's an answer for Swift 3:
Conform to NSWindowDelegate in your View Controller class. Then hide the window instead of closing it by overriding the following method.
self.view.window?.delegate = self
func windowShouldClose(_ sender: Any) -> Bool {
NSApplication.shared().hide(self)
return false
}
Then unhide the application when the app icon is clicked in the dock.
func applicationDidBecomeActive(_ notification: Notification) {
NSApplication.shared().unhide(self)
}
To implement simple show/hide functionality for the red (x) button, make your App Delegate class the window delegate for your main window, as well.
Then add the following code to it:
- (BOOL)windowShouldClose:(id)sender {
[[NSApplication sharedApplication] hide:self];
return NO;
}
- (void)applicationDidBecomeActive:(NSNotification *)notification {
[[NSApplication sharedApplication] unhide:self];
}
my 2 cernts for swift 5/Xcode 10
note: You can call these methods also in ViewController (if useful) to prevent splitting code between NASWindow/NSView-Controllers.
in this case:
class ViewController: NSViewController, **NSWindowDelegate**
{
...
override func viewWillAppear() {
// we choose to delegate ourselves. must be done here, in viewDidLoad window is nil
let window = self.view.window
window!.delegate = self
}
...
no neeed to pass self.. : (as per other guys above.)
func windowShouldClose(_ sender: NSWindow) -> Bool {
NSApplication.shared.hide(nil)
return false // prevent closing.
}
func applicationDidBecomeActive(_ notification: Notification) {
NSApplication.shared.unhide(nil)
}