目标 C 中 IOS 中的对话

哈姆扎·伊姆兰

我有一个按钮可以分享我的应用程序的链接。我想要一个对话框。我已经搜索了很多,但没有找到令人满意的解决方案。如何将我的应用程序链接分享到不同的社交平台,例如 Facebook、Twitter 或 Gmail?

我正在使用此代码:

- (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.
}
用户7219266

你可以Share Action Button直接从 Interface Builder创建你的,然后按住 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 帐户。

注意:对于谷歌来说有点不同,因为他们的共享方法现在已被弃用

分享 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 按钮以分享到社交网络。然后用户必须选择哪一个。

结果将是这样的:

在此处输入图片说明

和代码示例:

- (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 方法。

希望能帮助到你。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章