Bri*_*any 10 facebook objective-c ios
我刚刚将Facebook iOS SDK与我的应用程序集成,登录效果很好.也就是说,SDK似乎没有给我自定义登录按钮的选项(它为我提供了屏幕中间那个丑陋的默认按钮).我在我的应用程序中使用Storyboard - 如何将我自己的按钮连接到他们提供的代码?我已经看到一些较旧的答案发布到Stack,但FB文档已经改变了:/
Viewcontroller.m
FBSDKLoginButton *loginButton = [[FBSDKLoginButton alloc] init];
loginButton.center = self.view.center;
[self.view addSubview:loginButton];
Run Code Online (Sandbox Code Playgroud)
bea*_*ain 38
在故事板中创建自己的自定义按钮.将动作连接起来myButtonPressed.
- (void)viewDidLoad {
[super viewDidLoad];
self.loginButton = [[FBSDKLoginButton alloc] init];
self.loginButton.hidden = YES;
}
- (void)myButtonPressed {
[self.loginButton sendActionsForControlEvents: UIControlEventTouchUpInside];
}
Run Code Online (Sandbox Code Playgroud)
Mad*_*pta 16
针对Swift 3进行了更新
@IBAction func fblogin(_ sender: Any) {
let loginManager = LoginManager()
UIApplication.shared.statusBarStyle = .default // remove this line if not required
loginManager.logIn([ .publicProfile,.email ], viewController: self) { loginResult in
print(loginResult)
//use picture.type(large) for large size profile picture
let request = GraphRequest(graphPath: "me", parameters: ["fields":"email,name,gender,picture"], accessToken: AccessToken.current, httpMethod: .GET, apiVersion: FacebookCore.GraphAPIVersion.defaultVersion)
request.start { (response, result) in
switch result {
case .success(let value):
print(value.dictionaryValue)
case .failed(let error):
print(error)
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
对于Objective-C
您可以在UIButtonclick事件 上调用此方法
-(void)fblogin{
FBSDKLoginManager *login = [[FBSDKLoginManager alloc] init];
if ([UIApplication.sharedApplication canOpenURL:[NSURL URLWithString:@"fb://"]])
{
login.loginBehavior = FBSDKLoginBehaviorSystemAccount;
}
[login logInWithReadPermissions:@[@"public_profile", @"email"] handler:^(FBSDKLoginManagerLoginResult *result, NSError *error) {
if (error)
{
NSLog(@"Unexpected login error: %@", error);
NSString *alertMessage = error.userInfo[FBSDKErrorLocalizedDescriptionKey] ?: @"There was a problem logging in. Please try again later.";
NSString *alertTitle = error.userInfo[FBSDKErrorLocalizedTitleKey] ?: @"Oops";
[[[UIAlertView alloc] initWithTitle:alertTitle
message:alertMessage
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil] show];
}
else
{
if(result.token) // This means if There is current access token.
{
[[[FBSDKGraphRequest alloc] initWithGraphPath:@"me"
parameters:@{@"fields": @"picture, name, email"}]
startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id userinfo, NSError *error) {
if (!error) {
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul);
dispatch_async(queue, ^(void) {
dispatch_async(dispatch_get_main_queue(), ^{
// you are authorised and can access user data from user info object
});
});
}
else{
NSLog(@"%@", [error localizedDescription]);
}
}];
}
NSLog(@"Login Cancel");
}
}];
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
12984 次 |
| 最近记录: |