Where should i put the function?
Asked Answered
V

4

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)

Vicenary answered 26/8, 2016 at 13:39 Comment(2)
I bet there's a notification your could listen for to see when the charging state changes.Morie
Please don't use any of the suggested answers. As Daniel pointed out correctly, there is a notification for this: developer.apple.com/library/ios/documentation/UIKit/Reference/…Acetify
B
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!

Blent answered 26/8, 2016 at 14:0 Comment(5)
Thanks a lot! Works like a charm!Vicenary
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 appSmaze
@penatheboss Exactly, using an event-based mechanism (in this case notifications) is almost always the better choice.Acetify
Using a Timer is fine, but the time interval should be at least 3Smaze
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!Blent
K
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)
 }
Kebab answered 26/8, 2016 at 13:46 Comment(3)
Thanks, A Swift version please!Vicenary
@Vicenary - check the answerJessalin
@xxxy2j - why you said like, beileve yourself, you need to know your value, never ask like Pls accept this answers , it is depend up on questionerJessalin
B
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!

Blent answered 26/8, 2016 at 14:0 Comment(5)
Thanks a lot! Works like a charm!Vicenary
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 appSmaze
@penatheboss Exactly, using an event-based mechanism (in this case notifications) is almost always the better choice.Acetify
Using a Timer is fine, but the time interval should be at least 3Smaze
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!Blent
M
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
}
Malraux answered 26/8, 2016 at 13:53 Comment(0)
C
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
Carbonous answered 26/8, 2016 at 14:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.