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

Jonathan.
  • 53,997
  • 54
  • 186
  • 290
  • Do you want code for use in a shipping application or are you looking to perform UI testing? – Justin Nov 09 '10 at 19:01
  • For a shipping app if possible? – Jonathan. Nov 09 '10 at 20:29
  • 1
    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. – richcollins Dec 07 '11 at 03:07
  • PLZ If anyone can help me http://stackoverflow.com/questions/26460614/simulate-touch-on-ios7-8 – Dhekra Zaied Oct 20 '14 at 14:08
  • @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) ? – JBA May 10 '16 at 10:41
  • @JBA, to date, I have not. I haven't looked *super hard*. But, I agree. They'd be very useful to have again. :( – Nate May 10 '16 at 21:16

5 Answers5

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?

Community
  • 1
  • 1
Sylter
  • 1,622
  • 13
  • 26
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 ;)

drunknbass
  • 1,662
  • 1
  • 13
  • 19
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];
Nate
  • 12,963
  • 4
  • 59
  • 80
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.

Jorge Aguirre
  • 2,787
  • 3
  • 20
  • 27
  • 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.) – Jorge Aguirre May 07 '12 at 08:06
  • @JorgeAguirre Can you please let me know how it will work for iOS 7? For iOS 7 GSEvent API has been deprecated. – Nishith Shah May 21 '15 at 12:17
-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.

MobileOverlord
  • 4,580
  • 3
  • 22
  • 30
  • Are you sure? I have been looking into the GSEvent private API recently (https://github.com/rpetrich/iphoneheaders/blob/master/GraphicsServices/GSEvent.h) 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). – user293895 Feb 14 '12 at 23:58