Wait for function to end in Swift - ios

I have a function to get data with json and i append all the data to an array. I try to create semaphore and wait until sending a signal to semaphore to continue but it doesn't work(I'm not sure if i do it correct or not), then i saw a question in Stackoverflow, answer was creating a completion handler like that
func application(application: UIApplication!, performFetchWithCompletionHandler completionHandler: ((UIBackgroundFetchResult) -> Void)!) {
getUrunGrup(completionHandler)
}
so i changed my function like that
func getUrunGrup(completionHandler: ((UIBackgroundFetchResult) -> Void)!){
Alamofire.request(.GET, "http://213.136.86.160:27701/Thunder/DataService/GetUrunGrup")
.responseJSON {(request, response, jsonObj, error) in
if let jsonresult:NSDictionary = jsonObj as? NSDictionary{
if let result: AnyObject = jsonresult["Result"] {
let elementCount = result.count
for (var i = 0; i<elementCount; ++i){
if let name: AnyObject = result[i]["Adi"]!{
if let kod:AnyObject = result[i]["Kod"]!{
urunUstGrup.append(["Adi": "\(name)", "Kod": "\(kod)"])
println("getUrunGrup \(i)")
}
}
}
}
}
}
completionHandler(UIBackgroundFetchResult.NewData)
println("Background Fetch Complete")
}
But there is no answer for how should i call this function?

you have to pass your async function the handler to call later on,like this:
func application(application: UIApplication!, performFetchWithCompletionHandler completionHandler: ((UIBackgroundFetchResult) -> Void)!) {
loadShows(completionHandler)
}
func loadShows(completionHandler: ((UIBackgroundFetchResult) -> Void)!) {
//....
//DO IT
//....
completionHandler(UIBackgroundFetchResult.NewData)
println("Background Fetch Complete")
}
OR (cleaner way IMHO)
add an intermediate completionHandler
func application(application: UIApplication!, performFetchWithCompletionHandler completionHandler: ((UIBackgroundFetchResult) -> Void)!) {
loadShows() {
completionHandler(UIBackgroundFetchResult.NewData)
println("Background Fetch Complete")
}
}
func loadShows(completionHandler: (() -> Void)!) {
//....
//DO IT
//....
completionHandler()
}

Related

SiriKit Media Intents

I am trying to implement media intents(INPlayMediaIntentHandling), but nothing seems to work, the handlers are not being called nothing is executing, my aim is to capture what is being said to Siri ex: - Hey Siri, play Hero in MyApp
This is my intents handler -
class IntentHandler: INExtension, INPlayMediaIntentHandling {
func handle(intent: INPlayMediaIntent, completion: #escaping (INPlayMediaIntentResponse) -> Void) {
if let identifier = intent.mediaSearch?.mediaIdentifier {
print(identifier)
}
print("Aloha")
completion(INPlayMediaIntentResponse(code: .continueInApp, userActivity: nil))
}
func resolveMediaItems(for intent: INPlayMediaIntent, with completion: #escaping ([INPlayMediaMediaItemResolutionResult]) -> Void) {
if let identifier = intent.mediaSearch?.mediaIdentifier {
print(identifier)
}
print("Aloha")
completion([INPlayMediaMediaItemResolutionResult.unsupported()])
}
}
I do have this method in AppDelegete -
func application(_ application: UIApplication, handle intent: INIntent, completionHandler: #escaping (INIntentResponse) -> Void) {
guard let playMediaIntent = intent as? INPlayMediaIntent else {
completionHandler(INPlayMediaIntentResponse(code: .failure, userActivity: nil))
return
}
print("Print")
print(playMediaIntent.mediaSearch?.mediaIdentifier ?? "Print")
}
not sure what am doing wrong, any help is appreciated!!

REST request from iOS background URLSession using APNs

