问题
I have been looking around for a solution and found only specific apps have background capabilities i.e. locational data and playing music. Assuming we have this permission, is there any way to record the swipe amount from all other apps and home screen, etc? The end goal is to record (in total) how far a user has scrolled using Swift.
So far, I have tested in app and can record a running total. However, is it possible to achieve this even when the app is inactive?
import UIKit
//test variable for storing scoll amount
var counter2 : Int = 0;
class ViewController: UIViewController {
@IBOutlet weak var sCounter: UILabel!
@IBOutlet weak var sMeasure: UILabel!
@IBOutlet var scrollMeasuer: UIPanGestureRecognizer!
@IBAction func scrollCounter (recognizer : UIPanGestureRecognizer) {
//var counter : Int = 0
let translation = recognizer.translationInView(self.view)
sMeasure.text = "\(translation)"
counter2 = abs(Int(abs(translation.x) + abs(translation.y)) + counter2)
sCounter.text = "\(counter2)"
}
}
回答1:
No you cannot do this.
When in the background your views are not displayed and so swipes will not be recorded by them. You cannot access another apps views.
Only specific tasks can be performed in the background, see Background Execution.
You cannot guarantee that your app will remain in the background state - it may be terminated at any time by iOS.
From another angle: an app that did this would be using processor time and memory in a way the user may not expect even though they had dismissed it, which is not a well behaved app. When your app us dismissed by the user it needs to stop or at least minimise processor and memory usage and Apple and iOS will enforce this.
回答2:
No, you cannot do this. And that's good.
If you could capture the touch inputs of other apps you could also capture keyboard presses, which means: private messages of other chat apps, password input, etc...
来源:https://stackoverflow.com/questions/36382557/record-swipe-amount-when-app-is-in-the-background-ios-using-swift