SLComposeViewController 공유를위한 튜토리얼
iOS 6의 새로운 기능 SLComposeViewController
을 사용 하여 Facebook, Twitter 또는 Sina Weibo에 게시 하려면 어떤 단계를 따라야 합니까?
이 프레임 워크에 대한 자세한 내용은 Apple의 Social Framework 클래스 참조를 참조하십시오.
추가 튜토리얼 :
- 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
이 예에서는 SLComposeViewController
's SLServiceTypeFacebook
. Twitter 또는 SinaWeibo를 사용하려면 SLServiceType을 다음 중 하나로 변경하십시오.
- SLServiceTypeFacebook
- SLServiceTypeSinaWeibo
- SLServiceTypeTwitter
iOS 6에서는 .NET Framework를 사용하여 Facebook, Twitter 또는 Sina Weibo에 직접 게시하는 것이 매우 쉽습니다 SLComposeViewController
. 이것은 iOS 5의 TWTweetComposeViewController
.
먼저 뷰 컨트롤러의 헤더 파일 (.h) #import
에서 소셜 프레임 워크와 계정 프레임 워크입니다.
#import <Social/Social.h>
#import <Accounts/Accounts.h>
여기서는 단순 UIButton
하고 IBAction
나중에 해당 버튼에 연결할 것을 선언 void
하고 선택한 공유 서비스를 사용할 수 있는지 확인하는 데 사용할 (sharingStatus)를 선언합니다.
@interface ViewController : UIViewController
@property (weak, nonatomic) IBOutlet UIButton *easyFacebookButton;
- (IBAction)facebookPost:(id)sender;
- (void)sharingStatus;
@end
@implementation ViewController
그런 다음 구현 파일 (.m)에서 헤더 파일에서 선언 한 (sharingStatus) void를 구현하는 것으로 시작합니다. sharingStatus는 SLComposeViewController
의 isAvailableForServiceType
BOOL을 사용 하여 인수에 지정된 서비스에 게시 할 수 있는지 여부를 반환합니다. 이 경우 서비스 유형을 사용합니다 SLServiceTypeFacebook
. 서비스를 사용할 수있는 경우 게시 버튼은 알파 값 1.0f로 활성화되고 서비스를 사용할 수없는 경우 버튼은 알파 값이 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;
}
}
여기 IBAction
에서 작곡가를 호출 할를 설정합니다. 모범 사례 isAvailableForServiceType
를 위해 사용할 수없는 서비스 유형에 대해 작성기를 호출하지 않도록 다시 확인 합니다. (마지막 확인 중에 문제가 발생했거나 게시 버튼을 탭하고 작곡가 모두 / 초기화하는 사이에 순식간에 가용성이 변경된 경우. 아래 코드는 텍스트가있는 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];
}
}
에서 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!
Just use this code to share on 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.
Safe Use of 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];
}];
}
참고URL : https://stackoverflow.com/questions/12503287/tutorial-for-slcomposeviewcontroller-sharing
'program tip' 카테고리의 다른 글
Xcode 4 + iOS 4.3 : "아카이브 유형에 대한 Packager가 없습니다." (0) | 2020.10.16 |
---|---|
Java가 인터페이스에서 개인 멤버를 허용하지 않는 이유는 무엇입니까? (0) | 2020.10.16 |
Python에서 sys.path에 파일 경로를 영구적으로 추가 (0) | 2020.10.15 |
Gradle에서 ''AppPlugin '유형의 플러그인을 만들 수 없습니다.'라는 오류가 발생합니다. (0) | 2020.10.15 |
Oracle : SQL Server 용 프로파일 러와 같은 쿼리를 추적하는 도구가 있습니까? (0) | 2020.10.15 |