2

Hi am developing app using swift in my app I want use toast message and toast activity so i followed the link: https://github.com/scalessec/Toast-Swift. I am able to use in view controller method it works well but I can't use in app delegate methods.

My code in my app delegate:

func loadJsonData(){

    self.view.makeToastActivity(.center) 

}

The above mentioned code does not work because app delegate has no member view...please help me to use that in my app delegate.

Benjamin Lowry
  • 3,730
  • 1
  • 23
  • 27
user7333282
  • 101
  • 1
  • 2
  • 15

3 Answers3

3

AppDelegate is for handling things like initialising the app, closing the app, notifications, etc.

What you want to do is:

  • Go to the storyboard (named Main.storyboard)
  • Add a ViewController to the storyboard (drag it from the bottom right)
  • Create a Swift file and call it FirstView, for example, and add the following code

FirstView.swift

import UIKit

class FirstView: UIViewController
{
    override func viewDidLoad()
    {
        self.view.makeToastActivity(.center);
    }
}
  • Go back to the storyboard
  • Click on the ViewController that you just created
  • Look at the top right of the screen and there will be six little icons. Click the one third from the left and type FirstView in the first field named "Class" (see attached picture).

Note: make sure you save your FirstView.swift file or this won't work.

2

How about a customized Toast instead? One that is far more alluring, suits your need and requires no libraries or complex implications?

Now let us try the following bit of code

 func  sailAwayLabelAction(){

    // here creating a rectangle with certain dimensions you can easily manipulate 
    let rect = CGRect(origin: CGPoint(x: self.view.frame.size.width/2 - 150,y :self.view.frame.size.height-100), size: CGSize(width: 300, height: 35))


//here creating and manipulating the attributes of your text, i.e color,alignment etc..
let toastLabel = UILabel(frame: rect)
toastLabel.backgroundColor = UIColor.orange
toastLabel.textColor = UIColor.white
toastLabel.textAlignment = NSTextAlignment.center;
toastLabel.text = "This is my customized Toast !"
toastLabel.layer.cornerRadius = 10;
toastLabel.clipsToBounds  =  true

//first pop the toast into our view 
self.view.addSubview(toastLabel)

//then after 1 sec + 1 sec delay, animate the entire toastLabel out.
UIView.animate(withDuration: 1, delay: 1, usingSpringWithDamping: 1, initialSpringVelocity: 1, options: .curveEaseOut, animations: {

    toastLabel.alpha = 0.0

})



}

Whenever you activate the previous function, it should render something similar to this,

enter image description here

Khaledonia
  • 2,054
  • 3
  • 15
  • 33
  • very good, the only thing is I think toast messages are usually not meant to be animated away through time. Something has to trigger it, like if your network is connected again or if your access to location is working again, etc. – mfaani Jan 06 '17 at 14:43
  • sure why not, add background listener in the appdelegate to monitor network accessibility, once network is unavailable render your personal and customized toast or even animations in my case. – Khaledonia Jan 06 '17 at 15:05
1

try this please, through this you can get top controller of your app and then you can add toast on top controller

let win:UIWindow = UIApplication.shared.delegate!.window!!
win.currentViewController()?.view
Usman Javed
  • 2,437
  • 1
  • 17
  • 26
  • i just used self.window?.currentViewController()?.view.makeToastActivity(.Center)..it works thanks – user7333282 Jan 06 '17 at 13:19
  • you have an extra `!` after window...also it's best to safely unwrap it. Perhaps this method get's called when the app is in background ie there is no window (as shown [here](http://stackoverflow.com/a/41334428/5175709)) and would crash the app... – mfaani Jan 06 '17 at 14:39