您可以創建Share Action Button
直接從界面生成器和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帳戶。
注:對於谷歌這是一個有點不同,因爲他們的股票方法現在已不
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按鈕以便分享到社交網絡。 然後用戶必須選擇哪一個。
其結果將是這樣的:
和代碼示例:
- (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方法。
希望它有幫助。
切勿顯示來自'viewDidLoad'的對話框。始終從'viewDidAppear'。 – Sulthan
好的謝謝。@ Sulthan –