Robert Mao
Robert Mao

Reputation: 1919

Check if an overloading method exist in device iOS platform

There are 2 "writeImageToSavedPhotosAlbum" methods in ALAssetsLibrary Class:

- (void)writeImageToSavedPhotosAlbum:(CGImageRef)imageRef 
                            metadata:(NSDictionary *)metadata 
                     completionBlock:(ALAssetsLibraryWriteImageCompletionBlock)completionBlock  

(available on iOS 4.1+)

- (void)writeImageToSavedPhotosAlbum:(CGImageRef)imageRef 
                         orientation:(ALAssetOrientation)orientation
                     completionBlock:(ALAssetsLibraryWriteImageCompletionBlock)completionBlock

(available on iOS 4.0+)

I am using the 1st one (need iOS 4.1) in my code and it will crash on iOS 4.0 device. I am trying to use respondsToSelector to check which method is supported, however looks like the selector only check the method name, not the parameters.

I read some suggestions and feel it might not good by purely check on OS version, so is there anything similar to respondstoselector that can help me solve this problem?

Upvotes: 3

Views: 839

Answers (3)

Yuji
Yuji

Reputation: 34195

You misunderstand the Objective-C method naming system. The selector is the combination of all foo:bar:baz: combined. So, in this case, there is no method called writeImageToSavedPhotosAlbum. The first one is, as a selector, corresponds to

@selector(writeImageToSavedPhotosAlbum:metadata:completionBlock:)

and the second one is

@selector(writeImageToSavedPhotosAlbum:orientation:completionBlock:)

In your code, check whether the first selector is available or not, as in

if([obj respondsToSelector:@selector(writeImageToSavedPhotosAlbum:metadata:completionBlock:)]){
      ....
}

This should distinguish whether the first one is available or not.

Upvotes: 7

jtbandes
jtbandes

Reputation: 118761

These methods have different names, so you can test them separately.

if ([assetsLibrary respondsToSelector:
     @selector(writeImageToSavedPhotosAlbum:metadata:completionBlock:)]) {
    // Now you can safely use this method.
}

If you wanted to test the other one you would use @selector(writeImageToSavedPhotosAlbum:orientation:completionBlock:).

Upvotes: 7

Rahul Vyas
Rahul Vyas

Reputation: 28740

You can then differentiate them with os version. How about it?

Upvotes: 0

Related Questions