2016-02-02 28 views
7

Tôi vừa tích hợp Facebook iOS SDK với ứng dụng của mình và đăng nhập hoạt động rất tốt. Điều đó nói rằng, SDK doesnt dường như cung cấp cho tôi tùy chọn để tùy chỉnh nút đăng nhập của tôi (nó cung cấp cho tôi với nút mặc định xấu xí ở giữa màn hình). Tôi đang sử dụng Bảng phân cảnh cùng với ứng dụng của mình - làm cách nào tôi có thể tìm kiếm nút của riêng mình cho mã được cung cấp? Tôi đã nhìn thấy một số câu trả lời cũ được đưa lên ngăn xếp, nhưng tài liệu FB đã thay đổi kể từ:/Nút Đăng nhập Facebook Tùy chỉnh iOS

Viewcontroller.m

FBSDKLoginButton *loginButton = [[FBSDKLoginButton alloc] init]; 
loginButton.center = self.view.center; 
[self.view addSubview:loginButton]; 

Trả lời

30

làm cho bạn sở hữu nút tùy chỉnh trong kịch bản này. Kết thúc hành động lên myButtonPressed.

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    self.loginButton = [[FBSDKLoginButton alloc] init]; 
    self.loginButton.hidden = YES; 
} 

- (void)myButtonPressed { 
    [self.loginButton sendActionsForControlEvents: UIControlEventTouchUpInside]; 
} 
+19

Giải pháp siêu bẩn xD: D – Abo3atef

+0

nó phù hợp với tôi. Cảm ơn bạn –

+0

Nope: 'lỗi nghiêm trọng: bất ngờ tìm thấy nil khi unwrapping một giá trị tùy chọn ' – MarksCode

12

Cập nhật cho 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) 
      } 
     } 
    } 
} 

Đối với Objective-C

Bạn có thể gọi phương pháp này trên UIButton nhấp chuột kiện

-(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"); 
     } 
    }]; 
} 
0

Từ Facebook documentat ion: (https://developers.facebook.com/docs/swift/login)

import FacebookCore 
import FacebookLogin 

func viewDidLoad() {  
    // Add a custom login button to your app 
    let myLoginButton = UIButton(type: .Custom)] 
    myLoginButton.backgroundColor = UIColor.darkGrayColor() 
    myLoginButton.frame = CGRect(0, 0, 180, 40); 
    myLoginButton.center = view.center; 
    myLoginTitle.setTitle("My Login Button" forState: .Normal) 

    // Handle clicks on the button 
    myLoginButton.addTarget(self, action: @selector(self.loginButtonClicked) forControlEvents: .TouchUpInside) 

    // Add the button to the view 
    view.addSubview(myLoginButton) 
} 

// Once the button is clicked, show the login dialog 
@objc func loginButtonClicked() { 
    let loginManager = LoginManager() 
    loginManager.logIn([ .PublicProfile ], viewController: self) { loginResult in 
    switch loginResult { 
    case .Failed(let error): 
     print(error) 
    case .Cancelled: 
     print("User cancelled login.") 
    case .Success(let grantedPermissions, let declinedPermissions, let accessToken): 
     print("Logged in!") 
    } 
} 
2

URL mới của tài liệu hướng dẫn về các nút tùy chỉnh:

https://developers.facebook.com/docs/facebook-login/ios/advanced

Hoặc nếu bạn chỉ muốn biết phải làm gì khi auth nút khai thác làm điều này trong "Nút khai thác" phương pháp (đừng quên để liên kết phương pháp đó với nút của bạn):

#import <FBSDKLoginKit/FBSDKLoginKit.h> 
#import <FBSDKCoreKit/FBSDKCoreKit.h> 

Sau đó

- (IBAction)facebookAuthButtonTapped:(id)sender { 

    FBSDKLoginManager *login = [[FBSDKLoginManager alloc] init]; 
    [login 
    logInWithReadPermissions: @[@"public_profile"] 
    fromViewController:self 
    handler:^(FBSDKLoginManagerLoginResult *result, NSError *error) { 
     if (error) { 
      NSLog(@"Process error"); 
     } else if (result.isCancelled) { 
      NSLog(@"Cancelled"); 
     } else { 
      NSLog(@"Logged in"); 
     } 
    }]; 
} 
Các vấn đề liên quan