2012-12-05 10 views
5

UIActivityViewControllerはFacebook/Twitter/Emailでテキストと画像を共有しますが、iOS 6以降でのみ動作します。デバイスで実行中のiOSバージョンを取得し、 iOS 5のクラッシュを避けるためのIF Statement? iOS6の場合は、次のコードを実行します。iOS 6なら[これを行う]そうでなければiOS 5はそれをする? - UIActivityViewController

-(void)shareMenu 
{ 
    NSString *textToShare = @"Text that will be shared"; 
    UIImage *imageToShare = [UIImage imageNamed:@"share_picture.png"]; 
    NSArray *itemsToShare = [[NSArray alloc] initWithObjects:textToShare, imageToShare, nil]; 
    UIActivityViewController *activityVC = [[UIActivityViewController alloc] initWithActivityItems:itemsToShare applicationActivities:nil]; 
    activityVC.excludedActivityTypes = [[NSArray alloc] initWithObjects: UIActivityTypePrint, UIActivityTypeCopyToPasteboard, UIActivityTypeAssignToContact, UIActivityTypeSaveToCameraRoll, UIActivityTypeMessage, UIActivityTypePostToWeibo, nil]; 
    [self presentViewController:activityVC animated:YES completion:nil]; 
} 
+0

(ないこの特定の場合)私はそれは私がそれをサポートしているどのような方法に基づいて、扱ってるのiOSクラスのバージョンを確認するために、 'respondsToSelector'を使用するのがベストです見つけます。とりわけ、これは特定の依存関係を文書化するのに役立ちます。 –

答えて

25
if([UIActivityViewController class]) { 
    NSString *textToShare = @"Text that will be shared"; 
    UIImage *imageToShare = [UIImage imageNamed:@"share_picture.png"]; 
    NSArray *itemsToShare = [[NSArray alloc] initWithObjects:textToShare, imageToShare, nil]; 
    UIActivityViewController *activityVC = [[UIActivityViewController alloc] initWithActivityItems:itemsToShare applicationActivities:nil]; 
    activityVC.excludedActivityTypes = [[NSArray alloc] initWithObjects: UIActivityTypePrint, UIActivityTypeCopyToPasteboard, UIActivityTypeAssignToContact, UIActivityTypeSaveToCameraRoll, UIActivityTypeMessage, UIActivityTypePostToWeibo, nil]; 
    [self presentViewController:activityVC animated:YES completion:nil]; 
} 

またはそれ以上使用

if(NSClassFromString (@"UIActivityViewController")) { 

} 

読むthis

+2

これは、iOSのバージョン番号を明示的にテストするよりも優れた解決策です。 – vcsjones

+0

これは100%ありがとうございます!しかし、** UIBarButtonItem **を** NavigationBar **で非表示にするにはどうすればいいですか? iOS5ではクリック可能だが機能はないので、非表示にしたいご回答有難うございます! – emotality

+0

http://stackoverflow.com/questions/10021748/how-do-i-show-hide-a-uibarbuttonitem – Max

0

あなたは、現在のオペレーティングシステムのための文字列を返します[[UIDevice currentDevice] systemVersion]を使用して検討すべきです。バージョンが少なくとも指定された数であるかどうかを確認するためのマクロを簡単に書くことができます。

+4

これは良い考えではありません。希望するクラスの存在を確認する方がはるかに優れています。それは、バージョン番号に頼るよりはるかにクリーンで直接的です。 – rmaddy

+0

あなたの答えをありがとう! :) – emotality

+0

システムバージョンのテストには利点があります。テストを実際に削除できるとき(つまり、iOS 5サポートを削除するとき)は、非常に明確に文書化されています。 – nschum

4

これを行うにはいくつかの方法がありますが、最も簡単なのはおそらくこのクラスの存在をテストすることです。

if ([UIActivityViewController class]) 
    //iOS 6+ 
else 
    // < iOS 6 
一般に
+0

あなたの答えをありがとう! :) – emotality

関連する問題