Update 2018-05-25:
I replaced datatask with downloadTask after reading Rob's answer here: https://stackoverflow.com/a/44140059/4666760 . It still does not work when the app is backgrounded.
Hello
I need some help with iOS background tasks. I want to use Apple Push Notification service (APNs) to wake up my app in the background so that it can do a simple RESTful API call to my server. I am able to make it work when the app is in the foreground, but not in the background. I think I do something wrong with the configuration of the URLSession, but I don't know. The entire code for the app and the server is at my repo linked below. Please, clone it and do whatever you like - I just want your help :)
https://github.com/knutvalen/ping
In AppDelegate.swift the app listen for remote notifications:
class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate {
// MARK: - Properties
var window: UIWindow?
// MARK: - Private functions
private func registerForPushNotifications() {
UNUserNotificationCenter.current().delegate = self
UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge]) { (granted, error) in
guard granted else { return }
DispatchQueue.main.async {
UIApplication.shared.registerForRemoteNotifications()
}
}
}
// MARK: - Delegate functions
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
Login.shared.username = "foo"
registerForPushNotifications()
return true
}
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
let tokenParts = deviceToken.map { data -> String in
return String(format: "%02.2hhx", data)
}
let token = tokenParts.joined()
os_log("AppDelegate application(_:didRegisterForRemoteNotificationsWithDeviceToken:) token: %#", token)
}
func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
os_log("AppDelegate application(_:didFailToRegisterForRemoteNotificationsWithError:) error: %#", error.localizedDescription)
}
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: #escaping (UIBackgroundFetchResult) -> Void) {
os_log("AppDelegate application(_:didReceiveRemoteNotification:fetchCompletionHandler:)")
if let aps = userInfo["aps"] as? [String: AnyObject] {
if aps["content-available"] as? Int == 1 {
RestController.shared.onPing = { () in
RestController.shared.onPing = nil
completionHandler(.newData)
os_log("AppDelegate onPing")
}
RestController.shared.pingBackground(login: Login.shared)
// RestController.shared.pingForeground(login: Login.shared)
}
}
}
func application(_ application: UIApplication, handleEventsForBackgroundURLSession identifier: String, completionHandler: #escaping () -> Void) {
RestController.shared.backgroundSessionCompletionHandler = completionHandler
}
}
The RestController.swift handles URLSessions with background configurations:
class RestController: NSObject, URLSessionDelegate, URLSessionTaskDelegate, URLSessionDownloadDelegate {
// MARK: - Properties
static let shared = RestController()
let identifier = "no.qassql.ping.background"
let ip = "http://123.456.7.89:3000"
var backgroundUrlSession: URLSession?
var backgroundSessionCompletionHandler: (() -> Void)?
var onPing: (() -> ())?
// MARK: - Initialization
override init() {
super.init()
let configuration = URLSessionConfiguration.background(withIdentifier: identifier)
configuration.isDiscretionary = false
configuration.sessionSendsLaunchEvents = true
backgroundUrlSession = URLSession(configuration: configuration, delegate: self, delegateQueue: nil)
}
// MARK: - Delegate functions
func urlSessionDidFinishEvents(forBackgroundURLSession session: URLSession) {
DispatchQueue.main.async {
if let completionHandler = self.backgroundSessionCompletionHandler {
self.backgroundSessionCompletionHandler = nil
completionHandler()
}
}
}
func urlSession(_ session: URLSession, task: URLSessionTask, didCompleteWithError error: Error?) {
if let error = error {
os_log("RestController urlSession(_:task:didCompleteWithError:) error: %#", error.localizedDescription)
}
}
func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didFinishDownloadingTo location: URL) {
do {
let data = try Data(contentsOf: location)
let respopnse = downloadTask.response
let error = downloadTask.error
self.completionHandler(data: data, response: respopnse, error: error)
} catch {
os_log("RestController urlSession(_:downloadTask:didFinishDownloadingTo:) error: %#", error.localizedDescription)
}
}
// MARK: - Private functions
private func completionHandler(data: Data?, response: URLResponse?, error: Error?) {
guard let data = data else { return }
if let okResponse = OkResponse.deSerialize(data: data) {
if okResponse.message == ("ping_" + Login.shared.username) {
RestController.shared.onPing?()
}
}
}
// MARK: - Public functions
func pingBackground(login: Login) {
guard let url = URL(string: ip + "/ping") else { return }
var request = URLRequest(url: url, cachePolicy: .reloadIgnoringCacheData, timeoutInterval: 20)
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
request.httpMethod = "POST"
request.httpBody = login.serialize()
if let backgroundUrlSession = backgroundUrlSession {
backgroundUrlSession.downloadTask(with: request).resume()
}
}
func pingForeground(login: Login) {
guard let url = URL(string: ip + "/ping") else { return }
var request = URLRequest(url: url)
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
request.httpMethod = "POST"
request.httpBody = login.serialize()
URLSession.shared.dataTask(with: request) { (data, response, error) in
return self.completionHandler(data: data, response: response, error: error)
}.resume()
}
}
By adding App provides Voice over IP services as Required Background Mode in info.plist and using PushKit to handle the APNs payloads I were able to do what I wanted. A SSCCE (example) is available at my repository:
https://github.com/knutvalen/ping

