2017-07-03 6 views
0

私のアプリケーションのリンクを共有するボタンがあります。私はそれのためのダイアログボックスが必要です。私はそれをたくさん検索しましたが、満足のいく解決策を見つけられませんでした。アプリケーションリンクをFacebook、Twitter、Gmailなどのさまざまなソーシャルプラットフォームにどのように共有できますか?目的のCのIOSのダイアログ

私はこのコードを使用しています:

- (void)viewDidLoad { 
    [super viewDidLoad]; 

    NSString *textToShare = @"Look at this awesome website for aspiring iOS Developers!"; 
    NSURL *myWebsite = [NSURL URLWithString:@"My URL"]; 

    NSArray *objectsToShare = @[textToShare, myWebsite]; 

    UIActivityViewController *activityVC = [[UIActivityViewController alloc] initWithActivityItems:objectsToShare applicationActivities:nil]; 

    NSArray *excludeActivities = @[UIActivityTypePostToFacebook, 
            UIActivityTypePostToTwitter, 
            UIActivityTypePostToFlickr, 
            UIActivityTypePostToVimeo]; 

    activityVC.excludedActivityTypes = excludeActivities; 

    [self presentViewController:activityVC animated:YES completion:nil]; 
    // Do any additional setup after loading the view. 
} 
+0

'viewDidLoad'からのダイアログを表示しないでください。常に 'viewDidAppear'からです。 – Sulthan

+0

大丈夫です@ Sulthan –

答えて

3

あなたはあなたのコードにドラッグInterface BuilderのキーとCtrlから直接Share Action Buttonを作成することができます。

- (IBAction)shareByFacebook:(id)sender { 

    if([SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook]) { 
     SLComposeViewController *controller = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook]; 

     [self generateMessage:controller]; 

    }else{ 
     UIAlertView* facebookAlert = [[UIAlertView alloc]initWithTitle:NSLocalizedString(@"Social.Account.FB.title", @"") message:NSLocalizedString(@"Social.Account.FB.message", @"") delegate:nil cancelButtonTitle:NSLocalizedString(@"Error.ok", @"") otherButtonTitles: nil]; 
     [facebookAlert show]; 
    } 
} 

この方法のシェア画像とFacebookに対応するテキストメッセージ:

次に、あなたがこのような何かを行うことができます。

- (IBAction)shareByTwitter:(id)sender { 
    if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeTwitter]) 
    { 
     SLComposeViewController *tweetSheet = [SLComposeViewController 
               composeViewControllerForServiceType:SLServiceTypeTwitter]; 
     [self generateMessage:tweetSheet]; 

    }else{ 
     UIAlertView* twitterAlert = [[UIAlertView alloc]initWithTitle:NSLocalizedString(@"Social.Account.Twitter.title", @"") message:NSLocalizedString(@"Social.Account.Twitter.message", @"") delegate:nil cancelButtonTitle:NSLocalizedString(@"Error.ok", @"") otherButtonTitles: nil]; 
     [twitterAlert show]; 
    } 
} 

Twitterと同じです。

は、私は、コードの重複を避けるために、一般的なgenerateMessageメソッドを作成している#import <Social/Social.h>

をインポートすることを忘れないでください。

-(void)generateMessage:(SLComposeViewController *)controller 
{ 

    if ([controller.serviceType isEqualToString:SLServiceTypeTwitter]) { 
     NSString* message = @"The message you want." 

     [controller setInitialText:message]; 
    } 

    [controller setCompletionHandler:^(SLComposeViewControllerResult result) { 
     if (result == SLComposeViewControllerResultDone) { 
      DDLogInfo(@"Posted"); 
     } else if (result == SLComposeViewControllerResultCancelled) { 
      DDLogInfo(@"Post Cancelled"); 
     } else { 
      DDLogInfo(@"Post Failed"); 
     } 
    }]; 

    [self.parentVC presentViewController:controller animated:YES completion:nil]; 
} 

これらの方法で、あなたのFacebook/TwitterとGoogleアカウントにコンテンツ(画像、写真、メッセージ..)をあなたのアプリから直接共有することができます。

NB:彼らの共有方法は、今

Share Google+ iOS

を推奨されていません。しかし、あなたは、例えばURLを共有するために、この例のように、古い方法を使用することができるため、Googleにとっては少し違います:

