iOS swift background fetch - how to check time budget remaining? - swift4

I'm looking at apple documentation, but do not see the function call to tell me how much time I have left for background fetch execution.
How do I check the time budget I have left for background fetch?
//request refresh every 90 minutes:
UIApplication.shared.setMinimumBackgroundFetchInterval(90 * 60)
func application(_ application: UIApplication,
performFetchWithCompletionHandler completionHandler:
#escaping (UIBackgroundFetchResult) -> Void) {
//how to check if I still have background execution time remaining?
}

Related

For application(_:didReceiveRemoteNotification:fetchCompletionHandler:) how to implement custom completionHandler?

Hi I'm new to Swift programming. So I have the following function:
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: #escaping (UIBackgroundFetchResult) -> Void) {
...some tasks...
// want to call completionHandler here!
}
This function runs when the phone receives a silent push notification. I was wondering if it was possible to run a custom completion handler upon didReceiveRemoteNotification method finishing?
If this is not possible, and completionHandler is some Swift specific function that is not up for change, what is its purpose? I've done some research and all I see is people simply calling completionHandler(UIBackgroundFetchResultNewData) towards the end of didReceiveRemoteNotification.
Thanks

Background Fetch working every Second in Swift 4.2

Behold. I am trying to create an IOS app with swift 4.2, which, according to my client, has to sound an alarm, but it can only be stopped with a QR code. Therefore, what I will need will be a timer that works in the background, checking the time every second, because something else, the user could stop it, without the QR code. The only thing I've found for that is the Background Fetch, but can it work with 1 second checks every time?
Does anyone have a better idea?
Cheers
This is my source code for now
App delegate:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
UIApplication.shared.setMinimumBackgroundFetchInterval(2.0)
}
//ini----------------------------background task
func application(_ application: UIApplication, performFetchWithCompletionHandler completionHandler: #escaping (UIBackgroundFetchResult) -> Void) {
// fetch data from internet now
let time:TimerNow = TimerNow();
let hours = preferences.string(forKey: "hours");
let minutes = preferences.string(forKey: "minutes");
if((hours == time.getHours())&&(minutes == time.getMinutes())){
lem.onPlayCommand();
}
completionHandler(.newData)
}
//fin ---------------------------background task
I think you should use local notifications instead. It will be fired when you want to notify about the elapsed time. You could cancel it whenever and with any condition you want.
IMO timer and background fetching is not suitable for that situation.

seconds passed since i closed an app in swift/xcode?

I need a simple method of counting the seconds between the last time i closed an app and i opened it....is there any simple way to do so?
Nothing else in the code needs changing, only number of seconds between "lastAppClosingTime' and "app Launched is needed.
I am thinking about using the 'timeserving' but I', unsure if it'll be efficient
I have a countdown in my app, requiring the user to press a button 10 minutes before the end, however i have no idea how to make the app calculate whenever the user failed the LAST check or not
You can store time when you app is to be terminated, one once the app is launched compare the last close date with current time, as such (goes to AppDelegate.swift):
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
if let date = UserDefaults.standard.object(forKey: "TerminatedAt") as? Date {
let components = Calendar.current.dateComponents([.second], from: date, to: Date())
let numberOfSecondsSinceAppClosed = components.second ?? 0
}
return true
}
func applicationWillTerminate(_ application: UIApplication) {
UserDefaults.standard.set(Date(), forKey: "TerminatedAt")
UserDefaults.standard.synchronize()
}

How to implement CallKit in real app?

I am trying to implement Apple CallKit in my VoIP application. I am using Xcode 9.2, Swift 4.0.3, iOS 10.3. Problem is I do not know how exactly to do this. I have tried to search for tutorial in Web, but I failed to find tutorial based on real system. They all using fake systems, only simulate calls! I have a class which rules with someone third person library which is responsible for the calls. So, if I have incoming call happens, some event goes through observer. Like this:
NotificationCenter.default.addObserver(forName: Notification.Name(rawValue: Notifications.Name.transMessage.rawValue), object: nil, queue: nil) { notification in
self.handleLibtransEvents()
}
where self.handleLibtransEvents is private function calling calls.
Plus, I have installed working PUSH Notification system in AppDelegate.swift. Like this:
func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
print("NOTIFICATION ERROR: \(error)")
}
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
let pushToken = deviceToken.reduce("", {$0 + String(format: "%02X", $1)}).lowercased()
print("pushToken: \(pushToken)")
}
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any], fetchCompletionHandler completionHandler: #escaping (UIBackgroundFetchResult) -> Void) {
if UIApplication.shared.applicationState != UIApplicationState.active {
// PUSH: Incoming call event!
}
}
So there are two places, I can catch incoming call but I have not any idea how to catch real call through Apple CallKit. I will be thankful for any help, advice, call example. Thanks!
P. S. I have installed all settings correctly.
CallKit is basically a framework to show up a Call UI when ever you receive a voip call or make a new call, So it will do nothing if you pass it a number n assume it will make call, Keep it in mind its only to show a UI for receive call & make call.
To make calls n receive calls you will need 3rd party services like Sinch and many other.

Sirikit and parent app communication in ios 10 : Handoff

I am using Sirikit to integrate with my payment domain app where I need to interact with the app. I read Apple documentation, they asked to use common frameworks.
Is it possible to use handoff? if yes then how?
How can I call the other viewController which is in parent app from sirikit?
I will really appreciate for any help. Thanks
Every user activity object needs a type and we need to define those types in the
info.plist of the application
For Eg.
let userActivity = NSUserActivity(activityType: "uaType")
userActivity.title = "uaTitle"
userActivity.userInfo = ["uaKey": "uaValue"]
You can now access this activity object in AppDelegate as follows
func application(_ application: UIApplication, continue userActivity: NSUserActivity, restorationHandler: #escaping ([Any]?) -> Void) -> Bool {
switch userActivity.activityType {
case "uaType":
//Write your logic to access uaKey & uaValue here
return true
default: return false
}
}
Check SiriKit Programming Guide,
To use handoff, You can create intent response object with NSUserActivity object. While creating NSUserActivity object assign userInfo property to appropriate object that you want,
let userActivity = NSUserActivity()
userActivity.userInfo = ["myKey": myAnyObject]
You will get this NSUserActivity object in parent application AppDelegate,
optional public func application(_ application: UIApplication, continue userActivity: NSUserActivity, restorationHandler: ([AnyObject]?) -> Swift.Void) -> Bool
Here you can get userInfo object which you can compare as per your requirement and call appropriate viewController.