ios SLComposeViewController 分享教程
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12503287/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Tutorial for SLComposeViewController sharing
提问by Mick MacCallum
What are the steps I need to follow to use iOS 6's new SLComposeViewController
to post to Facebook, Twitter or Sina Weibo?
使用 iOS 6 的新SLComposeViewController
功能发布到 Facebook、Twitter 或新浪微博需要遵循哪些步骤?
回答by Mick MacCallum
For details on this framework please see Apple's Social Framework Class Reference
有关此框架的详细信息,请参阅 Apple 的社交框架类参考
Additional tutorials:
附加教程:
- http://soulwithmobiletechnology.blogspot.com/2012/07/tutorial-how-to-use-inbuilt.html
- http://www.mobile.safilsunny.com/iphone/integrating-facebook-ios/
- https://rudeboy-quickies.blogspot.com/2012/06/steps-to-integrate-facebook-in-ios6.html
- http://soulwithmobiletechnology.blogspot.com/2012/07/tutorial-how-to-use-inbuilt.html
- http://www.mobile.safilsunny.com/iphone/integrating-facebook-ios/
- https://rudeboy-quickies.blogspot.com/2012/06/steps-to-integrate-facebook-in-ios6.html
For this example, we will be using the SLComposeViewController
's SLServiceTypeFacebook
. If you wish to use Twitter or SinaWeibo just change out the SLServiceType to one of the following:
在这个例子中,我们将使用SLComposeViewController
's SLServiceTypeFacebook
。如果您想使用 Twitter 或新浪微博,只需将 SLServiceType 更改为以下之一:
- SLServiceTypeFacebook
- SLServiceTypeSinaWeibo
- SLServiceTypeTwitter
- SL服务类型Facebook
- SL服务类型新浪微博
- SLServiceTypeTwitter
iOS 6 has made it very easy to post directly to Facebook, Twitter or Sina Weibo using the SLComposeViewController
. This works very similarly to iOS 5's TWTweetComposeViewController
.
iOS 6 使得使用SLComposeViewController
. 这与 iOS 5 的TWTweetComposeViewController
.
First, in your view controller's header file (.h) #import
the Social Framework and the Accounts Framework.
首先,在您的视图控制器的头文件 (.h) 中#import
,社交框架和帐户框架。
#import <Social/Social.h>
#import <Social/Social.h>
#import <Accounts/Accounts.h>
#import <Accounts/Accounts.h>
Here we will declare a simple UIButton
and an IBAction
that we will later link to that button and a void
(sharingStatus) which will be used to check that the selected sharing service is available.
在这里,我们将声明一个 simpleUIButton
和 an IBAction
,稍后将链接到该按钮和一个void
(sharingStatus),用于检查所选共享服务是否可用。
@interface ViewController : UIViewController
@property (weak, nonatomic) IBOutlet UIButton *easyFacebookButton;
- (IBAction)facebookPost:(id)sender;
- (void)sharingStatus;
@end
@implementation ViewController
Then, in your implementation file (.m), we'll start by implementing the (sharingStatus) void that we declared in the header file. sharingStatus uses SLComposeViewController
's isAvailableForServiceType
BOOL to return whether or not you can post to the service specified in its argument. In this case, we will use the service type SLServiceTypeFacebook
. If the service is available the post button will be enabled with an alpha value of 1.0f, and if the service isn't available the button will be disabled its alpha value set to 0.5f.
然后,在您的实现文件 (.m) 中,我们将首先实现我们在头文件中声明的 (sharingStatus) void。sharedStatus 使用SLComposeViewController
's isAvailableForServiceType
BOOL 返回您是否可以发布到其参数中指定的服务。在这种情况下,我们将使用服务类型SLServiceTypeFacebook
。如果服务可用,发布按钮将被启用,alpha 值为 1.0f,如果服务不可用,按钮将被禁用,其 alpha 值设置为 0.5f。
- (void)sharingStatus {
if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook]) {
NSLog(@"service available");
self.easyFacebookButton.enabled = YES;
self.easyFacebookButton.alpha = 1.0f;
} else {
self.easyFacebookButton.enabled = NO;
self.easyFacebookButton.alpha = 0.5f;
}
}
Here we will set up the IBAction
that will call up the composer. For good practice, we will check isAvailableForServiceType
again to avoid calling up the composer for a service type that isn't available. (Incase something went wrong during the last check, or if availability somehow changed in the fraction of a second in between tapping the post button and the composers all/init. The code below has been set up to display a Facebook composers sheet with text, an image, and a link. This action also utilises a completion handler for the composer's cancelled and done results.
在这里,我们将设置IBAction
调用作曲家的 。作为良好的实践,我们将isAvailableForServiceType
再次检查以避免为不可用的服务类型调用 Composer。(以防在上次检查期间出现问题,或者如果在点击发布按钮和作曲家全部/初始化之间的几分之一秒内可用性以某种方式发生变化。下面的代码已设置为显示带有文本的 Facebook 作曲家表,一个图像和一个链接。这个动作还利用一个完成处理程序来处理作曲家的取消和完成结果。
- (IBAction)facebookPost:(id)sender {
if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook]) {
SLComposeViewController *mySLComposerSheet = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];
[mySLComposerSheet setInitialText:@"iOS 6 Social Framework test!"];
[mySLComposerSheet addImage:[UIImage imageNamed:@"myImage.png"]];
[mySLComposerSheet addURL:[NSURL URLWithString:@"http://stackoverflow.com/questions/12503287/tutorial-for-slcomposeviewcontroller-sharing"]];
[mySLComposerSheet setCompletionHandler:^(SLComposeViewControllerResult result) {
switch (result) {
case SLComposeViewControllerResultCancelled:
NSLog(@"Post Canceled");
break;
case SLComposeViewControllerResultDone:
NSLog(@"Post Sucessful");
break;
default:
break;
}
}];
[self presentViewController:mySLComposerSheet animated:YES completion:nil];
}
}
In viewWillAppear
we will register an observer to ACAccountStoreDidChangeNotification
so the application can be notified when account information changes. This observer will then be removed in viewDidDisappear
.
在viewWillAppear
我们的观察员注册到ACAccountStoreDidChangeNotification
这样的应用程序可以在账户信息发生变化的通知。然后将在 中删除此观察者viewDidDisappear
。
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(sharingStatus) name:ACAccountStoreDidChangeNotification object:nil];
}
- (void)viewDidDisappear:(BOOL)animated
{
[super viewDidDisappear:animated];
[[NSNotificationCenter defaultCenter] removeObserver:ACAccountStoreDidChangeNotification];
}
And finally, open up interface builder and add a UIButton
which will be the post button. Then in the connections inspector link the IBOutlet
and IBAction
we created earlier to the button, and that's it! You're done!
最后,打开界面构建器并添加一个UIButton
将作为发布按钮的按钮。然后在连接检查器中将我们之前创建的IBOutlet
和链接IBAction
到按钮,就是这样!你完成了!
回答by Desert Rose
Just use this code to share on Facebook.
只需使用此代码在 Facebook 上分享即可。
SLComposeViewController *controllerSLC = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];
[controllerSLC setInitialText:@"First post from my iPhone app"];
[controllerSLC addURL:[NSURL URLWithString:@"http://www.appcoda.com"]];
[controllerSLC addImage:[UIImage imageNamed:@"test.jpg"]];
[self presentViewController:controllerSLC animated:YES completion:Nil];
If you want this for Twitter then just change SLServiceTypeTwitter.
如果你想要这个 Twitter,那么只需更改 SLServiceTypeTwitter。
回答by A R
Safe Use of SLComposeViewController
SLComposeViewController 的安全使用
if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook])
{
SLComposeViewController *fbPost = [SLComposeViewController
composeViewControllerForServiceType: SLServiceTypeFacebook];
[fbPost setInitialText:@"Text You want to Share"];
[fbPost addImage:[UIImage imageNamed:@"shareImage.png"]];
[self presentViewController:fbPost animated:YES completion:nil];
[fbPost setCompletionHandler:^(SLComposeViewControllerResult result) {
switch (result) {
case SLComposeViewControllerResultCancelled:
NSLog(@"Post Canceled");
break;
case SLComposeViewControllerResultDone:
NSLog(@"Post Sucessful");
break;
default:
break;
}
[self dismissViewControllerAnimated:YES completion:nil];
}];
}