IOS Swift和Facebook SDK

前端之家收集整理的这篇文章主要介绍了IOS Swift和Facebook SDK前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
让我先道歉,只是快速学习,所以我不是100%做我正在做的事情.我找到了许多关于在 swift中集成facebook登录的教程,并且看起来在应用程序中有效,但似乎我的代表永远不会被调用.我期望发生的是当用户登录时应该发生两件事情.

1)facebook的登录按钮应更改为注销按钮.
2)控制台命令应该打印我登录的,最终用户信息.

ViewController类是:

class ViewController: UIViewController,FBLoginViewDelegate {

    @IBOutlet var fbLoginView : FBLoginView!

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view,typically from a nib.

        self.fbLoginView.delegate = self
        self.fbLoginView.readPermissions = ["public_profile","email","user_friends"]
    }

    // Facebook Delegate Methods

    func loginViewShowingLoggedInUser(loginView : FBLoginView!) {
        println("User Logged In")
        println("This is where you perform a segue.")
    }

    func loginViewFetchedUserInfo(loginView : FBLoginView!,user: FBGraphUser){
        println("User Name: \(user.name)")
    }

    func loginViewShowingLoggedOutUser(loginView : FBLoginView!) {
        println("User Logged Out")
    }

    func loginView(loginView : FBLoginView!,handleError:NSError) {
        println("Error: \(handleError.localizedDescription)")
    }


    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}

AppDelegate部分是:

func application(application: UIApplication,didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    // Override point for customization after application launch.

    FBLoginView.self
    FBProfilePictureView.self

    func application(application: UIApplication,openURL url: NSURL,sourceApplication: NSString?,annotation: AnyObject) -> Bool {

        var wasHandled:Bool = FBAppCall.handleOpenURL(url,sourceApplication: sourceApplication)
        return wasHandled

    }

    return true
}

正如您所看到的,大部分内容都是直接来自教程,但无法正常工作.我已经配置了我的info.plist,FBLoginView出现并通过整个身份验证,但没有任何内容打印到控制台,按钮也没有改变.当我再次尝试点击它时,我获得了已经授权的应用程序,这应该意味着auth成功,但在我的应用程序中没有触发任何内容.

我有信心我的appdelegate可能出现问题,但对于swift来说是新手,我不知道是什么……

解决方法

我想建议你检查这些步骤:
– 在您的VC类中,导入FacebookSDK语句和FBLoginViewDelegate?
– 你的出口应该是:
//FB outlet
@IBOutlet var fbLoginView: FBLoginView

– 在您的AppDelegate.swift中,还导入了FacebookSDK?
– 你的AppDelegate.swift中的func中有一个func.It应该是:

import FacebookSDK

@UIApplicationMain
class AppDelegate:UIResponder,UIApplicationDelegate {

var window: UIWindow?


func application(application: UIApplication,didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    // Override point for customization after application launch.

    //FB implementation
    FBLoginView.self
    FBProfilePictureView.self


    return true
}

//FB Method handles what happens after authentication
func application (application:UIApplication,openURL url:NSURL,sourceApplication:NSString?,annotation:AnyObject) -> Bool {
    //test var
    var wasHandled:Bool = FBAppCall.handleOpenURL(url,sourceApplication: sourceApplication)
// attempt to extract a token from the url
return wasHandled

}
原文链接:https://www.f2er.com/iOS/335139.html

猜你在找的iOS相关文章