我有一个按钮可以分享我的应用程序的链接。我想要一个对话框。我已经搜索了很多,但没有找到令人满意的解决方案。如何将我的应用程序链接分享到不同的社交平台,例如 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 来说有点不同,因为他们的共享方法现在已被弃用
但是您可以使用旧方法,例如为了共享 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 方法。
希望对您有所帮助。
关于ios - Objective-C 中 IOS 中的对话框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44888259/