invoke lambda function in iOS appDelegate didReceiveRemoteNotification when app is in background

I am trying to fetch data by invoking a lambda function in response to a remote push notification while my app is in the background. My notifications are configured correctly and the didReceiveRemoteNotification is called while the app is in the background.
I have the following code in that method:
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: #escaping (UIBackgroundFetchResult) -> Void) {
let lambdaInvoker = AWSLambdaInvoker.default()
lambdaInvoker.invokeFunction("lambdaFunctionName", jsonObject: jsonObject).continueWith(block: {(task:AWSTask<AnyObject>) -> Any? in
if let error = task.error as NSError? {
print(task.error!.localizedDescription)
print(task.error!)
DispatchQueue.main.async(execute: {
if (error.domain == AWSLambdaInvokerErrorDomain) && (AWSLambdaInvokerErrorType.functionError == AWSLambdaInvokerErrorType(rawValue: error.code)) {
print("Function error: \(String(describing: error.userInfo[AWSLambdaInvokerFunctionErrorKey]))")
} else {
print("Error: \(error)")
}
})
return nil
}
// Handle response in task.result
DispatchQueue.main.async(execute: {
if let jsonArray = task.result as? NSArray {
// do stuff
}
})
return nil
})
}
However the block is not executed in the lambda function. I have not used background fetch before and not sure how to make this work with a lambda function.
What I was missing was the completion handler. After adding that, the code and block executes as expected:
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: #escaping (UIBackgroundFetchResult) -> Void) {
let lambdaInvoker = AWSLambdaInvoker.default()
lambdaInvoker.invokeFunction("lambdaFunctionName", jsonObject: jsonObject).continueWith(block: {(task:AWSTask<AnyObject>) -> Any? in
if let error = task.error as NSError? {
print(task.error!.localizedDescription)
print(task.error!)
DispatchQueue.main.async(execute: {
if (error.domain == AWSLambdaInvokerErrorDomain) && (AWSLambdaInvokerErrorType.functionError == AWSLambdaInvokerErrorType(rawValue: error.code)) {
print("Function error: \(String(describing: error.userInfo[AWSLambdaInvokerFunctionErrorKey]))")
completionHandler(UIBackgroundFetchResult.newData)
} else {
print("Error: \(error)")
completionHandler(UIBackgroundFetchResult.newData)
}
})
return nil
}
// Handle response in task.result
DispatchQueue.main.async(execute: {
if let jsonArray = task.result as? NSArray {
// do stuff
completionHandler(UIBackgroundFetchResult.newData)
}
})
return nil
})
}

How to configure Background App Refresh using Swift?

