2017-07-03 22 views
0

我有一個按鈕來分享我的應用程序的鏈接。我想要一個對話框。我已經搜尋了很多,但沒有找到一個令人滿意的解決方案。我怎樣才能將我的應用程序鏈接分享到不同的社交平臺,如Facebook,Twitter或Gmail?IOS中的對象C

我使用這個代碼:

- (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

您可以創建Share Action Button直接從界面生成器和Ctrl拖動它到你的代碼。

然後,你可以做這樣的事情:

- (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相同。

不要忘了導入#import <Social/Social.h>

我已經爲了避免重複的代碼創建了一個通用的方法generateMessage

-(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帳戶。

注:對於谷歌這是一個有點不同,因爲他們的股票方法現在已不

Share Google+ iOS

但是你可以以共享例如一個URL中使用舊的方式,這樣的例子:

- (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]; 
    } 
} 

編輯:

您只能創建1個IBAction按鈕以便分享到社交網絡。 然後用戶必須選擇哪一個。

其結果將是這樣的:

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