6

I created a function which checks whether the iPhone is charging or not using UIDevice. Where should I call the function so that it monitors the status throughout the app session? The Function is called "connectivityStatus", at present it's in viewWillAppear.

Language : Swift 3
Platform : iOS 10 (Using UIDevice)

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Hari
  • 63
  • 1
  • 4
  • 2
    I bet there's a notification your could listen for to see when the charging state changes. – Daniel Storm Aug 26 '16 at 15:06
  • 1
    Please don't use any of the suggested answers. As Daniel pointed out correctly, there is a notification for this: https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIDevice_Class/#//apple_ref/c/data/UIDeviceBatteryLevelDidChangeNotification – HAS Aug 26 '16 at 16:30

4 Answers4

2

Hari please define a function in Appdelegate.h and give the defination of the function in Appdelegate.m . Now you can use this function through the app like this [[Appdelegate appdelegate] "Name of your function"]. I hope this will help. Paste the below code in Appdelegate.m inorder to use appdelegate .

+(AppDelegate*)appDelegate
{
    return (AppDelegate*)[UIApplication sharedApplication].delegate;
}

swift

class func appDelegate() -> AppDelegate {
return (UIApplication.sharedApplication().delegate as! AppDelegate)
 }
Anbu.Karthik
  • 82,064
  • 23
  • 174
  • 143
Prakash
  • 157
  • 10
2

Or you can use a Timer():

// in viewDidAppear()
connectivityStatus()
var timer = Timer.scheduledTimer(timeInterval: 2.0, target: self, selector: #selector(YourClassName.function) , userInfo: nil, repeats: true)

//outside viewDidAppear()
func function(){
    connectivityStatus()
}

This check the status every 2 seconds! Hope this Helps!

NOTE:

If you decide to update it more often, you can change the value from timeInterval: to a smaller one, but be aware that your app might get slower if a lot of processes are going on!

Mr. Xcoder
  • 4,719
  • 5
  • 26
  • 44
  • Thanks a lot! Works like a charm! – Hari Aug 26 '16 at 15:49
  • 1
    I don't suggest using the Timer at an interval of 0.5. That would call the function quite often and may slow down your app – Pranav Wadhwa Aug 26 '16 at 20:21
  • 1
    @penatheboss Exactly, using an event-based mechanism (in this case notifications) is almost always the better choice. – HAS Aug 26 '16 at 20:45
  • Using a Timer is fine, but the time interval should be at least 3 – Pranav Wadhwa Aug 26 '16 at 20:46
  • 1
    I know this is not the best practice, but as long as he wanted to update it very often and without so much code, it is fine. I will update the answer, so that the process doesn't get too messy and slow. Thanks for your comments! – Mr. Xcoder Aug 27 '16 at 06:35
0

If you want to monitor it for whole application life cycle do it in below method in AppDelegate.swift

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    // Override point for customization after application launch.
    return true
}
Omer Malik
  • 409
  • 7
  • 15
0

You can manage something like,

In your appdelegate,

  func connectivityStatus() -> Bool {
    // define your method body here. I assume return type as bool. you can customize as per your need

    return true // or false as per your requirement
}

Now in whatever viewcontroller you want to call this function you can do like,

  let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate


 appDelegate.connectivityStatus() // it will return bool as per your defination in method in appdelegate
Ketan Parmar
  • 27,092
  • 9
  • 50
  • 75