在没有对话框的情况下发布到 facebook ios 6
Posted
技术标签:
【中文标题】在没有对话框的情况下发布到 facebook ios 6【英文标题】:post to facebook without dialog ios 6 【发布时间】:2012-09-24 00:51:37 【问题描述】:我想知道是否可以使用 SLComposeViewController 在用户的 Facebook 墙上发布内容但不显示共享表/对话框?
以下是我正在使用的代码:
if([SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook])
SLComposeViewController *controller = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];
SLComposeViewControllerCompletionHandler myBlock = ^(SLComposeViewControllerResult result)
if (result == SLComposeViewControllerResultCancelled)
NSLog(@"Cancelled");
else
NSLog(@"Done");
[controller dismissViewControllerAnimated:YES completion:Nil];
;
controller.completionHandler =myBlock;
[controller setInitialText:eventInfoToFacebook];
[self presentViewController:controller animated:YES completion:Nil];
提前致谢。
【问题讨论】:
你需要使用苹果的框架吗? 【参考方案1】:导入社交框架:
#import <Social/Social.h>
-
if([SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook])
SLComposeViewController *controller = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];
SLComposeViewControllerCompletionHandler myBlock = ^(SLComposeViewControllerResult result)
if (result == SLComposeViewControllerResultCancelled)
NSLog(@"Cancelled");
else
NSLog(@"Done");
[controller dismissViewControllerAnimated:YES completion:Nil];
;
controller.completionHandler =myBlock;
[controller setInitialText:@"Test Post from mobile.safilsunny.com"];
[controller addURL:[NSURL URLWithString:@"http://www.mobile.safilsunny.com"]];
[controller addImage:[UIImage imageNamed:@"fb.png"]];
[self presentViewController:controller animated:YES completion:Nil];
else
NSLog(@"UnAvailable");
/
//Deprecated in ios6
ACAccountStore *accountStore = [[ACAccountStore alloc] init];
ACAccountType *accountType = [accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierFacebook];
[accountStore requestAccessToAccountsWithType:accountType withCompletionHandler:^(BOOL granted, NSError *error)
ACAccount *account = [[ACAccount alloc] initWithAccountType:accountType];
NSLog(@"%@, %@", account.username, account.description);
];
【讨论】:
【参考方案2】:这里我得到了答案,要同时发布image data
和url
我们需要添加Social.framework,这个框架在iOS6 中可用。
只需在您的项目中添加Social.framework
并添加自爆代码即可。
if([SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook])
SLComposeViewController *controller = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];
SLComposeViewControllerCompletionHandler myBlock = ^(SLComposeViewControllerResult result)
if (result == SLComposeViewControllerResultCancelled)
NSLog(@"ResultCancelled");
else
NSLog(@"Success");
[controller dismissViewControllerAnimated:YES completion:Nil];
;
controller.completionHandler =myBlock;
[controller addURL:[NSURL URLWithString:@"https://itunes.apple.com/us/app/social-checkin/id504791401?mt=8"]];
if (encoded_ImageData == nil)
[controller addImage:[UIImage imageNamed:@"No_ImageFound.png"]];
else
[controller addImage:[UIImage imageWithData:encoded_ImageData]];
NSString *businessName;
//Set business name string to be passed on facebook
if (m_BusinessNameString == nil || [m_BusinessNameString isEqualToString:@""])
businessName = @"Business name not specified!";
else
businessName = [m_BusinessNameString uppercaseString];
NSString *nameString = [NSString stringWithFormat:@"CHECKED IN @"];
//user has checked in with his friends if sc-merchant
NSString *friendsString;
if ([checkedFriendsNameArray count] > 0)
NSMutableString *checkedFriendsTempStr = [[NSMutableString alloc] init];
for (NSMutableString *checkedStr in checkedFriendsNameArray)
[checkedFriendsTempStr appendFormat:[NSString stringWithFormat:@"%@,",checkedStr]];
friendsString = [NSString stringWithFormat:@"WITH %@",checkedFriendsTempStr];
else
friendsString = [NSString stringWithFormat:@"WITH NO FRIENDS"];
NSString *fname= [[NSUserDefaults standardUserDefaults] valueForKey:@"userfname"];
NSString *lname= [[NSUserDefaults standardUserDefaults] valueForKey:@"userlname"];
NSString *name=[fname stringByAppendingString:[NSString stringWithFormat:@"%@",lname]];
NSString *main_TextString =[NSString stringWithFormat:@"%@ \n %@ %@ %@ %@",upperCaseStatusString,name,nameString,businessName,friendsString];
[controller setInitialText:main_TextString];
[self presentViewController:controller animated:YES completion:Nil];
else
NSLog(@"UnAvailable");
【讨论】:
【参考方案3】:在您的 ViewController 中添加以下代码并导入以下框架。
import FBSDKCoreKit
import FBSDKLoginKit
import FBSDKShareKit
@IBAction func btnPostClick(_ sender: Any)
if (FBSDKAccessToken.current() != nil)
if FBSDKAccessToken.current().hasGranted("publish_actions")
postOnFB()
else
let loginManager = FBSDKLoginManager()
loginManager.logIn(withPublishPermissions: ["publish_actions"], from: self, handler: (result, error) in
if error == nil
self.postOnFB()
else
print(error?.localizedDescription ?? "")
)
else
let loginManager = FBSDKLoginManager()
loginManager.logIn(withPublishPermissions: ["publish_actions"], from: self, handler: (result, error) in
if error == nil
self.postOnFB()
)
func postOnFB()
FBSDKGraphRequest(graphpath: "me/feed", parameters: ["message": "YOUR MESSAGE"], httpMethod: "POST").start (connection, result, error) in
if error == nil
print("post id \(result ?? "")")
else
print("error is \(error?.localizedDescription ?? "")")
【讨论】:
【参考方案4】:这在 facebook-ios-sdk 3.0 中是可能的。 here
我正在使用此代码进行分享。
[FBRequestConnection startWithGraphPath:@"me/feed"
parameters:params
HTTPMethod:@"POST"
completionHandler:^(FBRequestConnection *connection,
NSDictionary * result,
NSError *error)
if (error)
NSLog(@"Error: %@", [error localizedDescription]);
else
];
当用户单击共享按钮并且没有与用户直接发布到用户墙的对话时,我调用了此方法。 params 是共享包含的参数。
【讨论】:
以上是关于在没有对话框的情况下发布到 facebook ios 6的主要内容,如果未能解决你的问题,请参考以下文章
Facebook iOS sdk 3.2 在没有 FBWebDialogResult 的情况下发布到墙上
Facebook API:如何在不登录的情况下发布到自己的应用程序墙
是否有任何选项可以在不使用 Facebook Graph API 的情况下发布到用户朋友的墙上