【问题标题】:Social sharing url of current webview iPhone. ( Xcode)当前 webview iPhone 的社交分享 url。 (Xcode)
【发布时间】:2013-08-22 10:53:58
【问题描述】:

所以我的应用设置带有共享操作按钮,该按钮将当前 URL 共享到 Twitter 或 Facebook,但是当我单击 Facebook 按钮时,它会显示一个 Twiiter 共享表。 (Tiwtter 选项工作正常)

点击 FACEBOOK 选项后,在 iPhone 上进行测试时,会出现标准的推特分享表。

- (IBAction)social:(id)sender {
    UIActionSheet *share = [[UIActionSheet alloc] initWithTitle:@"Pass on the news!" delegate:self cancelButtonTitle:@"OK" destructiveButtonTitle:nil otherButtonTitles:@"Post to Twitter", @"Post to Facebook", nil];

    //You must show the action sheet for the user to see it.
    [share showInView:self.view];
}

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {

//Each button title we gave to our action sheet is given a tag starting with 0.
if (actionSheet.tag == 0) {

    //Check Twitter accessibility and at least one account is setup.
    if([SLComposeViewController isAvailableForServiceType:SLServiceTypeTwitter]) {

        SLComposeViewController *tweetSheet =[SLComposeViewController composeViewControllerForServiceType:SLServiceTypeTwitter];
[tweetSheet addURL:self.newsItemWebView.request.URL];            //This is setting the initial text for our share card.
        [tweetSheet setInitialText:@"Check out this article I found using the 'Pass'  iPhone app:,  "];

        //Brings up the little share card with the test we have pre defind.
        [self presentViewController:tweetSheet animated:YES completion:nil];

    } else {
        //This alreat tells the user that they can't use built in socal interegration and why they can't.
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Sorry" message:@"You can't send a tweet right now, make sure you have at least one Twitter account setup and your device is using iOS6 or above!." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [alertView show];
    }


} else if (actionSheet.tag == 1) {

    //Check Facebook accessibility and at least one account is setup.
    if([SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook]) {

        SLComposeViewController *facebookSheet =[SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];
        [facebookSheet addURL:self.newsItemWebView.request.URL];
        //This is setting the initial text for our share card.
        [facebookSheet setInitialText:@"Check out this article I found using the 'Pass'  iPhone app:"];

        //Brings up the little share card with the test we have pre defind.
        [self presentViewController:facebookSheet animated:YES completion:nil];

    } else {
        //This alreat tells the user that they can't use built in socal interegration and why they can't.
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Sorry" message:@"You can't post a Facebook post right now, make sure you have at least one Facebook account setup and your device is using iOS6 or above!." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [alertView show];
    }   
}
}

【问题讨论】:

  • 为什么不使用UIActivityViewController
  • @rckoenes 是的,我对此很陌生,但这听起来像是一种更好的方法!我仍然可以使用 addURL:self.newsItemWebView.request.URL 行将当前 URL 添加到共享表吗?非常感谢朋友!
  • 不,你需要传递一些你想分享的对象。只需将 URL 添加到数组并将其传递给 [[UIActivityViewController alloc] initWithActivityItems:items applicationActivities:nil];,其中 items 是包含您的 URL 的数组。
  • @rckoens 非常感谢,我会在我回到电脑前试一试,然后标记为已回答! (:出于兴趣,你知道为什么它不能使用我的方法吗?它仍然困扰着我。
  • 您在 if 语句中使用的是 actionSheet.tag 而不是 buttonIndex

标签: iphone xcode facebook twitter


【解决方案1】:

您正在使用actionSheet.tag,这对于整个操作表都是相同的。 如果您想知道按下了哪个按钮,您应该在 if 语句中使用 buttonIndex

更好的是使用UIActivityViewController

NSArray *items = @[self.newsItemWebView.request.URL];
UIActivityViewController *activityViewController = [[UIActivityViewController alloc] initWithActivityItems:items applicationActivities:nil];
activityViewController.completionHandler = ^(NSString *activityType, BOOL completed) {
    // do any thing you want when the user finishes the share activity.
    // Like present a message that that activity has completed or not.
};

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

【讨论】:

    猜你喜欢
    • 2012-08-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-08
    • 2013-02-08
    • 2017-06-07
    • 1970-01-01
    • 2012-07-29
    相关资源
    最近更新 更多