iphone sdk: prevent alearts while running process in Background - iphone

I'm developing an app where I use performSelectorInBackground to do some sync task in the background. In my code I want to be able to detect whether I'm runing in a background process. This way I can prevent alert messages which might otherwise show up.
Thanks for your help!
Markus

By "background process", I assume you mean "background thread". To check if you're running on the main thread, try this:
// Some error handling code
if ([NSThread isMainThread]) {
// Provide some sort of UI feedback
}
else {
// Send a message to the main thread to provide feedback
}

Related

Apple Watch: WKInterfaceDevice.current().play(.success) not working

I try to let the  Watch vibrate from within my code:
WKInterfaceDevice.current().play(.success)
but nothing happens. I also tried to ensure to be on the main thread:
DispatchQueue.main.async {
WKInterfaceDevice.current().play(.success)
}
still nothing. Am I missing something here? I expected it to be an easy task, but I fail completely...
Maybe your watch app is not in the active state? Citing the official documentation:
This method has no effect when called while your shared WKExtension object's applicationState property is either background or inactive. By default, you cannot play haptic feedback in the background. The only exception are apps with an active workout session.
So, unless you have a running HKWorkoutSession, if your app is in background you're not allowed to play haptic.
Alternatively, instead of HKWorkoutSession you could use WKExtendedRuntimeSession to support running the app in the background so sounds and haptics work. This needs to be done using either the Physical therapy or Smart alarm session type.

how to upload image in background in swift using GCD

i used the following code to upload image to server in background
var queue = dispatch_get_global_queue(QOS_CLASS_BACKGROUND, 0)
dispatch_async((queue), {
self.StartUploadProcess()//upload function
})
the above code run perfectly in simulator but when i test the application on my iPad it will stop the background execution when user click home button or open other application
please help me so i can run the application even the user click on home button?
The point of dispatch_sync() is to run the code in another thread synchronously. There is also dispatch_async() which runs asynchronously, that is, in the background.
From your question I assume you already know how to run it when you want to, just need to make it into an async call.

AVAudioSession sharedInstance requestRecordPermission:^(BOOL granted)

Here is my code:
-(void) recordButton{
[[AVAudioSession sharedInstance] requestRecordPermission:^(BOOL granted) {
NSLog(#"value of the grant is :: %hhd", granted);
if (granted)
{
// perform operation recording
// perfrom some operation on UI(changing labels and all)
}
else
{
// do operation
}
}];
}
Problem is, when i run my app for the first time , after reset in the privacy and my app calls the above defined method, it creates trouble.
when My app run for the first time, allow/disallow microphones messgae(OS defined) method pops up.
when i click allow it displays the boolean(granted correclty). Goes inside IF correctly. Starts the recording correctly. but the UI freezes. and the second part of IF i.e changing label names, doesnt execute till , a timer (added by me stops the method and recording) executes.
PLease help.
I can sense that my 2nd part of the IF(changing UI label are not working in foreground), i.e. background work is working perfectly. Please Help, I am not expert. started iOS programing 2 months back.
I got the answer. my problem was, that when program reached If(granted) ,
it was performing the recording function but didn't performed on UI(changing labels and all).
The problem was, that the whole code was treated as a separate thread and was performed in background.
That's why recording was working properly(as it was a background process).
But UI(changing the labels) was a foreground task.
So For that, I had to execute the UI CODE under a thread that was on MAIN queue. And now it works perfectly.
AVAudioSession's requestRecordPermission callback is a background thread. Using code on the main thread inside a background thread causes issues (and most likely a crash).
You should call a method on the main thread to execute any post granted code. Using performSelectorOnMainThread: is an excellent way to make sure your code is running on the main thread (as explained here: execution on main thread).

How to Cancel AVExportSession when app goes to background state?

I am having an issue with AVExportSession on exporting large files. App will crash during the export when app is in background state or in locked state. How to fix this?.
Unfortunately you will need to call cancelExport on your exportSession before the app goes into background state or if you're sure it will not take too long, you can register a background task in order to finish the export, which will give your app around a minute to finish before being kicked.
At least for iOS10 or later, the AVExport Session will return a failure with AVError.operationInterrupted when you app resumes. My way of doing it is to just tear everything down and restart the export once that is detected.

Catch remote notification while application is in Background

Is there a way to catch remote push notification while the application is "running" in Background ?
I have implemented the delegate function didReceiveRemoteNotification, and would like it to catch the remote notification while the application is in Background in the new iPhone OS 4.0
The application is correctly in background, since it keep the old state it was when I press the home button. But the push notification is appearing has a popup just like it should when the application is not running...
Well you just can't... :-( Too bad Apple's hasn't think about this...