第三方 SDK 还是少集成的好,不仅体积不小,而且你也不知道他在你的 App 里做了什么。对于这些 SDK 只能请出 MongkeyKing 齐天大圣来收服了。

安装

source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '8.0'
use_frameworks!

pod 'MonkeyKing', '~> 0.5'

基本使用

  1. 修改 plist Info.plist, 添加 URL Type:

    URL Scheme for WeChat

如果是 iOS 9, 那么你还需要在 Info.plist 设置 LSApplicationQueriesSchemes:

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

设置 NSAppTransportSecurity in your Info.plist:

	<dict>
		<key>NSExceptionDomains</key>
		<dict>
			<key>api.weibo.com</key>
			<dict>
				<key>NSIncludesSubdomains</key>
				<true/>
				<key>NSThirdPartyExceptionMinimumTLSVersion</key>
				<string>TLSv1.0</string>
				<key>NSThirdPartyExceptionRequiresForwardSecrecy</key>
				<false/>
			</dict>
		</dict>
	</dict>

结果如下图所示

URL Scheme for WeChat

  1. 准备要分享的消息:
    @IBAction func shareURLToWeChatSession(sender: UIButton) {

        MonkeyKing.registerAccount(.WeChat(appID: weChatAppID))

        let message = MonkeyKing.Message.WeChat(.Session(info: (
            title: "Session",
            description: "Hello Session",
            thumbnail: UIImage(named: "rabbit"),
            media: .URL(NSURL(string: "http://www.apple.com/cn")!)
        )))

        MonkeyKing.shareMessage(message) { success in
            print("shareURLToWeChatSession success: \(success)")
        }
    }
  1. 添加回调到 AppDelegate
    func application(application: UIApplication, openURL url: NSURL, sourceApplication: String?, annotation: AnyObject) -> Bool {

        if MonkeyKing.handleOpenURL(url) {
            return true
        }

        return false
    }

大功告成!

入如果你不希望每次都 registerAccount 那么可以如下处理:

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

    MonkeyKing.registerAccount(.WeChat(appID: "wxd930ea5d5a258f4f"))

    return true
}

OAuth

Weibo OAuth:

let account = MonkeyKing.Account.Weibo(appID: weiboAppID, appKey: weiboAppKey, redirectURL: weiboRedirectURL)

MonkeyKing.OAuth(account) { (dictionary, response, error) -> Void in
    print("dictionary \(dictionary) error \(error)")
}

如果用户手机上没有微博 App,MongkeyKing 将使用 Web OAuth:

weiboOAuth

More

如果你希望使用 UIActivityViewController 来分享, MonkeyKing 提供了 AnyActivity.

System Share

你可以从 Demo 中查看相关实现.