我想知道如何使用Xcode 4.5上的新框架向iOS 6上的Facebook發佈狀態消息。謝謝! :)如何使用ACAccountStore在Objective-C的iOS 6上發佈到iOS上的Facebook
24
A
回答
75
發佈消息相當簡單。這幾乎就像Twitter框架。
首先你要導入的框架:社會和應收
#import <Social/Social.h>
#import <Accounts/Accounts.h>
在您的.h文件中:
SLComposeViewController *mySLComposerSheet;
這些代碼被包含在.m文件的行動中:
if([SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook]) //check if Facebook Account is linked
{
mySLComposerSheet = [[SLComposeViewController alloc] init]; //initiate the Social Controller
mySLComposerSheet = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook]; //Tell him with what social platform to use it, e.g. facebook or twitter
[mySLComposerSheet setInitialText:[NSString stringWithFormat:@"Test",mySLComposerSheet.serviceType]]; //the message you want to post
[mySLComposerSheet addImage:yourimage]; //an image you could post
//for more instance methods, go here: https://developer.apple.com/documentation/social/slcomposeviewcontroller#//apple_ref/doc/uid/TP40012205
[self presentViewController:mySLComposerSheet animated:YES completion:nil];
}
[mySLComposerSheet setCompletionHandler:^(SLComposeViewControllerResult result) {
NSString *output;
switch (result) {
case SLComposeViewControllerResultCancelled:
output = @"Action Cancelled";
break;
case SLComposeViewControllerResultDone:
output = @"Post Successful";
break;
default:
break;
} //check if everything worked properly. Give out a message on the state.
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Facebook" message:output delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil];
[alert show];
}];
1
我該怎麼辦,當我只想在郵件成功時收到提醒,以及用戶取消帖子後什麼都沒有?
不幸的是,這對Twitter無法正常工作...它不會消除TweetSheet了。這裏是我的代碼:
if(NSClassFromString(@"SLComposeViewController") != nil)
{
mySLComposerSheet = [[SLComposeViewController alloc] init]; //initiate the Social Controller
mySLComposerSheet = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeTwitter]; //Tell him with what social plattform to use it, e.g. facebook or twitter
[mySLComposerSheet setInitialText:[NSString stringWithFormat:story.title,mySLComposerSheet.serviceType]]; //the message you want to post
[mySLComposerSheet addURL:[NSURL URLWithString:story.link]];
//for more instance methodes, go here:https://developer.apple.com/library/ios/#documentation/NetworkingInternet/Reference/SLComposeViewController_Class/Reference/Reference.html#//apple_ref/doc/uid/TP40012205
[mySLComposerSheet setCompletionHandler:^(SLComposeViewControllerResult result) {
NSString *output;
switch (result) {
case SLComposeViewControllerResultCancelled:
output = NSLocalizedStringFromTable(@"As it seems you didn't want to post to Twitter", @"ATLocalizable", @"");
break;
case SLComposeViewControllerResultDone:
output = NSLocalizedStringFromTable(@"You successfully posted to Twitter", @"ATLocalizable", @"");
break;
default:
break;
} //check if everything worked properly. Give out a message on the state.
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Twitter" message:output delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil];
[alert show];
}];
[self presentViewController:mySLComposerSheet animated:YES completion:nil];
0
好吧,所以我調整了原帖,適用於iOS 6/7。更改Facebook的服務類型,警報標題和消息。請享用!
- (IBAction)tweetMessage:(id)sender {
if(![SLComposeViewController isAvailableForServiceType:SLServiceTypeTwitter]) //check if Facebook Account is linked
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Unable to Tweet!" message:@"Please login to Twitter in your device settings." delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil];
[alert show];
return;
}
//self.mySLComposerSheet = [[SLComposeViewController alloc] init]; //initiate the Social Controller
self.mySLComposerSheet = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeTwitter];
[self.mySLComposerSheet setInitialText:[NSString stringWithFormat:@"I found this Thing, check it out at this Place:\n %@ \n", [self someplace]]];
[self.mySLComposerSheet addImage:self.photos.firstObject];
[self presentViewController:self.mySLComposerSheet animated:YES completion:nil];
//}
[self.mySLComposerSheet setCompletionHandler:^(SLComposeViewControllerResult result) {
NSString *output;
switch (result) {
case SLComposeViewControllerResultCancelled:
output = @"Action Cancelled";
break;
case SLComposeViewControllerResultDone:
output = @"Post Successfull";
break;
default:
break;
} //check if everything worked properly. Give out a message on the state.
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Twitter" message:output delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil];
[alert show];
}];
}
0
這是我在我的應用程序中實際使用它的方式。更平滑,並讓控制器完成艱苦的工作。
- (IBAction)postToFacebook:(id)sender {
if(![SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook]) {
NSLog(@"log output of your choice here");
}
// Facebook may not be available but the SLComposeViewController will handle the error for us.
self.mySLComposerSheet = [[SLComposeViewController alloc] init];
self.mySLComposerSheet = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];
[self.mySLComposerSheet setInitialText:[NSString stringWithFormat:@"I found this Thing, check it out at SomeWhere:\n %@ \n", [self someURLString]]];
[self.mySLComposerSheet addImage:self.photos.firstObject]; //an image you could post
[self presentViewController:self.mySLComposerSheet animated:YES completion:nil];
[self.mySLComposerSheet setCompletionHandler:^(SLComposeViewControllerResult result) {
NSString *output;
switch (result) {
case SLComposeViewControllerResultCancelled:
output = @"Action Cancelled";
break;
case SLComposeViewControllerResultDone:
output = @"Post Successfull";
break;
default:
break;
}
if (![output isEqualToString:@"Action Cancelled"]) {
// Only alert if the post was a success. Or not! Up to you.
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Facebook" message:output delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil];
[alert show];
}
}];
}
1
- (IBAction)btn_facebook:(id)sender {
SLComposeViewController *facebookcomposer =
[SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];
[facebookcomposer setInitialText:@"This is just a test"];
[facebookcomposer addURL:[NSURL URLWithString:@"http://www.google.com"]];
[facebookcomposer addImage:[UIImage imageNamed:@"images.jpg"]];
[self presentViewController:facebookcomposer animated:YES completion:nil];
[facebookcomposer setCompletionHandler:^(SLComposeViewControllerResult result)
{
switch (result)
{
case SLComposeViewControllerResultDone:
NSLog(@"done");
break;
case SLComposeViewControllerResultCancelled:
NSLog(@"cancelled");
break;
default:
break;
}
}];
}
- (IBAction)btn_twitter:(id)sender {
if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeTwitter]) {
SLComposeViewController *twitter =
[SLComposeViewController composeViewControllerForServiceType:SLServiceTypeTwitter];
[twitter setInitialText:@"this is just a test"];
[twitter addURL:[NSURL URLWithString:@"http://www.google.com"]];
[twitter addImage:[UIImage imageNamed:@"images.jpg"]];
[self presentViewController:twitter animated:YES completion:nil];
} else {
NSLog(@"it is not configured");
}
}
相關問題
- 1. 如何在iOS 6上發佈圖像到Facebook 6
- 2. 得到錯誤,在Facebook上發佈IOS
- 3. 發佈在iOS版的Facebook牆上
- 4. 圖片不在ios上發佈instagram 6
- 5. iOS ACAccountStore Facebook令牌類型?
- 6. 在iOS 6中使用SLRequest發佈到Facebook
- 7. 可以在Facebook上發佈圖片嗎? iOS 6 - Social.framework
- 8. 在Facebook上發佈圖像iOS
- 9. 在Facebook牆上發佈圖片xcode ios
- 10. 發佈在使用Facebook SDK 3.0的朋友牆上的iOS
- 11. 使用iOS SDK發佈評論在朋友的Facebook牆上
- 12. 在IOS上使用身份驗證在iOS上發佈文本
- 13. 如何在iOS的Facebook上發佈大型視頻?
- 14. 如何通過iOS應用在Facebook上發佈信息?
- 15. writeToFile在iOS 7上不在iOS 6上
- 16. 如何在iOS上分享多個網址到Facebook 6
- 17. 當發佈到用戶的牆上時,Facebook SDK API IOS錯誤
- 18. ios - 如何將PDF發佈到Facebook
- 19. 我想在iOS應用上發佈到Facebook時添加標籤
- 20. iOS 6如何在Facebook粉絲頁面發佈圖片
- 21. 如何使用iOS中的Sharekit直接在Facebook或Twitter上發佈
- 22. 如何在iOS中的朋友牆上的Facebook上發佈消息?
- 23. 如何在XCode 4.5的iOS 6的Facebook用戶牆上上傳視頻?
- 24. 使用Facebook的ios應用程序在Facebook上發佈狀態3.1 SDK
- 25. 如何在iOS上強制發佈
- 26. 無法在iOS 7上使用Facebook SDK發佈帖子
- 27. 如何在ios 6的facebook和twitter上自動共享?
- 28. 如何使用IOS中的最新Facebook sdk發佈朋友牆上的文本
- 29. 在Facebook(iOS)中使用SSO發佈Feed
- 30. 如何發佈iOS的Facebook OG操作?
有很好的一套教程這裏:http://developers.facebook.com/docs/getting-started/getting-started-with-the-ios-sdk/ –
不,我的意思使用iOS 6上的新框架。 – jaytrixz
您可以使用社交框架獲取更多詳細信息,請訪問http://kmithi.blogspot.in/2012/10/integrating-facebook-and-twitter-in-ios.html – mithilesh