Simulate touch on iphone
Asked Answered
Y

5

11

Im trying to simulate a touch on as UIWebView, how can I programmatically fire a touch event at a certain location? (x and y coordinates) Just call touchesBegan?

Ideally I'd like to do it without any javascript hack because in the future it may not be a uiwebview

You answered 9/11, 2010 at 18:35 Comment(6)
Do you want code for use in a shipping application or are you looking to perform UI testing?Apoplexy
For a shipping app if possible?You
Did you get this working on a UIWebView? I can get it to work on other types of views but it doesn't generate DOM events in UIWebView.Shawna
PLZ If anyone can help me https://mcmap.net/q/358306/-is-possible-to-simulate-touch-event-using-an-external-keyboard-on-ios-jailbrokenGaussmeter
@Nate did you find any replacement, now at the iOS9 time, for all those old good GSEvent API no more available (since iOS7 I guess) ?Bresnahan
@JBA, to date, I have not. I haven't looked super hard. But, I agree. They'd be very useful to have again. :(Cavalierly
N
12

It's not easy to synthesize a touch event on the iPhone: you have to use undisclosed API, so you have a high probability of breaking on every update of the iOS and getting rejecting from Apple.

Here's a link that demonstrates how to synthesize a touch event on the iPhone:

Here's another question on StackOverflow: How to send a touch event to iPhone OS?

Neiman answered 9/11, 2010 at 19:2 Comment(0)
C
4

It's worth pointing out the KIF framework here. It's intended to run in the simulator but part of the code is simulating touch evens in code. with luck, this will be a good starting point.

https://github.com/square/KIF

Specifically, look at stepToTapViewWithAccessibilityLabel in KIFTestStep.m and the line

[view tapAtPoint:tappablePointInElement];
Cavalierly answered 11/11, 2011 at 22:19 Comment(0)
K
4

What you need to do is first create the events you want, and then send them to SpringBoard over the "purple port" eg. mach port. To make them system wide you must forward them to each application over the port. That means you need to actually do what the windowmanager does and looking at which app is active, screen locked, etc.

There are a hand full of private framework APIs that work (IOSurface, GraphicServices, SpringBoardServices, etc.) to get you the pieces you need.

You will have to load these private frameworks at runtime using something like dlopen().

This is 100% possible without jailbreak as of iOS 6.1.4 (current ATM), but you will be loading private frameworks which is not allowed by apple for AppStore ;)

Kriegspiel answered 19/6, 2013 at 17:39 Comment(1)
Can you please let me know how it will work for iOS 7? For iOS 7 GSEvent API has been deprecatedVive
F
1

It is possible. Exactly how you mentioned, using GSEvents and sending them to the purple named port of the aplication you are trying to control/simulate. Of course you need KennyTM's GSEvent.h to accomplish this. I've done this for iOS 4.3, just by changing some of the values that Kenny had (like kGSHandInfoTypeTouchDown), but now I'm trying to do it for iOS 5 and it's not working, till now.

EDIT: It is now working for iOS 5.1.

Fearnought answered 3/4, 2012 at 6:37 Comment(2)
I suggest you translate every UIEvent that your App receives into a GSEvent for analizing. You can even try to receive a GSEvent for a touch and then Re-send it. Then begin building your own structure and changing the fields accordingly. KennyTM's API is a good start, and it actually works with iOS 4.3 (you just have to fill in the fields values, but like I said some are constant and have no explanation whatsoever, like PathProximity, which is 2 for TouchDown events and 1 for TouchUp. PathIndex is always 2, pathMajorRadious and pathPressure always 1.0, etc.)Fearnought
@JorgeAguirre Can you please let me know how it will work for iOS 7? For iOS 7 GSEvent API has been deprecated.Vive
C
-1

Without jailbreaking there is no real way to hook a gesture recognizer into all views of the entire system. First off, your app running in the background doesn't have the ability of executing this code.

Crenellate answered 10/2, 2012 at 1:28 Comment(1)
Are you sure? I have been looking into the GSEvent private API recently (github.com/rpetrich/iphoneheaders/blob/master/GraphicsServices/…) and was able to 'press' the lock button on and off while my app was in the background, I haven't been able to simulate a touch using GSEvent regardless of whether my app is running in the background or not (I can however do Accelerometer events).Remde

© 2022 - 2024 — McMap. All rights reserved.