Swift版 share sdk 第三方登陸facebook,推特,googleplus集成 全教程

  最近在寫一個swift項目,須要用到facebook,googleplus,推特,的第三方登陸,另一個作oc項目的同事和我說他的shareSDK第三方登陸有一個坑一直解決不了,讓我不要用sharesdk,可是樓主是一個有追求的人,毅然投身入sharesdk中。php

  第壹步:獲取sharesdk appkey.ios

  如何獲取appkey:http://bbs.mob.com/forum.php?mod=viewthread&tid=8212&extra=page%3D1web

  第二步:下載sharesdkswift

  下載連接:http://www.mob.com/downloadDetail/ShareSDK/ios 注意事項:sharesdk 的swift版 實際上是經過橋接oc版的來實習的app

  第三步: 集成sdk,因爲樓主只須要集成facebook,googleplus,推特,的第三方登陸,橋接文件以下,官方文檔中第5步有個小坑google

  集成教程:http://wiki.mob.com/swift-調用-sharesdk/ spa

  橋接文件代碼:code

#ifndef MoveApp_Bridging_Header_h
#define MoveApp_Bridging_Header_h


#import <ShareSDK/ShareSDK.h>
#import <ShareSDKConnector/ShareSDKConnector.h>
#import <FacebookConnector/FacebookConnector.h>
#import <GooglePlusConnector/SSDKGooglePlusControllerStyle.h>

#endif /* MoveApp_Bridging_Header_h */

  第四步:配置AppDelegate.swift文件,直接上代碼:orm

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey : Any]? = nil) -> Bool {
  ShareSDK.registerApp("你的sharesdkappkey", activePlatforms: [
            SSDKPlatformType.typeFacebook.rawValue,
            SSDKPlatformType.typeTwitter.rawValue,
            SSDKPlatformType.typeGooglePlus.rawValue,
            ],
            
         onImport: { (platform : SSDKPlatformType) in
            switch platform
            {
            default:
                break
            }
        }) { (platform : SSDKPlatformType, appInfo : NSMutableDictionary?) in
            
            switch platform
            {
                
            case SSDKPlatformType.typeFacebook:
                //設置Facebook應用信息,其中authType設置爲只用SSO形式受權
                
                appInfo?.ssdkSetupFacebook(byApiKey: "107704292745179",
                                           appSecret : "38053202e1a5fe26c80c753071f0b573",
                                           authType : SSDKAuthTypeBoth)
                
            case SSDKPlatformType.typeTwitter:
                //設置Twitter應用信息
                appInfo?.ssdkSetupTwitter(byConsumerKey: "LRBM0H75rWrU9gNHvlEAA2aOy",
                                          consumerSecret : "gbeWsZvA9ELJSdoBzJ5oLKX0TU09UOwrzdGfo9Tg7DjyGuMe8G",
                                          redirectUri : "http://www.baidu.com")
                //設置gooleplus應用信息
            case SSDKPlatformType.typeGooglePlus:
                appInfo?.ssdkSetupGooglePlus(byClientID: "232554794995.apps.googleusercontent.com", clientSecret: "可不填", redirectUri: "http://localhost");
                break
                
            default:
                break
            }
            
        }
     return true
    }

    }

第五部:控制器點擊按鈕受權登陸獲取令牌
func OAuth(sender: UIButton) {
        
        //受權
        ShareSDK.authorize(SSDKPlatformType.須要的第三方類型, settings: nil, onStateChanged: { (state : SSDKResponseState, user : SSDKUser?, error : Error?) -> Void in
            
            switch state{
                
            case SSDKResponseState.success: print("受權成功,用戶信息爲\(user)\n ----- 受權憑證爲\(user?.credential)")
            case SSDKResponseState.fail:    print("受權失敗,錯誤描述:\(error)")
            case SSDKResponseState.cancel:  print("操做取消")
                
            default:
                break
            }
        })
    
}

全教程終blog

注意事項:這時候社交平臺web受權窗口仍然不通有以下可能:

  redirectUri 與 該第三方開發者應用的回調地址不一樣,或者配置錯誤

  或者社交平臺appkey 和 secrt 有錯誤.

最後:

官方的開發者應用設置網址:各社交平臺申請AppKey的網址及申請流程彙總

相關文章
相關標籤/搜索