I have following function to download JSON data in my SeachVC (UIViewController) which works perfect.
func downloadJSON(){
guard let url = URL(string: "myURL") else { return }
URLSession.shared.dataTask(with: url) { (data, response, err) in
guard let data = data else { return }
do {
let downloadedCurrencies = try JSONDecoder().decode([Currency].self, from: data)
// Adding downloaded data into Local Array
Currencies = downloadedCurrencies
} catch let jsonErr {
print("Here! Error serializing json", jsonErr)
}
}.resume()
}
To implement Background App Refresh, I added following functions into App Delegate;
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
// Background App Refresh Config
UIApplication.shared.setMinimumBackgroundFetchInterval(UIApplicationBackgroundFetchIntervalMinimum)
return true
}
func application(_ application: UIApplication, performFetchWithCompletionHandler completionHandler: #escaping (UIBackgroundFetchResult) -> Void) {
if let VC = window?.rootViewController as? SearchVC {
// Update JSON data
VC.downloadJSON()
completionHandler(.newData)
}
}
However, when I simulate Background App Refresh on the simulator, I get warning:
Warning: Application delegate received call to -application:performFetchWithCompletionHandler: but the completion handler was never called.
Where I am going to implement completion handler and how?
Thank you
You will need to move your downloading code from the view controller and into another class or at least modify you current background refresh method to instantiate the view controller if required. Background refresh can be triggered when your app hasn't been launched in the foreground, so the if let will fall through.
Consider the code in your question:
func application(_ application: UIApplication, performFetchWithCompletionHandler completionHandler: #escaping (UIBackgroundFetchResult) -> Void) {
if let VC = window?.rootViewController as? SearchVC {
// Update JSON data
VC.downloadJSON()
completionHandler(.newData)
}
}
If the if let... doesn't pass then you exit from the function without calling the completionHandler, so you get the runtime warning that the completion handler was not called.
You could modify your code to include a call to the completionHandler in an else case, but in this case no fetch will have taken place:
func application(_ application: UIApplication, performFetchWithCompletionHandler completionHandler: #escaping (UIBackgroundFetchResult) -> Void) {
if let VC = window?.rootViewController as? SearchVC {
// Update JSON data
VC.downloadJSON()
completionHandler(.newData)
} else {
completionHandler(.noData)
}
Or you could instantiate the view controller (or I would suggest another data fetching class) if required:
func application(_ application: UIApplication, performFetchWithCompletionHandler completionHandler: #escaping (UIBackgroundFetchResult) -> Void) {
let vc = (window?.rootViewController as? SearchVC) ?? SearchVC()
// Update JSON data
vc.downloadJSON()
completionHandler(.newData)
}
You should also modify your downloadJSON function to include a completion handler argument, which you invoke when the JSON download is complete. This will let you call the background fetch completion handler once you have actually downloaded the data:
func downloadJSON(completion: ((Bool,Error?) -> Void )? = nil)) {
guard let url = URL(string: "myURL") else {
completion?(false, nil)
return
}
URLSession.shared.dataTask(with: url) { (data, response, err) in
guard nil == err else {
completion?(false, err)
return
}
guard let data = data else {
completion?(false, nil)
return
}
do {
let downloadedCurrencies = try JSONDecoder().decode([Currency].self, from: data)
// Adding downloaded data into Local Array
Currencies = downloadedCurrencies
completion(true,nil)
} catch let jsonErr {
print("Here! Error serializing json", jsonErr)
completion?(false,jsonErr)
}
}.resume()
}
func application(_ application: UIApplication, performFetchWithCompletionHandler completionHandler: #escaping (UIBackgroundFetchResult) -> Void) {
let vc = (window?.rootViewController as? SearchVC) ?? SearchVC()
// Update JSON data
vc.downloadJSON() { (newData,error) in
if let err = error {
NSLog("Background fetch error: \(err.localizedDescription)")
completionHandler(.fail)
} else {
completionHandler(newData ? .newData:.noData)
}
}
}
Update September 2019
Note that iOS 13 introduces new background fetch and processing functionality. Refer to this WWDC session for more details
It's propably because you don't call the completionHandler at the else-case (which will never happen but the compiler doesn't know)
func application(_ application: UIApplication, performFetchWithCompletionHandler completionHandler: #escaping (UIBackgroundFetchResult) -> Void) {
if let VC = window?.rootViewController as? SearchVC {
// Update JSON data
VC.downloadJSON()
completionHandler(.newData)
} else {
completionHandler(.failed)
}
}

How to set value to Firebase Realtime Database in background?

I call setValue() in Background like this:
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: #escaping (UIBackgroundFetchResult) -> Void) {
let semaphore: DispatchSemaphore = DispatchSemaphore(value: 0)
let ref = FIRDatabase.database().reference().child("data")
let data: NSDictionary = [
"test" : "test"
]
ref.setValue(data, withCompletionBlock:{
(error, reference) in
if error != nil {
print(error!)
} else {
print("success")
semaphore.signal()
}
})
_ = semaphore.wait(timeout: .distantFuture)
completionHandler(.newData)
}
But completion block never called. Is it impossible to upload data in background?
Apple allows few of the features to be called in background
microphone
location updates
bluetooth accessory
Voip
keepSynced(true) did the trick for me, just add to your code
let semaphore: DispatchSemaphore = DispatchSemaphore(value: 0)
let ref = FIRDatabase.database().reference().child("data")
let data: NSDictionary = [
"test" : "test"
]
ref.keepSynced(true)
ref.setValue(data, withCompletionBlock:{
(error, reference) in
if error != nil {
print(error!)
} else {
print("success")
semaphore.signal()
}
})
_ = semaphore.wait(timeout: .distantFuture)
completionHandler(.newData)

Resources