2015年12月18日 星期五

Swift 2 Xcode7.1 Facebook SDK + 自訂Facebook Button

一、下載Facebook SDK
二、Xcode開新專案
編輯專案下的info.plist
新增以下設定
Facebook developer key 記得先去申請 Facebook Developer
<key>CFBundleURLTypes</key>
<array>
  <dict>
    <key>CFBundleURLSchemes</key>
    <array>
      <string>fb{your-app-id}</string>
    </array>
  </dict>
</array>
<key>FacebookAppID</key>
<string>{your-app-id}</string>
<key>FacebookDisplayName</key>
<string>{your-app-name}</string> 
<key>NSAppTransportSecurity</key>
<dict>
  <key>NSExceptionDomains</key>
  <dict>
    <key>facebook.com</key>
    <dict>
      <key>NSIncludesSubdomains</key> <true/>        
      <key>NSThirdPartyExceptionRequiresForwardSecrecy</key> <false/>
    </dict>
    <key>fbcdn.net</key>
    <dict>
      <key>NSIncludesSubdomains</key> <true/>
      <key>NSThirdPartyExceptionRequiresForwardSecrecy</key>  <false/>
    </dict>
    <key>akamaihd.net</key>
    <dict>
      <key>NSIncludesSubdomains</key> <true/>
      <key>NSThirdPartyExceptionRequiresForwardSecrecy</key> <false/>
    </dict>
  </dict>
</dict>


三、將.framework檔放入
先new Group,名稱為Frameworks
在將FBSDKCoreKit.framework,FBSDKLoginKit.framework,FBSDKShareKit.framework放入Frameworks裡

四、寫程式
1.AppDelegate.swift

import UIKit
import CoreData
import FBSDKCoreKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


    func application(application: UIApplication,
        openURL url: NSURL,
        sourceApplication: String?,
        annotation: AnyObject) -> Bool {
            return FBSDKApplicationDelegate.sharedInstance().application(
                application,
                openURL: url,
                sourceApplication: sourceApplication,
                annotation: annotation)
    }
.....
}
2.ViewController.swift

import UIKit
import FBSDKCoreKit
import FBSDKShareKit
import FBSDKLoginKit

class ViewController: UIViewController {
    let fbLoginManager : FBSDKLoginManager = FBSDKLoginManager()
    override func viewDidLoad() {
        super.viewDidLoad()
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    @IBAction func loginAction(sender: AnyObject) {
        
        fbLoginManager.logInWithReadPermissions(["email"],fromViewController: self.parentViewController, handler: { (result, error) -> Void in
            if (error == nil){
                let fbloginresult : FBSDKLoginManagerLoginResult = result
                if(fbloginresult.grantedPermissions.contains("email"))
                {
                    self.getFBUserData()
                    self.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){
                    print(result)
                }
            })
        }
    }
    @IBAction func logoutAction(sender: AnyObject) {
        
        fbLoginManager.logOut()
    }
    
}


打完收功...

2015年12月17日 星期四

Swift 2 Xcode7.1 Google Map SDK example

前提:
來抱怨一下,相信大家也是這樣,只要Xcode版本一大改,很多套件都需要更新
以前我是用下載他的sdk裡面的framework檔放進去,但是現在用cocoapods來處理
本篇是用Swift2.0所開發,Object-C的話... 我不會

一、開始Terminal

安裝cocoapods
sudo gem install cocoapods

二、開新專案
開新增案後,在將專案關閉
專案名稱:TestMap
到專案目錄下,新增Podfile檔
以下是內容,編輯完後儲存
source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '8.1'
pod 'GoogleMaps'

在用Terminal cd到專案目錄
cd ........./TestMap

在用Terminal,執行以下的指令
pod install

四、取得Google Developer Console Key
點此連結申請

五、最後寫程式
請打開TestMap.xcworkspace
1.AppDelegate.swift
...
import GoogleMaps

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        // Override point for customization after application launch.
        GMSServices.provideAPIKey("key")
        return true
    }
...
}

2.ViewController.swift
import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        let camera = GMSCameraPosition.cameraWithLatitude(-33.86,
            longitude: 151.20, zoom: 6)
        let mapView = GMSMapView.mapWithFrame(CGRectZero, camera: camera)
        mapView.myLocationEnabled = true
        self.view = mapView
        
        let marker = GMSMarker()
        marker.position = CLLocationCoordinate2DMake(-33.86, 151.20)
        marker.title = "Sydney"
        marker.snippet = "Australia"
        marker.map = mapView
    }


}

3.Run... Finish

參考鏈結:
https://developers.google.com/maps/documentation/ios-sdk/start/
https://www.youtube.com/watch?v=q6tg3gl7CsU