Set Status bar in AppDelegate? - swift

Previously, I had this code and it worked perfectly before Swift was updated. Now it says:
Setter for 'statusBarStyle' was deprecated in iOS 9.0: Use -[UIViewController preferredStatusBarStyle]
Now, I read that you have to override the statusBarStyle but I don't want to do this manually in each UIViewController but instead, control it via a struct and an extension of the UIApplication. Not sure how to do that though.
The code:
extension UIApplication {
var statusBarView: UIView? {
return value(forKey: "statusBar") as? UIView
}
}
struct StatusBar {
static func setStatusBar() {
let appDelegate = UIApplication.shared.delegate as? AppDelegate
if let currentVC = appDelegate?.window?.rootViewController?.getCurrentlyDisplayedVC() {
if currentVC is LoginVC || currentVC is SignUpVC || currentVC is SignUpVC {
UIApplication.shared.statusBarView?.backgroundColor = .clear
}
else {
UIApplication.shared.statusBarView?.backgroundColor = Colors.mainBlueColor
}
}
UIApplication.shared.statusBarStyle = .lightContent
}
}
extension UIViewController {
func getCurrentlyDisplayedVC() -> UIViewController {
if let presentedVC = presentedViewController {
return presentedVC.getCurrentlyDisplayedVC()
}
else if let split = self as? UISplitViewController, let last = split.viewControllers.last {
return last.getCurrentlyDisplayedVC()
}
else if let nav = self as? UINavigationController, let top = nav.topViewController {
return top.getCurrentlyDisplayedVC()
}
else if let tab = self as? UITabBarController {
if let selected = tab.selectedViewController {
return selected.getCurrentlyDisplayedVC()
}
}
return self
}
}

Related

use a switch to change the value of boolean saved core data value

In my swift code below right now the code saves 2 boolean values to a core data boolean value. The two values are true false. I would like the user to turn on the switch and have the 2nd value be true as well. So it would be true true. I am trying to do that in func alternate() but I dont know how to exactly effect a specific value. Looking for any kind of help.
import UIKit;import CoreData
class ViewController: UIViewController {
var lbl = UILabel()
var sw = UISwitch()
var checkmarkButton = UIButton()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
[ lbl,sw,checkmarkButton].forEach {
view.addSubview($0)
$0.translatesAutoresizingMaskIntoConstraints = false
$0.layer.borderWidth = 1
}
helpBool.shareInstance.saveBoolean(true)
helpBool.shareInstance.saveBoolean(false)
getBool(imageNo: 1)
}
func getBool(imageNo:Int) {
// first check the array bounds
let info = helpBool.shareInstance.fetchBool()
if info.count > imageNo {
// if info[imageNo].bool {
if info[imageNo].bool == true {
checkmarkButton.setImage(UIImage(named:"unnamed"), for: .normal);
}
if info[imageNo].bool == false {
checkmarkButton.setImage(nil, for: .normal);
}
// }
}
}
#objc func alternate(){
//fetch alternate
getBool(imageNo: 1)
if sw.isOn = true {
helpBool.shareInstance.saveBoolean()
}
else {
helpBool.shareInstance.saveBoolean()
}
}
}
class helpBool: UIViewController{
static let shareInstance = helpBool()
let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
func saveBoolean(_ boolean: Bool) {
let imageInstance = OnOff(context: context)
imageInstance.bool = boolean
do {
try context.save()
print("text is saved")
} catch {
print(error.localizedDescription)
}
}
func fetchBool() -> [OnOff] {
var fetchingImage = [OnOff]()
let fetchRequest = NSFetchRequest<NSFetchRequestResult>(entityName: "OnOff")
do {
fetchingImage = try context.fetch(fetchRequest) as! [OnOff]
} catch {
print("Error while fetching the image")
}
return fetchingImage
}
}

macOS menu bar text with icon

