好久不跟新博客。虽然也没有几个人看。
话不多说,直插主题。现在每个应用里头都会有第三方分享,有的项目中用ShareSDK 或者 友盟社会化组件。但本宝宝做分享的时候,组长看见我用友盟分享,很不开心,说怕奔溃。宝宝想了想。分享这块如果用第三方,可能会多一次请求第三方服务器的过程。有点多余。所以宝宝决定自己搞一搞。
首先是在AppDelegate中注册 微博和微信这两个应用,QQ 的注册 是在TencentOAuth 在初始化的时候注册的
[[TencentOAuth alloc] initWithAppId:kQqAppId andDelegate:(id<TencentSessionDelegate>)self];
//微信SDK [WXApi registerApp:kWeichatAppId withDescription:@"嗨播"]; //微博SDK [WeiboSDK enableDebugMode:YES]; [WeiboSDK registerApp:kSINA_KEY];
-(BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url{ return [WXApi handleOpenURL:url delegate:self] || [TencentOAuth HandleOpenURL:url] || [WeiboSDK handleOpenURL:url delegate:self]; } -(BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation{ return [WXApi handleOpenURL:url delegate:self] || [TencentOAuth HandleOpenURL:url] || [WeiboSDK handleOpenURL:url delegate:self]; }
之后直接看博主分装的分享吧
share manager.h
// // HLShareManager.h // // // Created by 王飞 on 16/5/4. // Copyright © 2016年 . All rights reserved. // #import <Foundation/Foundation.h> #import <UIKit/UIKit.h> typedef enum { HLShareTypeWechatSession = 1, //发送给朋友 HLShareTypeWechatTimeline = 2, //发送到朋友圈 HLShareTypeMobileQQ = 3, HLShareTypeQzone = 4, HLShareTypeTypeSina = 5, } HLShareType; @interface HLShareManager : NSObject /** * 通过第三方分享内容 * * @param url 分享的链接 * @param text 分享的标题 * @param description 分享的详情(微信中没有这个,可以为空) * @param imageUrl 分享中图片的url (微博分享中,这个url的图片大小不能超过32k) * @param type 分享的途径或方式 */ + (void)shareUrl:(NSString *)url text:(NSString *)text description:(NSString *)description image:(NSString *)imageUrl shareTyep:(HLShareType)type; @end
sharemanager.m
// // HLShareManager.m // // Created by 王飞 on 16/5/4. // Copyright © 2016年 . All rights reserved. // #import "HLShareManager.h" #import "WechatAuthSDK.h" #import "WXApi.h" #import <TencentOpenAPI/TencentApiInterface.h> #import <TencentOpenAPI/TencentMessageObject.h> #import <TencentOpenAPI/TencentOAuth.h> #import <TencentOpenAPI/QQApiInterface.h> #import "WeiboSDK.h" @interface HLShareManager () @property (nonatomic, strong) UIView *blackView; @end @implementation HLShareManager + (void)shareUrl:(NSString *)url text:(NSString *)text description:(NSString *)description image:(NSString *)imageUrl shareTyep:(HLShareType)type { switch (type) { case 1: [[[self alloc] init] shareToWeiChat:url text:text image:imageUrl shareTyep:WXSceneSession]; break; case 2: [[[self alloc] init] shareToWeiChat:url text:text image:imageUrl shareTyep:WXSceneTimeline]; break; case 3: [[[self alloc] init] shareToQzone:url text:text description:description image:imageUrl]; break; case 4: [[[self alloc] init] shareToQQ:url text:text description:description image:imageUrl]; break; case 5: [[[self alloc] init] shareToSine:url text:text description:description image:imageUrl]; break; default: break; } } - (void)shareToWeiChat:(NSString *)url text:(NSString *)text image:(NSString *)imageUrl shareTyep:(int)type { if ([WXApi isWXAppInstalled] && [WXApi isWXAppSupportApi]) { WXMediaMessage *message = [WXMediaMessage message]; message.title = text; message.description = text; [message setThumbImage:[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:imageUrl]]]]; WXWebpageObject *webpage = [WXWebpageObject object]; webpage.webpageUrl = url; message.mediaObject = webpage; SendMessageToWXReq* req = [[SendMessageToWXReq alloc] init]; req.bText = NO; req.message = message; req.scene = WXSceneTimeline; [WXApi sendReq:req]; } else { [Tools showMessageCenter:@"未安装微信,请安装后重试"]; } } - (void)shareToQQ:(NSString *)url text:(NSString *)text description:(NSString *)description image:(NSString *)imageUrl { [[TencentOAuth alloc] initWithAppId:kQqAppId andDelegate:(id<TencentSessionDelegate>)self]; QQApiNewsObject *newsObj = [QQApiNewsObject objectWithURL :[NSURL URLWithString:url] title: text description :description previewImageURL:[NSURL URLWithString:imageUrl]]; SendMessageToQQReq *req = [SendMessageToQQReq reqWithContent:newsObj]; //将内容分享到qq //QQApiSendResultCode sent = [QQApiInterface sendReq:req]; //将内容分享到qzone QQApiSendResultCode sent = [QQApiInterface SendReqToQZone:req]; [self handleSendResult:sent]; } - (void)shareToQzone:(NSString *)url text:(NSString *)text description:(NSString *)description image:(NSString *)imageUrl { [[TencentOAuth alloc] initWithAppId:kQqAppId andDelegate:(id<TencentSessionDelegate>)self]; QQApiNewsObject *newsObj = [QQApiNewsObject objectWithURL :[NSURL URLWithString:url] title: text description :description previewImageData:[NSData dataWithContentsOfURL:[NSURL URLWithString:imageUrl]]]; SendMessageToQQReq *req = [SendMessageToQQReq reqWithContent:newsObj]; //将内容分享到qq QQApiSendResultCode sent = [QQApiInterface sendReq:req]; //将内容分享到qzone // QQApiSendResultCode sent = [QQApiInterface SendReqToQZone:req]; [self handleSendResult:sent]; } - (void)handleSendResult:(QQApiSendResultCode)sendResult { switch (sendResult) { case EQQAPIAPPNOTREGISTED: { UIAlertView *msgbox = [[UIAlertView alloc] initWithTitle:@"Error" message:@"App未注册" delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:nil]; [msgbox show]; // [msgbox release]; break; } case EQQAPIMESSAGECONTENTINVALID: case EQQAPIMESSAGECONTENTNULL: case EQQAPIMESSAGETYPEINVALID: { UIAlertView *msgbox = [[UIAlertView alloc] initWithTitle:@"Error" message:@"发送参数错误" delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:nil]; [msgbox show]; // [msgbox release]; break; } case EQQAPIQQNOTINSTALLED: { UIAlertView *msgbox = [[UIAlertView alloc] initWithTitle:@"Error" message:@"未安装手Q" delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:nil]; [msgbox show]; // [msgbox release]; break; } case EQQAPIQQNOTSUPPORTAPI: { UIAlertView *msgbox = [[UIAlertView alloc] initWithTitle:@"Error" message:@"API接口不支持" delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:nil]; [msgbox show]; // [msgbox release]; break; } case EQQAPISENDFAILD: { UIAlertView *msgbox = [[UIAlertView alloc] initWithTitle:@"Error" message:@"发送失败" delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:nil]; [msgbox show]; // [msgbox release]; break; } default: { break; } } } - (void)shareToSine:(NSString *)url text:(NSString *)text description:(NSString *)description image:(NSString *)imageUrl { [WeiboSDK enableDebugMode:YES]; [WeiboSDK registerApp:kSINA_KEY]; WBAuthorizeRequest *request = [WBAuthorizeRequest request]; request.redirectURI = kRedirectURI; request.scope = @"all"; WBMessageObject *message = [WBMessageObject message]; WBWebpageObject *webpage = [WBWebpageObject object]; webpage.objectID = @"identifier1"; webpage.title = NSLocalizedString(text, nil); webpage.description = description; webpage.thumbnailData = [NSData dataWithContentsOfURL:[NSURL URLWithString:imageUrl]]; webpage.webpageUrl = url; message.mediaObject = webpage; WBSendMessageToWeiboRequest *sendMessagerequest = [WBSendMessageToWeiboRequest requestWithMessage:message authInfo:request access_token:nil]; sendMessagerequest.userInfo = @{@"ShareMessageFrom": @"SendMessageToWeiboViewController", @"Other_Info_1": [NSNumber numberWithInt:123], @"Other_Info_2": @[@"obj1", @"obj2"], @"Other_Info_3": @{@"key1": @"obj1", @"key2": @"obj2"}}; // request.shouldOpenWeiboAppInstallPageIfNotInstalled = NO; [WeiboSDK sendRequest:sendMessagerequest]; } @end