How to handle API requests when user locks device - ios

I am working on a Xamarin iOS project. During my testing, I have found that if I perform a delete action, and lock the device while the task is executing, the app crashes. What is the proper way to prevent this from occurring?
// DeleteItem calls the api
bool success = await ViewModel.DeleteItem(itemToDelete);
if (success)
{
ViewController.NavigationController.PopToRootViewController(true);
}
else
{
DisplayErrorMessage("An error encountered.");
}
Edit:
Upon looking at the device log, this is the error that occurred:

Related

Swift - How can I resume URLRequest after app shut down and reopened

Question
If the app crashes for some reason while sending a lot of requests, how can I resume it?
Let's assume the following situation:
for i in 0..<50 {
URLSession.shared.dataTask(with: postMessage) { data, response, error in
...
}
if i == 25 {
// App shut down
exit(0)
}
}
// App reopened
for i in 25..<50 {
URLSession.shared.dataTask(with: postMessage) { data, response, error in
...
}
}
As an example, if you send multiple messages in an app and failed, it starts sending messages again from where it failed.
How can I implement this? I tried using FileManager to cache each requests in local disk but it seems giving too many overheads to device.
Are there any effective methods I can use? I will be really appreciated if you give me some ideas.

why don't we see the ios app setting page anymore?

So I am using flutter-permission-handler to handle all the permission functionality,
for E.G
// Location Permission
askForLocation() async {
// Permission type
var status = await Permission.location.request();
if (status.isGranted) {
_location.value = status.isGranted;
update();
} else {
print('open setting');
openAppSettings();
}
}
work as expected on android but when it comes to ios it always returns permanently denied
however, with that kind of return it should open the app setting page, it opens only the home setting page on the simulator and a physical device.
my first response was to try to get the app from the setting page I couldn't find the app itself.
you can see the behaviour in this video
https://youtu.be/ugpFMFyKhLY
anyone facing the same issue?

Workaround for missing "Web Push" on Safari for my PWA

I am developing a PWA that requires Push-Notifications. Sadly IOS/Safari does not support https://w3c.github.io/push-api/#pushmanager-interface for now, so I think i might have to wrap a native APP around in some way.
In Android (before their "Trusted Web Activities" was a thing) you could use a WebView to basically display a headless Chrome-View in your App. Whats the equivalent in IOS and how does the interaction between push-notifications and the Webapp (the browser need to jump to a specific page) work?
One more thing I need is integration with our companys Mobile Device Management, which is Microsoft Intune. Having integrated MDMs in Android in the past i Know that this might be a major pain in the a**, so i'm considering to build the wrapper myself, for maximum flexibility. Another option would be something like Ionic, not sure now.
This may not necessarily work in your situation, but I had the exact same issue with a PWA for Safari and I solved it by just using long polling. It will allow you to get around all of the limitations with Safari and I was able to redirect and load sections within our SPA.
async function subscribe() {
let response = await fetch("/subscribe");
if (response.status == 502) {
// Status 502 is a connection timeout error,
// may happen when the connection was pending for too long,
// and the remote server or a proxy closed it
// let's reconnect
await subscribe();
} else if (response.status != 200) {
// An error - let's show it
showMessage(response.statusText);
// Reconnect in one second
await new Promise(resolve => setTimeout(resolve, 1000));
await subscribe();
} else {
// Get and show the message
let message = await response.text();
showMessage(message);
// Call subscribe() again to get the next message
await subscribe();
}
}
subscribe();
https://javascript.info/long-polling

iOS - Asks Passcode/TouchID after user accept the call for Video Call?

I am working with a project requires VOIP and video call. Right now, I implemented the PushKit for initialising CallKit. I would like to ask the user for Passcode/TouchID (because after the user accepts the call, it will navigate application to video call controller). However, I searched online and found non-related topics.
There are 2 cases.
1st: application is active or background but not lockscreen -> navigate to the call viewcontroller
2nd: application is background/inactive with lockscreen -> need to ask for passcode for navigation.
My current problem is: if device is locked and the user accepted the call from lockscreen -> nothing happens and my app is still in background.
Can anyone give me a hand, please ?
Below is my current code: (i add this snippet on CXCallActionAnswer)
let context = LAContext()
var err: NSError?
if context.canEvaluatePolicy(.deviceOwnerAuthentication, error: &err) {
context.evaluatePolicy(.deviceOwnerAuthentication, localizedReason: "Active Session", reply: { (good, error) in
if good {
}else {
print("Cannot auth with passcode")
}
})
}
Thanks

Repeat an asynchronous request if it fails?

I am writing an ios app that relies on being able to tell when a user is connected to wifi and, when he or she connects or disconnects, send an asynchronous request using alamo fire.
The first time I connect, my asynchronous succeeds.
However, after I first connect, any toggling of the wifi results in 404s.
I suspect this is because I am sending the request as soon as the user connects/disconnects, meaning that for a brief moment he or she has no internet service.
My question is, can I repeat the request if it fails or is it possible to "cache" the requests I want to make and wait until the user has internet connection to make them?
There are many solutions to solve this. One is to call the download method recursively again and so implementing an automatic retry mechanism on errors:
func downloadSomething() {
Alamofire.request(.GET, "https://httpbin.org/get", parameters: ["foo": "bar"])
.response { request, response, data, error in
if let error = error {
log(error)
self.downloadSomething() // recursive call to downloadSomething
} else {
// do something on success
}
}
}
You can extend this by:
showing the user also an altert view asking him if he want's to retry
the download or not before retrying the download. (depending on your
UI strategy on network errors)
a specified count of automatic re-trys and then ask the user.
checking the error status code and then depending on the code do
different network error handling strategies...
etc...
I think there is no needed to re-invented apple code like reachability or this swift reachability porting. You can able to check if a user is connected to the net or wifi very easily:
class func hasConnectivity() -> Bool {
let reachability: Reachability = Reachability.reachabilityForInternetConnection()
let networkStatus: Int = reachability.currentReachabilityStatus().rawValue
return networkStatus != 0
}
For a Wi-Fi connection:
(reachability.currentReachabilityStatus().value == ReachableViaWiFi.value)

Resources