As you can see in the image I would like to be able to do a similar one, to make a way that instead of showing only the icon of the sun, also showing a text.
As seen in the image below, an icon followed by a text.
But I only managed to do this:
The problem I would like to put the icon on the left or right of the text, not above it, can you give me a hand?
P.s.
The text must change accordingly, how can I make the StatusBarController receive the text changes.
import AppKit
import SwiftUI
class StatusBarController {
#ObservedObject var userPreferences = UserPreferences.instance
private var statusBar: NSStatusBar
private var statusItem: NSStatusItem
private var popover: NSPopover
init(_ popover: NSPopover) {
self.popover = popover
statusBar = NSStatusBar.init()
statusItem = NSStatusBar.system.statusItem(withLength: NSStatusItem.variableLength)
if let statusBarButton = statusItem.button {
if let _ = userPreferences.$inDownload {
statusItem.button?.title = userPreferences.$percentualDownload
}
statusBarButton.image = #imageLiteral(resourceName: "Weather")
statusBarButton.image?.size = NSSize(width: 18.0, height: 18.0)
statusBarButton.image?.isTemplate = true
statusBarButton.action = #selector(togglePopover(sender:))
statusBarButton.target = self
statusBarButton.imagePosition = NSControl.ImagePosition.imageLeft
}
}
#objc func togglePopover(sender: AnyObject) {
if(popover.isShown) {
hidePopover(sender)
}
else {
showPopover(sender)
}
}
func showPopover(_ sender: AnyObject) {
if let statusBarButton = statusItem.button {
popover.show(relativeTo: statusBarButton.bounds, of: statusBarButton, preferredEdge: NSRectEdge.maxY)
}
}
func hidePopover(_ sender: AnyObject) {
popover.performClose(sender)
}
}
I'm thinking of using something like that:
import EventKit
import ServiceManagement
private struct PreferencesKeys {
static let backgroundIsTransparent = "backgroundIsTransparent"
static let inDownload = "inDownload"
static let percentualDownload = "percentualDownload"
}
class UserPreferences: ObservableObject {
static let instance = UserPreferences()
private init() {
// This prevents others from using the default '()' initializer for this class.
}
private static let defaults = UserDefaults.standard
#Published var backgroundIsTransparent: Bool = {
guard UserDefaults.standard.object(forKey: PreferencesKeys.backgroundIsTransparent) != nil else {
return true
}
return UserDefaults.standard.bool(forKey: PreferencesKeys.backgroundIsTransparent)
}() {
didSet {
UserPreferences.defaults.set(backgroundIsTransparent, forKey: PreferencesKeys.backgroundIsTransparent)
}
}
#Published var inDownload: Bool = {
guard UserDefaults.standard.object(forKey: PreferencesKeys.inDownload) != nil else {
return true
}
return UserDefaults.standard.bool(forKey: PreferencesKeys.inDownload)
}() {
didSet {
UserPreferences.defaults.set(inDownload, forKey: PreferencesKeys.inDownload)
}
}
#Published var percentualDownload: String = {
guard UserDefaults.standard.object(forKey: PreferencesKeys.percentualDownload) != nil else {
return "0%"
}
return UserDefaults.standard.string(forKey: PreferencesKeys.percentualDownload)!
}() {
didSet {
UserPreferences.defaults.set(percentualDownload, forKey: PreferencesKeys.percentualDownload)
}
}
}
but I get the following error:
Edit:
First problem solved I used:
statusBarButton.imagePosition = NSControl.ImagePosition.imageLeft
statusBarButton.imagePosition = NSControl.ImagePosition.imageRight
For the update text problem, what can I do?

Passing data from Scene Delegate to ViewController when opening app with URL

I need to Show a View on my initial controller and set a UILabel when the app launches through URL Dynamic Link during Firebase Email Verification.
Problem I'm having is the label is not initialised so app crashes during the process. I have tried many approaches but can't get it to show my view and set the label when opening a Dynamic Link, even when the App is already open.
Scene Delegate
func scene(_ scene: UIScene, continue userActivity: NSUserActivity) {
if let link = userActivity.webpageURL?.absoluteString
{
if Auth.auth().isSignIn(withEmailLink: link) {
Config().verifyEmail(link: link)
}
}
}
AppDelegate
#main
class AppDelegate: UIResponder, UIApplicationDelegate {
var errorText: String?
var errorType: Bool?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
FirebaseApp.configure()
return true
}
ViewController
func loadErrorInfo()
{
let appDelegate = UIApplication.shared.delegate as! AppDelegate
if let eText = appDelegate.errorText, let eType = appDelegate.errorType {
errorCall(message: eText, error: eType)
} else {
errorBox.isHidden = true
}
}
func errorCall(message: String, error: Bool)
{
if error {
self.errorLabel.textColor = UIColor.yellow
self.errorLabel.text = message
self.errorBox.isHidden = false
self.errorBox.shake()
} else {
self.errorLabel.textColor = UIColor.white
self.errorLabel.text = message
self.errorBox.isHidden = false
self.errorBox.shakeL()
}
}
Custom Config NSObject Class
import UIKit
import FirebaseAuth
open class Config: NSObject {
public func verifyEmail(link: String)
{
var email = ""; var password = ""
if let x = UserDefaults.standard.object(forKey: "Email") as? String { email = x }
if let y = UserDefaults.standard.object(forKey: "Password-\(email)") as? String { password = y }
if password != "" && email != ""
{
Auth.auth().signIn(withEmail: email, link: link) { (user, error) in
if let use = user, error == nil
{
Auth.auth().currentUser?.updatePassword(to: password) { (error) in
if let error = error
{
print(error)
let appDelegate = UIApplication.shared.delegate as! AppDelegate
appDelegate.errorText = error.localizedDescription
appDelegate.errorType = true
ViewController().loadErrorInfo()
}
else
{
print(use, "Logged In")
let appDelegate = UIApplication.shared.delegate as! AppDelegate
appDelegate.errorText = "\(use) Logged In"
appDelegate.errorType = false
ViewController().loadErrorInfo()
}
}
}
}
}
}
}
Not sure what other method will work. To summarise, after clicking a Dynamic Link in an Email, I want to Change my UILabel on main ViewController which is an initial controller. At the moment every approach causes it to crash as the UILabel is not Set, even if the controller is already initialised.
Managed to fix it by initialising the controller again and updating the root controller.
// SceneDelegate
func changeRootViewController(_ vc: UIViewController, animated: Bool = true)
{
guard let window = self.window else {
return
}
window.rootViewController = vc
}
//Reinitialise
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let destinationNavigationController = storyboard.instantiateViewController(withIdentifier: "ViewController2")
(UIApplication.shared.connectedScenes.first?.delegate as? SceneDelegate)?.changeRootViewController(destinationNavigationController)

