ios - Objective-C 中 IOS 中的对话框

标签 ios objective-c

我有一个按钮可以分享我的应用程序的链接。我想要一个对话框。我已经搜索了很多,但没有找到令人满意的解决方案。如何将我的应用程序链接分享到不同的社交平台,例如 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.
}

最佳答案

您可以创建自己的 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 帐户。

N.B:对于 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 方法。

希望对您有所帮助。

关于ios - Objective-C 中 IOS 中的对话框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44888259/

相关文章:

ios - 使用正则表达式在 Swift 中搜索 Twitter 句柄的字符串

objective-c - 最佳实践? - 数组/字典作为核心数据实体属性

ios - NSURLConnection的困惑sendAsynchronous:request:queue:completionHandler和sendSynchronousRequest:returningResponse:error:不起作用

iphone - 我的 UIView 上已经有渐变,插入新渐变时遇到问题

iOS - 如何将事件添加到 Objective C 中的 kal 库?

ios - 模块名称前缀应该已被 native 端剥离,但不适用于 RCTJSCExecutor

ios - App Delegate 的托管对象上下文为零

ios - 将 iPad 中显示的两个 Controller 分开显示在同一屏幕中以在 iPhone 中使用

ios - 当点击以编程方式创建的 UIButton 时,是否可以推送到可在界面生成器中编辑的 Controller ?

ios - 如何在后台的另一个插件的回调中使用插件?