Detecting continuous/repeated shaking - ios

An app I'm writing requires the user to shake their device for X seconds.
I tried doing this via motionBegan and it works sometimes. But sometimes either motionEnded or motionCancelled get called in the middle of the shake process, and motionBegan doesn't get called again unless you stop shaking completely. And there doesn't seem to be a way to detect whether the device is currently shaking.
There are a number of apps in the App Store that do this successfully, so there's obviously something I'm missing.

Motion events are discrete: Once iOS detects the device has been shaken, it sends the corresponding event and that's that—you have no way to tell it you're interested in long or short shakes. In fact, the documentation here says:
An event is canceled if the shake motion is interrupted or if iOS determines that the motion is not valid after all—for example, if the shaking lasts too long.
If the basic shake motion events aren't adequate for your application, you'll need to implement your own custom shake detection using accelerometer data. This answer is a good place to start.

Related

ios app, running accelerometer while the app is in the background

I have an app using accelerometer. When a certain motion is detected, i am trying to make my iphone vibrate. While the app is in the foreground, and I conduct a certain motion, it vibrates. But it does not when the app is in the background.
I have a counter that detects this motion. When I execute this motion while in the background for three times, although it does not emit any vibration, after I transition back to foreground, the counter is increased by three. So I know the accelerometer is working. Or is putting the motions in a queue and executing it when the app comes back to foreground.
And I'm not making a rookie mistake by touching the phone's vibrate to off.
I'm quite not sure where to go from here. Is iphone inherently not capable of capturing and processing accelerometer data while in the background? Or is there something I'm not doing correctly?
In iOS there are only a certain type of activities allowed in background. Here you can check them: Background Modes.
I'm currently working in an app that uses sensors like the accelerometer and I get updates from it using the CoreLocation delegate method didUpdateLocation, since using the GPS to locate the device is one of the Apple allowed background modes. I hope this helps you!

UITouch Force on touchesBegan

3D touch seems really cool and I wanted to see how it would work in a musical context. I was reading a bit about 3D touch and it seems like the force property almost always reads as 0 when a touch begins (touchesBegan method).
How do I get a usable force when a touch begins because even if I'm pressing with maximum "force" I'm still getting that initial 0? I was thinking about just approximating a value within a time frame but that would involve me moving the touch. I just want a usable force upon the first touch.
Thanks!
Unfortunately this is impossible.
Every touch starts as a contact with no force at all, as soon as the user's finger makes contact (or even near-contact) with the screen. The force only starts to build as the user's finger is compressed against the screen.
The only way for touchesBegan: to report any force at all would be for it to delay artificially for a certain number of milliseconds before reporting a touch, to allow the force to build up. This would destroy the interactivity of all iOS applications; and Apple put a huge amount of work into delivering a touch the instant it is detected.

Objective C - Detect if finger is held on screen at app startup

I'd like to perform an action when the user has their finger held on the screen when my app startups.
To give an example: When the App launches and the launch screen is showing up, the user has a finger on the screen as long as the first ViewController appears. When the first ViewController gets into the viewDidAppear() function, I want to detect, that the users finger is on the screen and perform an action, like f.ex. jumping straight into the lastest received email. Basically this is supposed to be a kind of shortcut to an main action.
Is there any method to detect an already laying finger on the screen? To be exactly I'd like to check for the tap in viewDidAppear()
Unless the nature of Time has changed since the last time I checked, your app cannot detect what the user was doing before the app launched. The app, ex hypothesi, was not running at that time. And the mere presence of a finger on the screen during app launch will not generate a touch event that the app can detect.
The system can detect it, however, since is running before your app launches. That is why Apple added force-touch Shortcuts (for appropriate hardware). The only way you can do what you're asking is to rely on that API. Hardware that lacks this feature will simply have to do without this feature.
(After all, this is how Apple makes money: by trying to make users jealous of hardware they don't have, so that they buy new hardware. You would want to rob Apple of its income by reading this feature backwards onto old hardware, even if you could, now would you?)

How to create iPhone app without user operation in response to the iBeacon detection

Is it possible the following behaviors on my iPhone app ?
1) Proccess begins in background ,when detecting iBeacon wave.
2) Proccess begins with a screen, when detecting iBeacon wave, but
the application will finish automatically(without any user operations) after proseccing.
To simply put it, no. In order for your code to execute in the first place, the user needs to tap on the app. That sort of style application MAY be able to work on Android, but not on iPhone.
You CANNOT start an application from the background.
Also, when it's in the background, you CANNOT bring its UI up and obstruct what the user's trying to do.

Suitable sensors in iPhone to pick the user interaction

I'm working on an app - one of its main properties is to inform the app if the user is using the phone, interacting with it in any way, or even touching it!
What are the suitable sensors in iPhone that can help me to detect these things?
And how can I benefit from the sensors to make this property work?
Thanks
The iPhone has accelerometers, gyros, and GPS. With these, you can monitor motion of the phone, sudden shocks(like when the phone is picked up and put down), orientation, and over all motion. If outside, you can also use the GPS to pick up on motion and position (lat, long, course, speed, altitude).
When interacting with the app, you've also got touch events and multi-touch events(like using two fingers to zoom in or zoom out or rotate). Most of the 'gestures' are coded and defined by apple so you don't need to figure out the user intent, just respond to their event.
Numerous sensor monitoring apps exist... eg:
http://wavefrontlabs.com/Wavefront_Labs/Sensor_Data.html
Tutorials on how to do some of this stuff :
https://www.youtube.com/watch?v=Hml2jB_Qpds
https://www.youtube.com/watch?v=Xk5cJlhePCI
https://www.youtube.com/watch?v=qY4xCMTejH8
https://www.youtube.com/watch?v=7GHc8ySyWcY
https://developer.apple.com/library/ios/documentation/EventHandling/Conceptual/EventHandlingiPhoneOS/GestureRecognizer_basics/GestureRecognizer_basics.html

Resources