当前webview iPhone的社交分享网址。 (Xcode)

时间:2013-08-20 11:47:03

标签: iphone xcode facebook twitter

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

单击FACEBOOK选项后,在iPhone上进行测试时,会出现标准的twiiter共享表。

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

1 个答案:

答案 0 :(得分:0)

您使用的actionSheet.tag与entiere操作表相同。 如果您想知道按下了哪个按钮,则应在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];