WechatKit icon indicating copy to clipboard operation
WechatKit copied to clipboard

一款快速实现微信第三方登录的框架(Swift版) SDK 1.8.5

WechatKit

Build Status Swift version Documentation SwiftLint Carthage compatible CocoaPods Compatible License MIT GitHub release FOSSA Status


Getting Started

  • CocoaPods
pod 'WechatKit'
  • Carthage

安装Carthage

github "starboychina/WechatKit"

Setting

  • 设置URL scheme

    在Xcode中,选择你的工程设置项,选中“TARGETS”一栏,在“info”标签栏的“URL type“添加“URL scheme”为你在微信开放平台,注册的应用程序id

Setting

  • IOS9以后 需要添加weixin到白名单(如图)

    或以源代码方式打开info.plist, 并添加以下内容.

	<key>LSApplicationQueriesSchemes</key>
	<array>
		<string>wechat</string>
		<string>weixin</string>
	</array>

Setting

  • AppDelegate的handleOpenURL和openURL方法:

    在AppDelegate.swift中添加import WechatKit

    /// iOS 9 以后将弃用 请用下面的方法 #20
    /// -> NS_DEPRECATED_IOS(4_2, 9_0, "Please use application:openURL:options:") __TVOS_PROHIBITED;
    func application(_ application: UIApplication, open url: URL, sourceApplication: String?, annotation: Any) -> Bool {
        return WechatManager.shared.handleOpenURL(url)
        // 如需要使用其他第三方可以 使用 || 连接 其他第三方库的handleOpenURL
        // return WechatManager.shared.handleOpenURL(url) || TencentOAuth.HandleOpenURL(url) || WeiboSDK.handleOpenURL(url, delegate: SinaWeiboManager.shared) ......
    }
    /// iOS 9.0 以后请使用这个方法
    /// Please use this (application:openURL:options:)
    func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool {
        return WechatManager.shared.handleOpenURL(url)
        // 如需要使用其他第三方可以 使用 || 连接 其他第三方库的handleOpenURL
        // return WechatManager.shared.handleOpenURL(url) || TencentOAuth.HandleOpenURL(url) || WeiboSDK.handleOpenURL(url, delegate: SinaWeiboManager.shared) ......
    }

Usage

  • 注册app
  WechatManager.appid = "微信开放平台,注册的应用程序id"
  WechatManager.universalLink = "微信开放平台,注册的应用程序所对应的 Universal Links"
  WechatManager.appSecret = "微信开放平台,注册的应用程序Secret"
  • 检测微信是否安装
  WechatManager.shared.isInstalled()
  • 使用微信登录

    默认会记住openid,以及access_token,在token还在有效期时,调用checkAuth则不会打开微信客户端,直接使用token和微信服务器获取认证信息

    WechatManager.shared.checkAuth { result in
        switch result {
        case .failure(let errCode)://登录失败
            print(errCode)
        case .success(let value)://登录成功 value为([String: String]) 从微信返回的openid access_token 以及 refresh_token
            print(value) //当前是在子线程,如需回到主线程调用 DispatchQueue.main.async { print(value) }
        }
    }
  • 获取微信用户信息
  WechatManager.shared.getUserInfo { result in
      switch result {
      case .failure(let errCode)://获取失败
          print(errCode)
      case .success(let value)://获取成功 value为([String: String]) 微信用户基本信息
          print(value) //当前是在子线程,如需回到主线程调用 DispatchQueue.main.async { print(value) }
      }
  }
  • 退出登录

    由于默认会记住openid,以及access_token,如需要切换用户则需要退出登录.

WechatManager.shared.logout()

  • 分享到微信
  WechatManager.shared.shareDelegate = self
  /**
  分享

  - parameter scence:      请求发送场景
  - parameter image:       消息缩略图
  - parameter title:       标题
  - parameter description: 描述内容
  - parameter url:         地址
  - parameter extInfo:     app分享信息(点击分享内容返回程序时,会传给WechatManagerShareDelegate.showMessage(message: String)
  */
  WechatManager.shared.share(scence: WXScene, image: UIImage?, title: String, description: String, url: String? = default, extInfo: String? = default)
  • Delegation

分享Delegation

    //app分享后 点击分享返回时调用
    func showMessage(message: String)

License

FOSSA Status