开发者

How do I reference iOS 4.0 only enums in a project with a deployment target < 4.0?

开发者 https://www.devze.com 2023-04-05 22:55 出处:网络
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 t

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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消