iOS Facebook 接入

这几天接facebook登录分享邀请等功能

facebook 开发者中心网址:https://developers.facebook.com

找到下载的sdk,并把sdk中的 FBSDKCoreKit.Framework, FBSDKLoginKit.Framework, FBSDKShareKit.Framework 添加到xcode工程中

更改Plist 文件  添加 FacebookAppID (对应appid)与 FacebookDisplayName (对应应用名)两个字段

适配iOS9 :


FB登录:

- (void)fbLogin:(int)funcId{

NSLog(@"funid = %d", funcId);

UIViewController* ctrol=[UIApplicationsharedApplication].keyWindow.rootViewController;

[ctrol.viewaddSubview:self.view];  //facebook视图添加到主视图中

[loginButtonsendActionsForControlEvents:UIControlEventTouchUpInside];//按钮自动点击

_funcId= funcId;

}

FB 登录成功获取用户资料:

- (void)loginButton:(FBSDKLoginButton*)loginButton

didCompleteWithResult:(FBSDKLoginManagerLoginResult*)result

error:(NSError*)error{

NSLog(@"%@", error);

if(result.isCancelled) {

}else{

FBSDKAccessToken*fbtoken = result.token;

NSString*userId = fbtoken.userID;

NSString*appId = fbtoken.appID;

NSString*url =@"https://graph.facebook.com/";

NSString*type =@"/picture?type=large";

NSString*imageString = [NSStringstringWithFormat:@"%@%@%@", url, userId, type];  //获取FB用户头像url

NSLog(@"userid = %@, appId = %@, imageUrl = %@", userId, appId, imageString);

FBSDKGraphRequest*request = [[FBSDKGraphRequestalloc]initWithGraphPath:userIdparameters:[NSDictionarynew]HTTPMethod:@"Get"];

[requeststartWithCompletionHandler:^(FBSDKGraphRequestConnection*connection,idresult,NSError*error) {

nickName= [resultobjectForKey:@"name"];  //获取FB用户昵称

NSString*userId = [resultobjectForKey:@"id"]; //获取FB用户ID

NSLog(@"nickName = %@, userId = %@",nickName, userId);

if(_funcId!= -1) {

LuaObjcBridge::pushLuaFunctionById(_funcId);

LuaValueDictdict;

dict["openId"] =LuaValue::stringValue( [userIdUTF8String] );

dict["nickName"] =LuaValue::stringValue( [nickNameUTF8String] );

dict["imageUrl"] =LuaValue::stringValue( [imageStringUTF8String] );

LuaObjcBridge::getStack()->pushLuaValueDict( dict );

LuaObjcBridge::getStack()->executeFunction(1);

LuaObjcBridge::releaseLuaFunctionById(_funcId);

}

}];

}

[selfgetLoginButton].hidden=YES;

[self.viewremoveFromSuperview];

[selfrelease];

}

FB分享:

- (void)shareToFB:(int)sceneType :(NSString*)imagePath :(NSString*)title :(NSString*)desc :(NSString*)url

{

UIViewController* ctrol=[UIApplicationsharedApplication].keyWindow.rootViewController;

[ctrol.viewaddSubview:self.view];

FBSDKShareLinkContent*linkContent = [[FBSDKShareLinkContentalloc]init];

linkContent.contentURL= [NSURLURLWithString:url];

linkContent.contentTitle= title;

NSString*str =@"http://down.songplay.cn/dzpk/android/00000/0.0.0/";

NSString*imageUrl = [NSStringstringWithFormat:@"%@%@", str,imagePath];

linkContent.imageURL= [NSURLURLWithString:imageUrl];

linkContent.contentDescription= desc;

[FBSDKShareDialogshowFromViewController:selfwithContent:linkContentdelegate:self];

}

#pragma mark fb分享代理

- (void)sharer:(id)sharer didCompleteWithResults:(NSDictionary*)results

{

[selfgetLoginButton].hidden=YES;

[self.viewremoveFromSuperview];

[selfrelease];

}

/*!

@abstract Sent to the delegate when the sharer encounters an error.

@param sharer The FBSDKSharing that completed.

@param error The error.

*/

- (void)sharer:(id)sharer didFailWithError:(NSError*)error

{

NSLog(@"%@", error);

[selfgetLoginButton].hidden=YES;

[self.viewremoveFromSuperview];

[selfrelease];

UIViewController* ctrol=[UIApplicationsharedApplication].keyWindow.rootViewController;

[ctrol.viewaddSubview:self.view];  //分享失败  登录FB

[[selfgetLoginButton]sendActionsForControlEvents:UIControlEventTouchUpInside];

}

FB邀请:

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 原文网址:http://www.jianshu.com/p/7e3c5fc31708 0.demo说明别的先不说d...
    枫之叶_小乙哥阅读 2,913评论 1 5
  • 在这里总结一些iOS开发中的小技巧,能大大方便我们的开发,持续更新。 UITableView的Group样式下顶部...
    UI爱好者阅读 540评论 0 0
  • iOS支付 iOS支付分为两类,第三方支付和应用内支付(内购)。 第三方支付包括:支付宝支付、微信支付、银联支付、...
    请输入账号名阅读 6,352评论 3 22
  • 在这里总结一些iOS开发中的小技巧,能大大方便我们的开发 原文地址:http://www.jianshu.com/...
    Marray阅读 351评论 0 0
  • 01 最近几天一直在自我谴责中,好几天没有写文章了。原因有很多 不知道写什么好。和大多数人一样,每天过着重复昨天的...
    拉小登阅读 197评论 0 0