這幾天接facebook登錄分享邀請(qǐng)等功能
facebook 開(kāi)發(fā)者中心網(wǎng)址:https://developers.facebook.com
找到下載的sdk,并把sdk中的?FBSDKCoreKit.Framework, FBSDKLoginKit.Framework, FBSDKShareKit.Framework?添加到xcode工程中
更改Plist 文件? 添加 FacebookAppID (對(duì)應(yīng)appid)與 FacebookDisplayName (對(duì)應(yīng)應(yīng)用名)兩個(gè)字段
適配iOS9 :
FB登錄:
- (void)fbLogin:(int)funcId{
NSLog(@"funid = %d", funcId);
UIViewController* ctrol=[UIApplicationsharedApplication].keyWindow.rootViewController;
[ctrol.viewaddSubview:self.view]; ?//facebook視圖添加到主視圖中
[loginButtonsendActionsForControlEvents:UIControlEventTouchUpInside];//按鈕自動(dòng)點(diǎn)擊
_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邀請(qǐng):