2013-08-20 36 views
-1

,所以我有我的應用程序的設置與共享當前URL到Twitter或Facebook然而,當我點擊Facebook的按鈕,它顯示了一個Twiiter份額片份額動作按鈕。我單擊Facebook選項後(Tiwtter選項正常工作)當前webview iPhone的社交分享網址。 (Xcode中)

,在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]; 
    } 
} 
} 
+0

爲什麼不使用['UIActivityViewController'(https://developer.apple.com/library/ios/documentation/uikit/reference/UIActivityViewController_Class/Reference/Reference.html)? – rckoenes

+0

@rckoenes是的,即時通訊非常新,但這聽起來像是一個更好的方式!我仍然可以使用addURL:self.newsItemWebView.request.URL行來將當前URL添加到共享表?非常感謝你! –

+0

不,你需要傳遞一些你想分享的東西。剛剛所述URL添加到一個數組並把它傳遞到'[[UIActivityViewController的alloc] initWithActivityItems:項applicationActivities:無];'其中'items'是包含您URL的陣列。 – rckoenes

回答

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