寫在前面
關(guān)于微信分享這個(gè)功能的實(shí)現(xiàn)是有很多辦法的,比如大家都知道的友盟、shareSDK以及MonkeyKing等,MonkeyKing是用Swift寫的,有興趣的可以去github上面下載來看看,我這里只說調(diào)用微信SDK來實(shí)現(xiàn)分享功能,我會(huì)把微信分享、QQ分享以及新浪微博分享分開寫,方便我也方便大家看,不墨跡 直接干正事。
一.去微信開放平臺(tái)注冊一個(gè)應(yīng)用(鏈接:https://open.weixin.qq.com/)
在開放平臺(tái)注冊應(yīng)用并通過審核后,會(huì)得到該應(yīng)用的必要信息:

1.png
二.添加URL types

2.png
填寫相應(yīng)的identifier和URL Schemes,URL Schemes就是App ID
三.向微信注冊
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// WXAPPID是在平臺(tái)注冊應(yīng)用時(shí)的AppID
[WXApi registerApp:WXAPPID];
return YES;
}
四.處理open url
- (BOOL)application:(UIApplication *)application openURL:(nonnull NSURL *)url options:(nonnull NSDictionary<NSString *,id> *)options {
return [WXApi handleOpenURL:url delegate:self];
}
#pragma mark -
#pragma mark WXApiDelegate 微信分享的相關(guān)回調(diào)
//onReq是微信終端向第三方程序發(fā)起請求,要求第三方程序響應(yīng)。第三方程序響應(yīng)完后必須調(diào)用sendRsp返回。在調(diào)用sendRsp返回時(shí),會(huì)切回到微信終端程序界面。
- (void)onReq:(BaseReq *)req
{
}
/** 如果第三方程序向微信發(fā)送了sendReq的請求,那么onResp會(huì)被回調(diào)。sendReq請求調(diào)用后,會(huì)切到微信終端程序界面。*/
/**
enum WXErrCode {
WXSuccess = 0, /**< 成功 */
WXErrCodeCommon = -1, /**< 普通錯(cuò)誤類型 */
WXErrCodeUserCancel = -2, /**< 用戶點(diǎn)擊取消并返回 */
WXErrCodeSentFail = -3, /**< 發(fā)送失敗 */
WXErrCodeAuthDeny = -4, /**< 授權(quán)失敗 */
WXErrCodeUnsupport = -5, /**< 微信不支持 */
};*/
- (void)onResp:(BaseResp *)resp
{
if([resp isKindOfClass:[SendMessageToWXResp class]]) {
switch (resp.errCode) {
case WXSuccess:
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"成功" message:@"微信分享成功" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
[alert show];
}
break;
case WXErrCodeUserCancel:
break;
default:
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"失敗" message:@"微信分享失敗" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
[alert show];
}
break;
}
}
}
五.添加微信SDK依賴的庫
在進(jìn)行到第四步的時(shí)候,可以跑跑程序,你會(huì)發(fā)現(xiàn)程序編譯不通過,原因缺少了一些庫,根據(jù)錯(cuò)誤提示添加就行了,如下:

3.png
六.開始使用
#pragma mark -- life circle
- (void)viewDidLoad {
[super viewDidLoad];
self.inviteButton = [[UIButton alloc] initWithFrame:CGRectMake(150, 200, 80, 30)];
self.inviteButton.backgroundColor = [UIColor greenColor];
[self.inviteButton setTitle:@"微信分享" forState:UIControlStateNormal];
self.inviteButton.titleLabel.font = [UIFont systemFontOfSize:16.0];
self.inviteButton.layer.cornerRadius = 5;
self.inviteButton.layer.borderWidth = 1;
self.inviteButton.layer.borderColor = [UIColor grayColor].CGColor;
[self.inviteButton addTarget:self action:@selector(weixinInviteButtonClick) forControlEvents:UIControlEventTouchUpInside];
self.inviteButton.clipsToBounds = YES;
[self.view addSubview:self.inviteButton];
}
- (void)weixinInviteButtonClick {
[self SendTextImageLink];
}
/** 發(fā)送純文本*/
- (void)sendText {
if (![WXApi isWXAppInstalled]) {
NSLog(@"?請移步App Store去下載微信客戶端");
}else {
SendMessageToWXReq *sendReq = [[SendMessageToWXReq alloc] init];
sendReq.bText = YES;//YES表示使用文本信息 NO表示不使用文本信息
sendReq.text = @" 這是測試微信分享";
// 0:分享到好友列表 1:分享到朋友圈 2:收藏
sendReq.scene = 0;
//發(fā)送分享信息
[WXApi sendReq:sendReq];
// 返回分享成功還是失敗
NSLog(@" 成功和失敗 - %d",[WXApi sendReq:sendReq]);
}
}
/** 發(fā)送圖片文字鏈接*/
- (void)SendTextImageLink {
if (![WXApi isWXAppInstalled]) {
NSLog(@"請移步App Store去下載微信客戶端");
}else {
SendMessageToWXReq *sendReq = [[SendMessageToWXReq alloc] init];
sendReq.bText = NO;
sendReq.scene = 0;
// 2.創(chuàng)建分享內(nèi)容
WXMediaMessage *message = [WXMediaMessage message];
//分享標(biāo)題
message.title = @"寶寶也是醉了";
// 描述
message.description = @"微信微信微信微信微信微信微信微信微信微信測試";
//分享圖片,使用SDK的setThumbImage方法可壓縮圖片大小
[message setThumbImage:[UIImage imageNamed:@"1"]];
//創(chuàng)建多媒體對(duì)象
WXWebpageObject *webObj = [WXWebpageObject object];
// 點(diǎn)擊后的跳轉(zhuǎn)鏈接
webObj.webpageUrl = @"www.baidu.com";
message.mediaObject = webObj;
sendReq.message = message;
[WXApi sendReq:sendReq];
}
}
七.最后一步:如果在ios9上的話,你可能還不能正常分享,會(huì)提示如下錯(cuò)誤信息:
-canOpenURL: failed for URL: "weixin://app/wx3de242dd39206961/" - error: "This app is not allowed to query for scheme weixin"
需要在“Info.plist”中將要使用的URL Schemes列為白名單,才可正常檢查其他應(yīng)用是否安裝。
需要添加哪些就根據(jù)錯(cuò)誤提示一個(gè)一個(gè)添加就ok了。
在info.plist里面添加如下信息:

4.png
PS:添加到?jīng)]有如上的錯(cuò)誤提示即可。
八.測試
純文本:

5.png
圖文鏈接通通都有:

6.png
總結(jié)
其實(shí)還是看微信的SDK比較全面,哈哈哈哈