Replacing UISearchBar magnifying icon with UIActivityViewController

I've been searching all day but couldn't find out a fix for this code.
This extension replaces the UISearchField magnifying icon with UIActivityView (shows the loading icon when is true)
The extension was working fine on iOS 12, Xcode 10.3 but after I've changed into iOS 13, Xcode 11 Beta 4 it stopped working.
I've still made a workaround using this:
if let textFieldInsideSearchBar = searchBar.value(forKey: "searchField") as? UITextField {
let loadingIcon = UIActivityIndicatorView()
loadingIcon.style = .medium
loadingIcon.backgroundColor = UIColor.clear
loadingIcon.startAnimating()
textFieldInsideSearchBar.leftView = loadingIcon
}
But I can't understand the reason why the extension stopped working.
Also I've noticed that .flatMap was deprecated in iOS 13 and changed to .compactMap but as I understood there were no differences, and I've already tried to change the .flatMap to .compactMap but still didn't work.
Here is the extension:
extension UISearchBar {
private var textField: UITextField? {
let subViews = self.subviews.compactMap { $0.subviews }
return (subViews.filter { $0 is UITextField }).first as? UITextField
}
private var searchIcon: UIImage? {
let subViews = subviews.flatMap { $0.subviews }
return ((subViews.filter { $0 is UIImageView }).first as? UIImageView)?.image
}
private var activityIndicator: UIActivityIndicatorView? {
return textField?.leftView?.subviews.compactMap{ $0 as? UIActivityIndicatorView }.first
}
var isLoading: Bool {
get {
return activityIndicator != nil
} set {
let _searchIcon = searchIcon
if newValue {
if activityIndicator == nil {
let _activityIndicator = UIActivityIndicatorView()
_activityIndicator.style = .medium
_activityIndicator.startAnimating()
_activityIndicator.backgroundColor = UIColor.clear
self.setImage(UIImage(), for: .search, state: .normal)
textField?.leftView?.addSubview(_activityIndicator)
let leftViewSize = textField?.leftView?.frame.size ?? CGSize.zero
_activityIndicator.center = CGPoint(x: leftViewSize.width/2, y: leftViewSize.height/2)
}
} else {
self.setImage(_searchIcon, for: .search, state: .normal)
activityIndicator?.removeFromSuperview()
}
}
}
}
There have been some changes with iOS 13 in terms of UISearchBar, And you can use UISearchBar.searchTextField instead of searchBar.value(forKey: "searchField")
searchBar.searchTextField.backgroundColor = .red
Or if you want to keep it work with the extension, You can do this:
var searchTextField: UITextField? {
let subViews = self.subviews.first?.subviews.last?.subviews
return subViews?.first as? UITextField
}

Thread 1: EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0

I am using below code in my project. After update to swift 4 and run my app crashed . How can I fix it?
Code:
func from(systemItem: UIBarButtonSystemItem)-> UIImage? {
let tempItem = UIBarButtonItem(barButtonSystemItem: systemItem, target: nil, action: nil)
// add to toolbar and render it
UIToolbar().setItems([tempItem], animated: false)
// got image from real uibutton
let itemView = tempItem.value(forKey: "view") as! UIView
for view in itemView.subviews {
if let button = view as? UIButton, let imageView = button.imageView {
return imageView.image
}
}
return nil
}
}
extension UITextView {
static let ScrollModeBottom = "UITextFieldScrollModeBottom"
static let ScrollModeUp = "UITextFieldScrollModeUp"
static let ScrollModeMiddle = "UITextFieldScrollModeMiddle"
func scrollToBotom() {
let range = NSMakeRange((text as NSString).length - 1, 1);
scrollRangeToVisible(range);
}
var scrollMode: String {
let scrollViewHeight: Float = Float(frame.size.height)
let scrollContentSizeHeight: Float = Float(contentSize.height)
let scrollOffset: Float = Float(contentOffset.y)
if scrollOffset == 0 {
return UITextView.ScrollModeUp
} else if scrollOffset + scrollViewHeight == scrollContentSizeHeight {
return UITextView.ScrollModeBottom
} else {
return UITextView.ScrollModeMiddle
}
}
}
Thread 1: EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0)
// add to toolbar and render it
let bar = UIToolbar()
bar.setItems([tempItem],
animated: false)
bar.snapshotView(afterScreenUpdates: true)
//got image from real uibutton
let itemView = tempItem.value(forKey: "view") as! UIView
for view in itemView.subviews {
if let button = view as? UIButton,
let image = button.imageView?.image {
return image.withRenderingMode(.alwaysTemplate)
}
}
return nil
}
}