Recently, when I was writing a swift project, I needed to use the third party login of facebook, googleplus, Twitter. Another colleague who worked on the oc project told me that his shareSDK third party login had a pit that I couldn't solve, so I didn't use sharesdk, but the landlord was a person who pursued it and plunged into sharesdk.
Step 1: Get sharesdk appkey.
How to get appkey: http://bbs.mob.com/forum.php?mod=viewthread&tid=8212&extra=page%3D1
Step 2: Download sharesdk
Download link: http://www.mob.com/downloadDetail/ShareSDK/ios Note: sharesdk's swift version is actually practiced by bridging the oc version
Step 3: Integrate sdk, because the landlord only needs to integrate facebook, googleplus, Twitter, third party login, the bridge file is as follows, step 5 of the official document has a small pit.
Integrated tutorials: http://wiki.mob.com/swift-call-sharesdk/
Bridge file 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 */
Step 4: Configure the AppDelegate.swift file and code it directly:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey : Any]? = nil) -> Bool {
ShareSDK.registerApp("Your 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: //Set up Facebook Application information, where authType Set to Use Only SSO Formal authorization appInfo?.ssdkSetupFacebook(byApiKey: "107704292745179", appSecret : "38053202e1a5fe26c80c753071f0b573", authType : SSDKAuthTypeBoth) case SSDKPlatformType.typeTwitter: //Set up Twitter Application information appInfo?.ssdkSetupTwitter(byConsumerKey: "LRBM0H75rWrU9gNHvlEAA2aOy", consumerSecret : "gbeWsZvA9ELJSdoBzJ5oLKX0TU09UOwrzdGfo9Tg7DjyGuMe8G", redirectUri : "http://www.baidu.com") //Set up gooleplus Application information case SSDKPlatformType.typeGooglePlus: appInfo?.ssdkSetupGooglePlus(byClientID: "232554794995.apps.googleusercontent.com", clientSecret: "Can not fill", redirectUri: "http://localhost"); break default: break } }
return true } }
Part 5: Controller clicks button to authorize login to get token
func OAuth(sender: UIButton) { //To grant authorization ShareSDK.authorize(SSDKPlatformType.Types of third parties needed, settings: nil, onStateChanged: { (state : SSDKResponseState, user : SSDKUser?, error : Error?) -> Void in switch state{ case SSDKResponseState.success: print("Authorized success,User information is\(user)\n ----- The certificate of authorization is\(user?.credential)") case SSDKResponseState.fail: print("privilege grant failed,Wrong description:\(error)") case SSDKResponseState.cancel: print("Operation cancelled") default: break } }) }
End of the whole course
Note: At this time, the social platform web authorization window still does not have the following possibilities:
redirectUri differs from the callback address of the third-party developer's application, or is misconfigured
Or the social platform appkey and secrt have errors.
Last:
Official Developer Application Settings Web Site: Summary of AppKey's Web Sites and Application Procedures for Social Platforms