美文网首页
使用MonkeyKing进行第三方分享一

使用MonkeyKing进行第三方分享一

作者: 山有木枝壮 | 来源:发表于2017-05-17 13:45 被阅读314次

一、MonkeyKing介绍

1、MonkeyKing可以用来进行第三方应用的分享,目前支持的有微信、qq、微博、支付宝、Pocket,最近新增了Twitter
2、MonkeyKing还可以支持OAuth授权,目前主流的第三方登录(说的是国内),都是采用的这种授权方式,授权后就可以拿到对应的openid/access_token等信息,然后用于客户端登录
3、MonkeyKing还支持微信和支付宝支付,这部分需要后台配合
今天主要介绍MonkeyKing分享

二、MonkeyKing集成
1、参照github地址https://github.com/nixzhu/MonkeyKing,可以使用CocoaPods和cathage的方式集成,当然,也可以手动集成。使用CocoaPods集成如下

pod 'MonkeyKing' '1.2.3'

2、在info.plist中配置URL types和LSApplicationQueriesSchemes

Paste_Image.png

URL types也可以在对应的target->info->URL Types中添加,主要用来设置帐号对应的信息,设置之后分享就可以带上app对应的信息

Paste_Image.png

注意

这里的账号设置有一些规则,
1、微信是appID
2、qq好友和qq空间使用的不是一个URL schemes
QQ空间是 tencent+appID
QQ好友是 QQ+appID的十六进制
比如appID为1104881792,则对应的qq空间schemes为tencent1104881792, 对应qq好友的schemes为QQ41db2880

3、注册帐号,可以将所有的帐号信息用一个enum保存起来,然后在
<pre>func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
MonkeyKing.registerAccount(.weChat(appID: "xxx", appKey: "yyy"))
return true
}
</pre>
中注册对应的帐号信息
4、处理分享的回调,这里主要用来打开分享的第三方页面,或者授权页面
<pre>func application(_ application: UIApplication, openURL url: URL, sourceApplication: String?, annotation: Any) -> Bool {

if MonkeyKing.handleOpenURL(url) {
    return true
}

return false

}</pre>

三、MonkeyKing分享

1、MonkeyKing进行分享的数据主要是一个Info的元组

public typealias Info = (title: String?, description: String?, thumbnail: UIImage?, media: Media?)

所有的分享都可以使用这个对象,分别代表标题、内容、缩略图,分享类型。Media分享类型主要有以下几种,qq好友分享的如果为纯文本,Media设置为nil(还有很多的特殊规则,需要去看各自的api,当然也可以demo中手动修改进行测试,我就不测试了,大家自己跑个demo就知道了)
<pre>
public enum Media {
case url(URL) // 分享链接
case image(UIImage) // 分享图片
case audio(audioURL: URL, linkURL: URL?) // 分享
case video(URL)
case file(Data)
}
</pre>

2、以微信分享给好友为例,创建好需要分享的Info信息之后,需要根据分享的类型创建Message,其中Message为一个enum,包含了所有可以分享的类型以及二级类型(比如微信朋友圈、微信好友)
<pre>

public enum Message {

public enum WeChatSubtype {
        case session(info: Info)
        case timeline(info: Info)
        case favorite(info: Info)
        var scene: String {
            switch self {
            case .session:
                return "0"
            case .timeline:
                return "1"
            case .favorite:
                return "2"
            }
        }
        var info: Info {
            switch self {
            case .session(let info):
                return info
            case .timeline(let info):
                return info
            case .favorite(let info):
                return info
            }
        }
    }
    case weChat(WeChatSubtype)

}
</pre>
创建Message,然后使用MonkeyKing的deliver方法分享数据,并得到回调的信息
<pre>
var message = MonkeyKing.Message.weChat(.timeline(info: info))
MonkeyKing.deliver(message) { result in
print("result: (result)")
}
</pre>
回掉信息中包含了成功和失败的枚举信息,成功会返回一个字典
<pre>
public enum DeliverResult {
case success(ResponseJSON?)
case failure(Error)
}
public typealias ResponseJSON = [String: Any]
public typealias DeliverCompletionHandler = (_ result: DeliverResult) -> Void
</pre>

整个过程大概可以概括为:
1.注册帐号,必须在分享之前将第三方帐号注册到app中
2.构建分享信息,可以分享纯文本和纯图片,注意Info的格式
3.构建不同分享信息的Message
4.分享并得到回调信息

相关文章

网友评论

      本文标题:使用MonkeyKing进行第三方分享一

      本文链接:https://www.haomeiwen.com/subject/sailxxtx.html