这几天接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邀请: