IOS Swift和Facebook SDK [英] IOS Swift and Facebook SDK

查看:113
本文介绍了IOS Swift和Facebook SDK的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我首先道歉,只是学习迅速,所以我不是100%在做什么。我已经发现了很多关于在swift中集成facebook登录的教程,并且有什么似乎在应用程序中工作,但似乎我的代表从来没有被调用。我期望发生的是当用户登录时应该发生两件事情。



1)Facebook的登录按钮应该更改为注销按钮。

2)控制台命令应该打印我登录,最终用户信息。



ViewController类

  class ViewController:UIViewController,FBLoginViewDelegate {

@IBOutlet var fbLoginView:FBLoginView!

覆盖func viewDidLoad(){
super.viewDidLoad()
//加载视图后,通常从笔尖进行任何其他设置。

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

// Facebook代理方法

func loginViewShowingLoggedInUser(loginView:FBLoginView!){
println(用户登录)
println(这是执行segue的地方。 )
}

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

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

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


覆盖func didReceiveMemoryWarning(){
super.didReceiveMemoryWarning()
//处理可以重新创建的任何资源天。
}


}

应用程序部分是:

  func应用程序(应用程序:UIApplication,didFinishLaunchingWithOptions launchOptions:[NSObject:AnyObject]?) > Bool {
//在应用程序启动后重写自定义点。

FBLoginView.self
FBProfilePictureView.self

func应用程序(应用程序:UIApplication,openURL url:NSURL,sourceApplication:NSString?注释:AnyObject) - > Bool {

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

}

返回true
}

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



我觉得有一些可能是错误的我的appdelegate但是是新的swift,我不知道什么..

解决方案

我想建议你这些步骤的嘘声:
- 在你的VC类中,导入FacebookSDK语句和FBLoginViewDelegate?
- 您的出口应该是:

  // FB outlet 
@IBOutlet var fbLoginView:FBLoginView

- 在AppDelegate.swift中,还导入了FacebookSDK?
- 您在AppDelegate.swift中的func中有一个func。它应该是:

  import FacebookSDK 

@UIApplicationMain
class AppDelegate:UIResponder,UIApplicationDelegate {

  var window:UIWindow? 


func应用程序(应用程序:UIApplication,didFinishLaunchingWithOptions launchOptions:[NSObject:AnyObject]?) - > Bool {
//在应用程序启动后重写自定义点。

// FB实现
FBLoginView.self
FBProfilePictureView.self


返回true
}

// FB方法处理身份验证后发生的情况
func应用程序(应用程序:UIApplication,openURL url:NSURL,sourceApplication:NSString?注释:AnyObject) - > Bool {
// test var
var wasHandled:Bool = FBAppCall.handleOpenURL(url,sourceApplication:sourceApplication)
//尝试从url中提取一个令牌
return wasHandled

}


Let me apologize first, just learning swift, so I am not 100% on what I am doing. I have found many tutorials on integrating a facebook login in swift and have what appears to be working in the app, but it seems my delegates are never getting called. What I expect to happen is when a user logins 2 things should happen.

1) The login button for facebook should change to a logout button.
2) A console command should print that I logged in, and ultimately user information.

ViewController Class is:

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 sections are:

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
}

As you can see, most of this is straight from the tutorials but is not working correctly. I've got my info.plist configured, and the FBLoginView appears and goes thru the entire authentication, but nothing is printed to console and the button doesn't change.
When I try to click it again, I get app already authorized, which should mean that the auth was successful, but nothing got triggered back in my app.

I feel confident that something is probably wrong with my appdelegate but being new to swift, I have no clue what..

解决方案

I would like to suggest you the cheking of these steps: -in your VC class, import FacebookSDK statement and FBLoginViewDelegate ? - your outlet should be:

//FB outlet
@IBOutlet var fbLoginView: FBLoginView

- in your AppDelegate.swift, also imported the FacebookSDK ? - you have a func in a func in your AppDelegate.swift.It should be:

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

}

这篇关于IOS Swift和Facebook SDK的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