- (void)showGooglePlusShare:(NSURL*)shareURL { 

    // Construct the Google+ share URL 
    NSURLComponents* urlComponents = [[NSURLComponents alloc] 
             initWithString:@"https://plus.google.com/share"]; 
    urlComponents.queryItems = @[[[NSURLQueryItem alloc] 
            initWithName:@"url" 
            value:[shareURL absoluteString]]]; 
    NSURL* url = [urlComponents URL]; 

    if ([SFSafariViewController class]) { 
     // Open the URL in SFSafariViewController (iOS 9+) 
     SFSafariViewController* controller = [[SFSafariViewController alloc] 
               initWithURL:url]; 
     controller.delegate = self; 
     [self.parentVC presentViewController:controller animated:YES completion:nil]; 
    } else { 
     // Open the URL in the device's browser 
     [[UIApplication sharedApplication] openURL:url]; 
    } 
} 

EDIT:

ソーシャルネットワークに共有するには、IBActionボタンを1つしか作成できません。 そして、ユーザーはどちらを選択する必要があります。

enter image description here

とコード例:

- (IBAction)shareContentSocialNetwork:(id)sender 
{ 
    if ([UIAlertController class]){ 
     // ios 8 or higher 
     UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"" message:@"Share on Social Network" preferredStyle:UIAlertControllerStyleActionSheet]; 

     UIAlertAction* fb = [UIAlertAction actionWithTitle:@"Facebook" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) 
          { 
           if([SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook]) { 
            SLComposeViewController *controller = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook]; 
            // Create a method in order to add image, text etc.. 
            [self generateMessage:controller]; 

           }else{ 
            UIAlertView* facebookAlert = [[UIAlertView alloc]initWithTitle:NSLocalizedString(@"Social.Account.FB.title", @"") message:NSLocalizedString(@"Social.Account.FB.message", @"") delegate:nil cancelButtonTitle:NSLocalizedString(@"Error.ok", @"") otherButtonTitles: nil]; 
            [facebookAlert show]; 
           } 
          }]; 

     [alertController addAction:fb]; 

     UIAlertAction* twit = [UIAlertAction actionWithTitle:@"Twitter" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) 
           { 
            if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeTwitter]) 
            { 
             SLComposeViewController *tweetSheet = [SLComposeViewController 
                       composeViewControllerForServiceType:SLServiceTypeTwitter]; 
             // Create a method in order to add image, text etc.. 
             [self generateMessage:controller]; 

            }else{ 
             UIAlertView* twitterAlert = [[UIAlertView alloc]initWithTitle:NSLocalizedString(@"Social.Account.Twitter.title", @"") message:NSLocalizedString(@"Social.Account.Twitter.message", @"") delegate:nil cancelButtonTitle:NSLocalizedString(@"Error.ok", @"") otherButtonTitles: nil]; 
             [twitterAlert show]; 
            } 
           }]; 
     [alertController addAction:twit]; 

     UIAlertAction* ggl = [UIAlertAction actionWithTitle:@"Google+" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) 
           { 
            NSURL *url = [[NSURL alloc] initWithString:@"yourContentURL"]; 
            [self showGooglePlusShare:url]; 
           }]; 
     [alertController addAction:ggl]; 

     UIAlertAction* cancel = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:nil]; 
     [alertController addAction:cancel]; 

     [self presentViewController:alertController animated:YES completion:nil]; 

    } 
} 

は基本的に私はAlertControllerの3つの具体的なアクションを作成してい

結果はこのようになります。 TwitterとFacebookの場合は、前の手順で示したgenerateMessageメソッドを使用する必要がありますが、それはかなり簡単です。

希望します。

+0

どのようにして、さまざまなソーシャルメディアアイコンを表示して、ユーザーがそれを選択してさらに処理することができますか?@Janna –

+0

左の引き出しに共有ボタンがあり、ユーザーがボタンをクリックすると、別のソーシャルメディアオプションがアプリのリンクを共有するように表示され、ユーザーがそれを簡単に共有できるものをクリックすると表示されます。@ Janna –

+0

私の解決策は3つの異なるボタンのためですが、選択肢の選択肢の答えを編集できるようにします。 – Balanced

関連する問題