3

I am attempting to write an app for iOS that will take advantage of iOS 4.0 features, but also work on an earlier version of the OS (3.1.3). I have set the deployment target to 3.1.3 and the Base SDK to 4.3 (latest)

Specifically, I am trying to take advantage of the ability to intercept commands from the remote control.

The document linked below is very useful in explaining how to (at run-time) check for the presence of classes and methods, but I still get a compiler error when attempting to reference an enum from the UIEvent class which only appears in iOS 4.0 and later.

https://developer.apple.com/library/archive/documentation/DeveloperTools/Conceptual/cross_development/Using/using.html#//apple_ref/doc/uid/20002000-SW3

Here is the section of code which causes the compilation to fail:

- (void)remoteControlReceivedWithEvent:(UIEvent *)receivedEvent {   
    if (receivedEvent.type == UIEventTypeRemoteControl) {
        switch (receivedEvent.subtype) {
            case UIEventSubtypeRemoteControlTogglePlayPause:
                [self playPauseAction:nil];
                break;
            case UIEventSubtypeRemoteControlPreviousTrack:
                [self previousChapter:nil];
                break;
            case UIEventSubtypeRemoteControlNextTrack:
                [self nextChapter:nil];
                break;
            default:
                break;
        }
    }
}

The compiler complains that:

error: 'UIEventTypeRemoteControl' undeclared (first use in this function)

UIEventTypeRemoteControl is an enum that isn't defined until 4.0 (from UIEvent.h)

typedef enum {
    UIEventTypeTouches,
    UIEventTypeMotion,
    UIEventTypeRemoteControl,
} UIEventType;

typedef enum {
    // available in iPhone OS 3.0
    UIEventSubtypeNone                              = 0,
    
    // for UIEventTypeMotion, available in iPhone OS 3.0
    UIEventSubtypeMotionShake                       = 1,
    
    // for UIEventTypeRemoteControl, available in iOS 4.0
    UIEventSubtypeRemoteControlPlay                 = 100,
    UIEventSubtypeRemoteControlPause                = 101,
    UIEventSubtypeRemoteControlStop                 = 102,
    UIEventSubtypeRemoteControlTogglePlayPause      = 103,
    UIEventSubtypeRemoteControlNextTrack            = 104,
    UIEventSubtypeRemoteControlPreviousTrack        = 105,
    UIEventSubtypeRemoteControlBeginSeekingBackward = 106,
    UIEventSubtypeRemoteControlEndSeekingBackward   = 107,
    UIEventSubtypeRemoteControlBeginSeekingForward  = 108,
    UIEventSubtypeRemoteControlEndSeekingForward    = 109,
} UIEventSubtype;

So how do I stop the compiler complaining about it?

Also - how do i stop the compiler warnings that someClass may not respond to someMethod (where I check at runtime if that class does actually respond to the method, before calling it.) I suppose I could turn off that warning in the compiler settings - but it's a useful warning in other cases.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Diggory
  • 635
  • 9
  • 20
  • (Answering my own question) - I've just realised that I can solve this issue by re-defining the symbols in my own code - or just use the values that they refer to instead of referencing the symbols themselves. – Diggory Sep 20 '11 at 12:57
  • Hmm - maybe not - I can use the numeric values instead of the UIEventSubtypeRemote.... items but I can't work out how to make the line `if (receivedEvent.type == UIEventTypeRemoteControl) {` work. – Diggory Sep 20 '11 at 13:47
  • I cannot reproduce the problem with a new project created with the "View-based application" template. I changed the deployment target to 3.1.3 and added a UIView subclass to this project, then added an override for the remoteControlReceivedWithEvent: method with the code you provided. It compiles fine. So there may be something else in your project configuration that is messing things up... – Pascal Bourque Sep 20 '11 at 18:28
  • This is most odd - I have just tried the same thing as you and get the same result. It appears that when the deployment_target is 3.1.3 and you compile to the simulator it compiles fine, but if you use a scheme which launches on an actual iOS device - i.e. an iPhone running iOS 4.3 then it fails to compile. – Diggory Sep 21 '11 at 13:28
  • My test project: http://download.monkeyfood.com/testRemoteControl.zip – Diggory Sep 21 '11 at 13:30

1 Answers1

3

OK - Here's what I have discovered:

  1. Switching the deployment_target to 4.3 then 3.1.3 causes the compilation errors and warnings to appear.

  2. Once they appear you can get rid of them by compiling using a simulator scheme.

  3. Once you have done that, you can compile using a real device scheme and the errors and warnings are gone.

Diggory
  • 635
  • 9
  • 20