UIGestureRecognizer in iOS 3.1.3?

I am working to make an existing iPhone / iPad project backward compatible with iPhoneOS 3.0.

My current test device is the iPod Touch with 3.1.3 on it.

The following bit of code causes problems:

Class gestureRecognizer = NSClassFromString(@"UISwipeGestureRecognizer"); if (gestureRecognizer != nil) { UISwipeGestureRecognizer * leftSwipeRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(didSwipeLeft:)]; leftSwipeRecognizer.direction = UISwipeGestureRecognizerDirectionLeft; leftSwipeRecognizer.delegate = self; [self.view addGestureRecognizer:leftSwipeRecognizer]; _leftSwipeRecognizer = leftSwipeRecognizer; } 

According to Apple's documentation, UIGestureRecognizer is defined starting with iOS 3.2. Therefore, I expect that the Class gestureReconizer will be nil in the previous version of the OS and that there will be the following for skipping: However, it does not skip. gestureRecognizer not nil , the code inside if starts execution and fails with leftSwipeRecognizer.direction , because:

 -[UISwipeGestureRecognizer setDirection:]: unrecognized selector sent to instance 0x1e5720 

This situation is rather confusing. I think I'm doing everything according to the book. I am trying to check if a class exists before I use it, however a class that should not be there, fools my test, does not meet its expected specifications and crashes.

I could, of course, put a few respondsToSelector checks here and there to get around this crash, but that would not be an elegant way to do this.

Any other suggestions?

+4
source share
1 answer

According to the UIGestureRecognizer class reference, in "Special Considerations for Use" , you really need to do additional verification of the response to the request, after checking that the class exists.

This is directly from the documentation:

To determine if a class is available at runtime on a given iOS, you usually check that the class is zero. Unfortunately, this test is not purely accurate for UIGestureRecognizer. Although this class has been publicly available since iOS 3.2, it was in development a short period before. Although the class exists in an earlier release, the use of other gesture recognition classes is not supported in this earlier release. You should not try to use instances of these classes.

To determine at runtime whether you can use gesture recognizers in your application, check if the class exists and, if so, select the instance and see if it responds to the location of the InView: selector. This method was not added to the class until iOS 3.2. The code may look like this:

 UIGestureRecognizer *gestureRecognizer = [[UIGestureRecognizer alloc] initWithTarget:self action:@selector(myAction:)]; if (![gestureRecognizer respondsToSelector:@selector(locationInView:)]) { [gestureRecognizer release]; gestureRecognizer = nil; } // do something else if gestureRecognizer is nil 
+11
source

Source: https://habr.com/ru/post/1341418/


All Articles