登录后,FBSDKGraphRequest不会重定向到应用程序

时间:2015-06-08 09:40:16

标签: ios facebook swift

我正在使用FBSDKGraphRequest在我的iOS应用程序中实现Facebook登录。

但是在登录完成并且用户确认后,我没有被重定向到我的应用程序。

这是我的代码:

FBSDKGraphRequest(graphPath: "me", parameters:   nil).startWithCompletionHandler({
        (connection, result, error: NSError!) -> Void in
        if error == nil {
            println("\(result)")
        } else {
            println("\(error)")
            self.performSegueWithIdentifier("home", sender: self)
        }
    })

2 个答案:

答案 0 :(得分:1)

在你的appdelegate中:

import UIKit

import FBSDKCoreKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?

    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        return FBSDKApplicationDelegate.sharedInstance().application(application, didFinishLaunchingWithOptions: launchOptions)
    }
    func applicationWillResignActive(application: UIApplication) {    
        FBSDKAppEvents.activateApp()
    }
    func application(application: UIApplication, openURL url: NSURL, sourceApplication: String?, annotation: AnyObject?) -> Bool {
        return FBSDKApplicationDelegate.sharedInstance().application(application, openURL: url, sourceApplication: sourceApplication, annotation: annotation)
    }
}

在视图控制器中:

import UIKit
import FBSDKLoginKit

class ViewController: UIViewController {

    var dict : NSDictionary!

    override func viewDidLoad() {
        super.viewDidLoad()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }

    @IBAction func btnFBLoginPressed(sender: AnyObject) {
        var fbLoginManager : FBSDKLoginManager = FBSDKLoginManager()
        fbLoginManager .logInWithReadPermissions(["email"], handler: { (result, error) -> Void in
            if (error == nil){
                var fbloginresult : FBSDKLoginManagerLoginResult = result
                if(fbloginresult.grantedPermissions.containsObject("email"))
                {
                    self.getFBUserData()
                    fbLoginManager.logOut()
                }
            }
        })
    }

    func getFBUserData(){
        if((FBSDKAccessToken.currentAccessToken()) != nil){
            FBSDKGraphRequest(graphPath: "me", parameters: ["fields": "id, name, first_name, last_name, picture.type(large), email"]).startWithCompletionHandler({ (connection, result, error) -> Void in
                if (error == nil){
                    self.dict = result as NSDictionary
                    println(result)
                    println(self.dict)
                    NSLog(self.dict.objectForKey("picture")?.objectForKey("data")?.objectForKey("url") as String)
                }
            })
        }
    }
}

在plist.info文件中添加属性。

enter image description here

添加网址类型,FacebookAppID和FacebookDisplayName属性

有关更多内容,您可以执行https://developers.facebook.com/docs/ios/getting-started

的步骤

答案 1 :(得分:0)

您必须在应用代理中添加句柄网址代码,如下所示 -

- (BOOL)application: (UIApplication *)application
            openURL: (NSURL *)url
  sourceApplication: (NSString *)sourceApplication
         annotation: (id)annotation
{

    if ([[url scheme] isEqualToString:FACEBOOK_SCHEME])
        return [FBSession.activeSession handleOpenURL:url];


    return NO;
}

您必须在目标设置中添加网址方案 - >信息 - >网址类型

enter image description here