简体   繁体   English

如何通过我的iOS应用在Facebook上分享内容?

[英]How to share something on facebook from my iOS app?

I'm adding share functionalities to my iOS app (I have to support down to iOS 4.3). 我正在向iOS应用程序添加共享功能(我必须支持iOS 4.3)。 In the Facebook case, the behavior I want to implement is: 在Facebook情况下,我要实现的行为是:

if the facebook app is present on the iDevice then open it and go to share view with everything already pre-filled 如果iApp上存在facebook应用,则将其打开并与已预先填充的所有内容共享视图

else open www.facebook.com/sharer.php?[etc] in safari (or whatever browser) to share my page. 否则在Safari(或其他浏览器)中打开www.facebook.com/sharer.php?[etc]共享我的页面。

I wrote this : 我写了这个:

    NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"fb://publish/profile/me?text=%@", facebookShareLink]];

    if([[UIApplication sharedApplication] canOpenURL:url]) {

        [[UIApplication sharedApplication] openURL:url];

    } else {

        NSString *facebookLink = [NSString stringWithFormat:@"http://www.facebook.com/sharer.php?u=%@&t=%@", facebookShareLink, titleToShare];
        facebookLink = [facebookLink stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:facebookLink]];
    }

When the Facebook app is NOT installed it works pretty well but when it's installed then it opens but it won't go to the share view. 如果未安装Facebook应用程序,则效果很好,但安装后便会打开,但不会进入共享视图。

I read somewhere that the URL schemes are not supported anymore (I don't have the source). 我在某处读到不再支持URL方案(我没有源)。 Is that true? 真的吗?

Then is there still a way to do this without integrating the Facebook iOS SDK? 那还有没有集成Facebook iOS SDK的方法吗? I don't want to add this SDK, I just need the sharing functionality not all the Graph stuff. 我不想添加此SDK,我只需要共享功能,而不是所有Graph的东西。

EDIT: I don't want my app to share itself sth on a social media but to prompt the user and give him the ability to do it himself easily. 编辑:我不希望我的应用在社交媒体上分享自己的东西,而是提示用户并让他能够自己轻松地进行操作。 I think I shouldn't have to authenticate with a Facebook app or a Twitter app in order to do that, should I? 我认为我不必通过Facebook应用程序或Twitter应用程序进行身份验证,对吗? The user will authenticate himself in the app or on the mobile website. 用户将在应用程序或移动网站上对自己进行身份验证。

I finally made a solution using the iOS built-in facebook share dialog (SLComposeViewController) that falls back on the website if the user is not using iOS 6 我终于使用iOS内置的Facebook共享对话框(SLComposeViewController)提出了一个解决方案,如果用户未使用iOS 6,该对话框会退回到网站上

NSString *facebookShareLink = @"some link to share";
NSString *titleToShare = @"some title";

// if we're not on iOS 6, SLComposeViewController won't be available. Then fallback on website.
Class composeViewControllerClass = [SLComposeViewController class];

if(composeViewControllerClass == nil || ![composeViewControllerClass isAvailableForServiceType:SLServiceTypeFacebook]) {

    NSString *facebookLink = [NSString stringWithFormat:@"http://www.facebook.com/sharer.php?u=%@&t=%@", facebookShareLink, titleToShare];
    facebookLink = [facebookLink stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:facebookLink]];

} else {

    SLComposeViewController *composeViewController = [composeViewControllerClass composeViewControllerForServiceType:SLServiceTypeFacebook];
    [composeViewController setInitialText:titleToShare];
    [composeViewController addURL:[NSURL URLWithString:facebookShareLink]];

    [parentController presentModalViewController:composeViewController animated:YES];
}

you can use third party components such as ShareKit. 您可以使用第三方组件,例如ShareKit。
http://getsharekit.com/ http://getsharekit.com/

you can share in facebook, twitter or by email using uiactivity 您可以使用uiactivity在Facebook,Twitter或电子邮件中共享

UIImage *image1 = shareimgview.image;
NSString *str1=[@"Brand: " stringByAppendingString:brand_field.text];
NSString *str2=[@"Product: " stringByAppendingString:product_field.text];
NSString *str3=[@"Model: "stringByAppendingString:model_field.text];
NSString *str4=[@"Comment: "stringByAppendingString:status_field.text];
NSArray* dataToShare = @[str1,myurl,str2,str3,str4, image1];
UIActivityViewController *actviewcon = [[UIActivityViewController alloc] initWithActivityItems:dataToShare applicationActivities:nil];
[self presentViewController:actviewcon animated:YES completion:NULL];

For those of you looking for a Swift 3 solution, here it is: 对于那些正在寻找Swift 3解决方案的人,这里是:

if let composeVC = SLComposeViewController(forServiceType:serviceType) {
    composeVC.add(object.image())
    composeVC.add(object.url())
    composeVC.setInitialText(object.text())
    presentingVC.present(composeVC, animated: true, completion: nil)
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM