寫在前面
關于微信分享這個功能的實現(xiàn)是有很多辦法的,比如大家都知道的友盟奇钞、shareSDK以及MonkeyKing等,MonkeyKing是用Swift寫的,有興趣的可以去github上面下載來看看诺祸,我這里只說調(diào)用微信SDK來實現(xiàn)分享功能,我會把微信分享旁振、QQ分享以及新浪微博分享分開寫,方便我也方便大家看雳灾,不墨跡 直接干正事漠酿。
一.去微信開放平臺注冊一個應用(鏈接:https://open.weixin.qq.com/)
在開放平臺注冊應用并通過審核后,會得到該應用的必要信息:
1.png
二.添加URL types
2.png
填寫相應的identifier和URL Schemes,URL Schemes就是App ID
三.向微信注冊
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// WXAPPID是在平臺注冊應用時的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 微信分享的相關回調(diào)
//onReq是微信終端向第三方程序發(fā)起請求谎亩,要求第三方程序響應炒嘲。第三方程序響應完后必須調(diào)用sendRsp返回。在調(diào)用sendRsp返回時匈庭,會切回到微信終端程序界面夫凸。
- (void)onReq:(BaseReq *)req
{
}
/** 如果第三方程序向微信發(fā)送了sendReq的請求,那么onResp會被回調(diào)阱持。sendReq請求調(diào)用后夭拌,會切到微信終端程序界面。*/
/**
enum WXErrCode {
WXSuccess = 0, /**< 成功 */
WXErrCodeCommon = -1, /**< 普通錯誤類型 */
WXErrCodeUserCancel = -2, /**< 用戶點擊取消并返回 */
WXErrCodeSentFail = -3, /**< 發(fā)送失敗 */
WXErrCodeAuthDeny = -4, /**< 授權失敗 */
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依賴的庫
在進行到第四步的時候紊选,可以跑跑程序啼止,你會發(fā)現(xiàn)程序編譯不通過,原因缺少了一些庫兵罢,根據(jù)錯誤提示添加就行了献烦,如下:
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];
//分享標題
message.title = @"寶寶也是醉了";
// 描述
message.description = @"微信微信微信微信微信微信微信微信微信微信測試";
//分享圖片,使用SDK的setThumbImage方法可壓縮圖片大小
[message setThumbImage:[UIImage imageNamed:@"1"]];
//創(chuàng)建多媒體對象
WXWebpageObject *webObj = [WXWebpageObject object];
// 點擊后的跳轉鏈接
webObj.webpageUrl = @"www.baidu.com";
message.mediaObject = webObj;
sendReq.message = message;
[WXApi sendReq:sendReq];
}
}
七.最后一步:如果在ios9上的話,你可能還不能正常分享卖词,會提示如下錯誤信息:
-canOpenURL: failed for URL: "weixin://app/wx3de242dd39206961/" - error: "This app is not allowed to query for scheme weixin"
需要在“Info.plist”中將要使用的URL Schemes列為白名單巩那,才可正常檢查其他應用是否安裝。
需要添加哪些就根據(jù)錯誤提示一個一個添加就ok了此蜈。
在info.plist里面添加如下信息:
4.png
PS:添加到?jīng)]有如上的錯誤提示即可即横。
八.測試
純文本:
5.png
圖文鏈接通通都有:
6.png
總結
其實還是看微信的SDK比較全面,哈哈哈哈