2016-07-29 14 views
0

Ich entwickle eine App in iOS für Facebook Login. Ich kann mich mit Facebook einloggen, aber ich konnte das Zugangs-Token nicht bekommen.So erhalten Sie Zugang Token in Facebook iOS

Das ist mein Delegatmethode:

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject:AnyObject]?) -> Bool { 
    // Override point for customization after application launch. 
    print("at delegate") 
    var configureError: NSError? 
    GGLContext.sharedInstance().configureWithError(&configureError) 
    assert(configureError == nil, "Error configuring Google services: \(configureError)") 
    GIDSignIn.sharedInstance().delegate = self 
    FBSDKLoginButton.classForCoder() 

     return FBSDKApplicationDelegate.sharedInstance().application(application, 

                     didFinishLaunchingWithOptions:launchOptions) 
} 
func application(application: UIApplication, openURL url: NSURL, sourceApplication: String?, annotation: AnyObject?) -> Bool { 
    print("at delegatehg") 
      return FBSDKApplicationDelegate.sharedInstance().application(application, openURL: url, sourceApplication: sourceApplication, annotation: annotation) || GIDSignIn.sharedInstance().handleURL(url,sourceApplication: sourceApplication, 
                                                   annotation: annotation) 
} 

Und mit meinem Viewcontroller, ich habe eine Schaltfläche in dem Storyboard für die Login Facebook. Das ist mein Inhalt in Taste:

@IBAction func fbLogin(sender: FBSDKLoginButton) {  

     if (FBSDKAccessToken.currentAccessToken() != nil){ 
      print("token is not nil") 
      let Level2 = self.storyboard!.instantiateViewControllerWithIdentifier("facebook") as? UIViewController 
      self.presentViewController(Level2!, animated: true, completion: nil) 
     } else{ 
     var fbLoginManager : FBSDKLoginManager = FBSDKLoginManager() 
     fbLoginManager.logInWithReadPermissions(["public_profile", "email", "user_friends"], handler: { (result, error) -> Void in 

      if (error == nil){ 
       var fbloginresult : FBSDKLoginManagerLoginResult = result 
       if(fbloginresult.grantedPermissions.contains("email")) 
       { 
        self.getFBUserData() 
        fbLoginManager.logOut() 

       } 
      } 
     }) 
    } 
    } 
    func getFBUserData(){ 
     FBSDKGraphRequest.init(graphPath: "me", parameters: ["fields":"name,email,picture.type(large),gender"]).startWithCompletionHandler { (connection, result, error) -> Void in 

      let strName: String = (result.objectForKey("name") as? String)! 
         print(result) 
      let Gender : String = (result.objectForKey("gender") as? String)! 
      let Id = (result.objectForKey("id") as? String)! 
      let deviceId = UIDevice.currentDevice().identifierForVendor?.UUIDString 
      let UUID = NSUUID().UUIDString 
      print(UUID) 
      var DeviceId = deviceId 
      print(DeviceId) 
      print(Id) 
      print(Gender) 
      print(strName) 
      var strEmail : String 
      if ((result.objectForKey("email") as? String) != nil){ 
       strEmail = (result.objectForKey("email") as? String)! 
      }else { 
       strEmail = "not available" 
      } 
      //let strEmail: String = (result.objectForKey("email") as? String)! 

      var userID = result["id"] as! NSString 
      var facebookProfileUrl = "http://graph.facebook.com/\(userID)/picture?type=large" 
      print(strEmail) 

      print(strEmail) 
      let request = NSMutableURLRequest(URL: NSURL(string: "http://projects.svirtzone.com/wified/api/authentication.php")!) 
      request.HTTPMethod = "POST" 
      let postString = "username=\(strName)&email=\(strEmail)&action=auth&pic=\(facebookProfileUrl)&dob=1989/05/05&logintype=Facebook&gender=\(Gender)&device_type=Mac&facebookid=\(Id)&agerange=15&deviceid=\(deviceId)&accesstoken=fdfsfs" 
      request.HTTPBody = postString.dataUsingEncoding(NSUTF8StringEncoding) 
      let task = NSURLSession.sharedSession().dataTaskWithRequest(request) { data, response, error in 
       guard error == nil && data != nil else {               // check for fundamental networking error 
        print("error=\(error)") 
        return 
       } 
       if let httpStatus = response as? NSHTTPURLResponse where httpStatus.statusCode != 200 {   // check for http errors 
        print("statusCode should be 200, but is \(httpStatus.statusCode)") 
        print("response = \(response)") 
       } 

       let responseString = NSString(data: data!, encoding: NSUTF8StringEncoding) 
       print("responseString = \(responseString)") 
      } 
      task.resume() 
      let Level2 = self.storyboard!.instantiateViewControllerWithIdentifier("facebook") as? UIViewController 
      self.presentViewController(Level2!, animated: true, completion: nil) 

      if (error == nil){ 
       print(result) 
      } 
     } 

Eine Sache habe ich bemerkt, dass, wenn ich bei Facebook bin anmelden, es nicht application() method in AppDelegate nicht nennen. Es hat auch keine Zeile in meinem Code loginView.delegate = self hinzugefügt. Dies könnte ein Grund für mein Problem sein, und ich weiß nicht, wie ich diese Zeile gemäß meinem Code hinzufügen soll.

Wenn ich versuche, Zugriffstoken durch diese Linie zu bekommen:

var token = FBSDKAccessToken.currentAccessToken().tokenString 
print(token) 

ich diese Störung erhalte:

Fatal error: unexpectedly found nil while unwrapping an Optional value

Kann mir jemand bitte helfen?

Antwort

0

Sie Delegatmethode verwenden können:

- (void)fbDialogLogin: 
(NSString *) 
token expirationDate:(NSDate *) 
expirationDate 
+0

für weitere Informationen auf die folgende Frage beziehen: Facebook Zugriffstoken –

+0

können Sie mir sagen, klar bitte @ Bandna Prasher –

+0

Like ... fbdialoglogin –

Verwandte Themen