Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
The demonstration of how to use Facebook SDK
//
// ViewController.swift
// FacebookSDKDemo
//
// Created by rayc5 on 2015/08/26.
// Copyright (c) 2015年 rayc5. All rights reserved.
//
import UIKit
import FBSDKCoreKit
import FBSDKLoginKit
class ViewController: UIViewController, FBSDKLoginButtonDelegate {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
// ログインボタンの設置
let loginButton = FBSDKLoginButton()
loginButton.center = self.view.center
loginButton.delegate = self
loginButton.readPermissions = ["public_profile"/*, "email"*/, "user_friends"]
self.view.addSubview(loginButton)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// MARK: FBSDKLoginButtonDelegate
// ログイン時の処理
func loginButton(loginButton: FBSDKLoginButton!, didCompleteWithResult result: FBSDKLoginManagerLoginResult!, error: NSError!){
println("User Logged In")
if ((error) != nil)
{
// Process error
}
else if result.isCancelled {
// Handle cancellations
}
else
{
// ログイン成功時にユーザーと友達の情報を取得
returnUserData()
friendsList()
// If you ask for multiple permissions at once, you
// should check if specific permissions missing
if result.grantedPermissions.contains("email")
{
// Do work
}
}
}
// ログアウト時の処理
func loginButtonDidLogOut(loginButton: FBSDKLoginButton!) {
println("User Logged Out")
}
// MARK: Facebook SDK methods
// ユーザーの情報を取得する
func returnUserData() {
let graphRequest : FBSDKGraphRequest = FBSDKGraphRequest(graphPath: "me",
parameters: ["fields": "id,email,gender,link,locale,name,timezone,updated_time,verified,last_name,first_name,middle_name"])
graphRequest.startWithCompletionHandler({ (connection, result, error) -> Void in
if ((error) != nil)
{
// Process error
println("Error: \(error)")
}
else
{
println("fetched user: \(result)")
let userName : NSString = result.valueForKey("name") as! NSString
println("User Name is: \(userName)")
}
})
}
// 友達のリストを出力する
func friendsList() {
let graphRequest : FBSDKGraphRequest = FBSDKGraphRequest(graphPath: "me/friends", parameters: nil)
graphRequest.startWithCompletionHandler( { (connection, result, error) -> Void in
if ((error) != nil)
{
// Process error
println("Error: \(error)")
return
}
// 友達の数を取得
let summary = result.valueForKey("summary") as! NSDictionary
let counts = summary.valueForKey("total_count") as! NSNumber
let graphRequest : FBSDKGraphRequest = FBSDKGraphRequest(graphPath: "me/taggable_friends", parameters: ["fields": "id,name,picture", "limit": "\(counts)"])
graphRequest.startWithCompletionHandler( { (connection, result, error) -> Void in
if ((error) != nil)
{
// Process error
println("Error: \(error)")
return
}
else
{
// 友達を一人ずつ出力
let friends = result.valueForKey("data") as! NSArray
var count = 1
if let array = friends as? [NSDictionary] {
for friend : NSDictionary in array {
let name = friend.valueForKey("name") as! NSString
println("\(count) \(name)")
count++
}
}
}
})
})
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment