diff --git a/README.md b/README.md index 01e8c17..483b9b6 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,8 @@ 该模块会继续完善优化,争取为大家提供一个更快速、更简单、更规范、扩展性更好的集成模块。 =========== -![图片](http://i1.tietuku.com/d62804985316df60.gif) + +![图片](https://github.com/xumeng/XMShareModule/blob/master/demo.gif) # 特点: * 快速 @@ -18,7 +19,14 @@ * 扩展性好 * 可定制 -=== +=========== + +# 更新 Log + * 2015-09-29 修复网页微博分享时内容没有传入的 Bug + * 2015-10-29 适配 iOS 9 下分享 + * 2015-11-04 新增三大平台分享后的回调 + * 2016-01-11 新增适配 iOS 9 SSO 的 `LSApplicationQueriesSchemes`配置说明 + # 集成步骤 ### 1. 下载微信、QQ、微博的社交sdk并导入至项目中 @@ -38,7 +46,7 @@ ### 4. 设置URL Schema -程序 —— Targets —— Info —— URL Types +1. 程序 —— Targets —— Info —— URL Types 分别添加微信,QQ,微博 @@ -53,11 +61,32 @@ * *以上均为测试app key,具体可以去对应的开放平台注册* * *mqqapi 是 tencent 的 app key 转十六进制,不足八位在前面补 0 的结果* +
+2. 在 Info.plist 中添加 `LSApplicationQueriesSchemes` 项, +分别添加社交平台的几个白名单: +mqq + +mqqopensdkapiV2 + +mqqapi + +weibosdk2.5 + +weibosdk + +weibo + +weixin + +wechat + +具体列表请至获取 [iOS 9 应用跳转适配](http://dev.umeng.com/social/ios/ios9) + ### 5. 导入XMShareView至项目中 -#####XMShareView结构介绍: +##### XMShareView结构介绍: 名称 | 解释 --- | --- CommonMarco.h | 通用宏文件,包含APP Key等宏 @@ -130,10 +159,113 @@ XMShareView.h | 分享显示视图 ``` +### 8. 处理 URL 跳转 +#### 在 `AppDelegate` 类中实现两个处理 URL 跳转的方法。 +``` +- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation +{ + if ([[url absoluteString] hasPrefix:@"tencent"]) { + +// return [TencentOAuth HandleOpenURL:url]; + return [QQApiInterface handleOpenURL:url delegate:self]; + + }else if([[url absoluteString] hasPrefix:@"wb"]) { + + return [WeiboSDK handleOpenURL:url delegate:self]; + + }else if([[url absoluteString] hasPrefix:@"wx"]) { + + // 处理微信回调需要在具体的 ViewController 中处理。 + ViewController *vc = (ViewController *)self.window.rootViewController; + return [WXApi handleOpenURL:url delegate:vc]; + + } + + return NO; +} + +- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url +{ + if ([[url absoluteString] hasPrefix:@"tencent"]) { + + return [TencentOAuth HandleOpenURL:url]; + + }else if([[url absoluteString] hasPrefix:@"wb"]) { + + return [WeiboSDK handleOpenURL:url delegate:self]; + + }else{ + + ViewController *vc = (ViewController *)self.window.rootViewController; + return [WXApi handleOpenURL:url delegate:vc]; + + } +} + +``` + +### 9. 处理分享后的回调 +***特别提醒:(下方方法体内是示例代码,具体业务逻辑请具体实现)*** +* 微信分享:在具体的调用 ViewController 类里面实现微信 `WXApiDelegate` 协议,然后实现以下方法: + +``` +- (void)onResp:(BaseResp *)resp +{ + NSString *message; + if(resp.errCode == 0) { + message = @"分享成功"; + }else{ + message = @"分享失败"; + } + showAlert(message); +} +``` + +* QQ 分享:在 `AppDelegate` 类中实现 `QQApiInterfaceDelegate` 协议,然后实现以下方法: + +``` +- (void)onResp:(QQBaseResp *)resp +{ + NSString *message; + if([resp.result integerValue] == 0) { + message = @"分享成功"; + }else{ + message = @"分享失败"; + } + showAlert(message); +} +``` + +* 微博分享:在 `AppDelegate` 类中实现 `WeiboSDKDelegate` 协议,然后实现以下方法: + +``` +- (void)didReceiveWeiboResponse:(WBBaseResponse *)response +{ + NSString *message; + switch (response.statusCode) { + case WeiboSDKResponseStatusCodeSuccess: + message = @"分享成功"; + break; + case WeiboSDKResponseStatusCodeUserCancel: + message = @"取消分享"; + break; + case WeiboSDKResponseStatusCodeSentFail: + message = @"分享失败"; + break; + default: + message = @"分享失败"; + break; + } + showAlert(message); +} +``` + # 参考资料 [QQ iOS SDK 环境搭建](http://wiki.connect.qq.com/ios_sdk%E7%8E%AF%E5%A2%83%E6%90%AD%E5%BB%BA) [新浪微博 iOS SDK 环境搭建](https://github.com/sinaweibosdk/weibo_ios_sdk/blob/master/%E5%BE%AE%E5%8D%9AiOS%E5%B9%B3%E5%8F%B0SDK%E6%96%87%E6%A1%A3V3.1.1.pdf) -[微信 iOS SDK 环境搭建](https://open.weixin.qq.com/cgi-bin/showdocument?action=dir_list&t=resource/res_list&verify=1&id=1417694084&token=&lang=zh_CN) \ No newline at end of file +[微信 iOS SDK 环境搭建](https://open.weixin.qq.com/cgi-bin/showdocument?action=dir_list&t=resource/res_list&verify=1&id=1417694084&token=&lang=zh_CN) + +[iOS 9 适配 SSO](http://dev.umeng.com/social/ios/ios9) \ No newline at end of file diff --git a/XMShare/XMShare.xcodeproj/project.pbxproj b/XMShare/XMShare.xcodeproj/project.pbxproj index 9656ba9..d41fe03 100644 --- a/XMShare/XMShare.xcodeproj/project.pbxproj +++ b/XMShare/XMShare.xcodeproj/project.pbxproj @@ -388,6 +388,7 @@ TargetAttributes = { 3F7A7C181B731B91005D9564 = { CreatedOnToolsVersion = 6.4; + DevelopmentTeam = VTBQB83AWS; }; 3F7A7C311B731B91005D9564 = { CreatedOnToolsVersion = 6.4; @@ -581,6 +582,8 @@ isa = XCBuildConfiguration; buildSettings = { ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; + CODE_SIGN_IDENTITY = "iPhone Developer"; + ENABLE_BITCODE = NO; FRAMEWORK_SEARCH_PATHS = ( "$(inherited)", "$(PROJECT_DIR)/XMShare/Vendor/Tencent", @@ -601,6 +604,8 @@ isa = XCBuildConfiguration; buildSettings = { ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; + CODE_SIGN_IDENTITY = "iPhone Developer"; + ENABLE_BITCODE = NO; FRAMEWORK_SEARCH_PATHS = ( "$(inherited)", "$(PROJECT_DIR)/XMShare/Vendor/Tencent", diff --git a/XMShare/XMShare/AppDelegate.m b/XMShare/XMShare/AppDelegate.m index e4d82c6..22ab2e3 100644 --- a/XMShare/XMShare/AppDelegate.m +++ b/XMShare/XMShare/AppDelegate.m @@ -8,13 +8,15 @@ #import "AppDelegate.h" #import "CommonMarco.h" +#import "ViewController.h" #import "WXApi.h" #import +#import #import "WeiboSDK.h" -@interface AppDelegate () +@interface AppDelegate () @end @@ -34,12 +36,18 @@ - (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceAppl { if ([[url absoluteString] hasPrefix:@"tencent"]) { - return [TencentOAuth HandleOpenURL:url]; +// return [TencentOAuth HandleOpenURL:url]; + return [QQApiInterface handleOpenURL:url delegate:self]; - }else if([[url absoluteString] hasPrefix:@"wb"]){ + }else if([[url absoluteString] hasPrefix:@"wb"]) { return [WeiboSDK handleOpenURL:url delegate:self]; + }else if([[url absoluteString] hasPrefix:@"wx"]) { + + ViewController *vc = (ViewController *)self.window.rootViewController; + return [WXApi handleOpenURL:url delegate:vc]; + } return NO; @@ -47,7 +55,20 @@ - (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceAppl - (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url { - return [TencentOAuth HandleOpenURL:url]; + if ([[url absoluteString] hasPrefix:@"tencent"]) { + + return [TencentOAuth HandleOpenURL:url]; + + }else if([[url absoluteString] hasPrefix:@"wb"]) { + + return [WeiboSDK handleOpenURL:url delegate:self]; + + }else{ + + ViewController *vc = (ViewController *)self.window.rootViewController; + return [WXApi handleOpenURL:url delegate:vc]; + + } } @@ -62,10 +83,59 @@ - (void)init3rdParty [WeiboSDK registerApp:APP_KEY_WEIBO]; } + +#pragma mark - 实现代理回调 +/** + * 微博 + * + * @param response 响应体。根据 WeiboSDKResponseStatusCode 作对应的处理. + * 具体参见 `WeiboSDKResponseStatusCode` 枚举. + */ - (void)didReceiveWeiboResponse:(WBBaseResponse *)response +{ + NSString *message; + switch (response.statusCode) { + case WeiboSDKResponseStatusCodeSuccess: + message = @"分享成功"; + break; + case WeiboSDKResponseStatusCodeUserCancel: + message = @"取消分享"; + break; + case WeiboSDKResponseStatusCodeSentFail: + message = @"分享失败"; + break; + default: + message = @"分享失败"; + break; + } + showAlert(message); +} + +/** + * 处理来至QQ的请求 + * + * @param req QQApi请求消息基类 + */ +- (void)onReq:(QQBaseReq *)req { } +/** + * 处理来至QQ的响应 + * + * @param resp 响应体,根据响应结果作对应处理 + */ +- (void)onResp:(QQBaseResp *)resp +{ + NSString *message; + if([resp.result integerValue] == 0) { + message = @"分享成功"; + }else{ + message = @"分享失败"; + } + showAlert(message); +} + @end diff --git a/XMShare/XMShare/Base.lproj/Main.storyboard b/XMShare/XMShare/Base.lproj/Main.storyboard index fb8aa31..336182a 100644 --- a/XMShare/XMShare/Base.lproj/Main.storyboard +++ b/XMShare/XMShare/Base.lproj/Main.storyboard @@ -1,7 +1,12 @@ - - + + + + + - + + + @@ -13,21 +18,28 @@ - + - - + + + + + @@ -38,6 +50,6 @@ - + diff --git a/XMShare/XMShare/Images.xcassets/AppIcon.appiconset/Contents.json b/XMShare/XMShare/Images.xcassets/AppIcon.appiconset/Contents.json index 118c98f..3a0d043 100644 --- a/XMShare/XMShare/Images.xcassets/AppIcon.appiconset/Contents.json +++ b/XMShare/XMShare/Images.xcassets/AppIcon.appiconset/Contents.json @@ -16,19 +16,118 @@ "scale" : "2x" }, { - "idiom" : "iphone", "size" : "40x40", + "idiom" : "iphone", + "filename" : "Icon-40@3x.png", "scale" : "3x" }, { - "idiom" : "iphone", "size" : "60x60", + "idiom" : "iphone", + "filename" : "Icon-60@2x.png", "scale" : "2x" }, { - "idiom" : "iphone", "size" : "60x60", + "idiom" : "iphone", + "filename" : "Icon-60@3x.png", "scale" : "3x" + }, + { + "size" : "29x29", + "idiom" : "ipad", + "filename" : "Icon-Small.png", + "scale" : "1x" + }, + { + "idiom" : "ipad", + "size" : "29x29", + "scale" : "2x" + }, + { + "size" : "40x40", + "idiom" : "ipad", + "filename" : "Icon-40.png", + "scale" : "1x" + }, + { + "size" : "40x40", + "idiom" : "ipad", + "filename" : "Icon-40@2x.png", + "scale" : "2x" + }, + { + "size" : "76x76", + "idiom" : "ipad", + "filename" : "Icon-76.png", + "scale" : "1x" + }, + { + "size" : "76x76", + "idiom" : "ipad", + "filename" : "Icon-76@2x.png", + "scale" : "2x" + }, + { + "idiom" : "ipad", + "size" : "83.5x83.5", + "scale" : "2x" + }, + { + "size" : "24x24", + "idiom" : "watch", + "scale" : "2x", + "role" : "notificationCenter", + "subtype" : "38mm" + }, + { + "size" : "27.5x27.5", + "idiom" : "watch", + "scale" : "2x", + "role" : "notificationCenter", + "subtype" : "42mm" + }, + { + "size" : "29x29", + "idiom" : "watch", + "filename" : "Icon-Small@2x.png", + "role" : "companionSettings", + "scale" : "2x" + }, + { + "size" : "29x29", + "idiom" : "watch", + "filename" : "Icon-Small@3x.png", + "role" : "companionSettings", + "scale" : "3x" + }, + { + "size" : "40x40", + "idiom" : "watch", + "scale" : "2x", + "role" : "appLauncher", + "subtype" : "38mm" + }, + { + "size" : "44x44", + "idiom" : "watch", + "scale" : "2x", + "role" : "longLook", + "subtype" : "42mm" + }, + { + "size" : "86x86", + "idiom" : "watch", + "scale" : "2x", + "role" : "quickLook", + "subtype" : "38mm" + }, + { + "size" : "98x98", + "idiom" : "watch", + "scale" : "2x", + "role" : "quickLook", + "subtype" : "42mm" } ], "info" : { diff --git a/XMShare/XMShare/Images.xcassets/AppIcon.appiconset/Icon-40.png b/XMShare/XMShare/Images.xcassets/AppIcon.appiconset/Icon-40.png new file mode 100644 index 0000000..3c1c2a9 Binary files /dev/null and b/XMShare/XMShare/Images.xcassets/AppIcon.appiconset/Icon-40.png differ diff --git a/XMShare/XMShare/Images.xcassets/AppIcon.appiconset/Icon-40@2x.png b/XMShare/XMShare/Images.xcassets/AppIcon.appiconset/Icon-40@2x.png new file mode 100644 index 0000000..643272d Binary files /dev/null and b/XMShare/XMShare/Images.xcassets/AppIcon.appiconset/Icon-40@2x.png differ diff --git a/XMShare/XMShare/Images.xcassets/AppIcon.appiconset/Icon-40@3x.png b/XMShare/XMShare/Images.xcassets/AppIcon.appiconset/Icon-40@3x.png new file mode 100644 index 0000000..b818b5e Binary files /dev/null and b/XMShare/XMShare/Images.xcassets/AppIcon.appiconset/Icon-40@3x.png differ diff --git a/XMShare/XMShare/Images.xcassets/AppIcon.appiconset/Icon-60@2x.png b/XMShare/XMShare/Images.xcassets/AppIcon.appiconset/Icon-60@2x.png new file mode 100644 index 0000000..b818b5e Binary files /dev/null and b/XMShare/XMShare/Images.xcassets/AppIcon.appiconset/Icon-60@2x.png differ diff --git a/XMShare/XMShare/Images.xcassets/AppIcon.appiconset/Icon-60@3x.png b/XMShare/XMShare/Images.xcassets/AppIcon.appiconset/Icon-60@3x.png new file mode 100644 index 0000000..803f6ec Binary files /dev/null and b/XMShare/XMShare/Images.xcassets/AppIcon.appiconset/Icon-60@3x.png differ diff --git a/XMShare/XMShare/Images.xcassets/AppIcon.appiconset/Icon-76.png b/XMShare/XMShare/Images.xcassets/AppIcon.appiconset/Icon-76.png new file mode 100644 index 0000000..cf7a37e Binary files /dev/null and b/XMShare/XMShare/Images.xcassets/AppIcon.appiconset/Icon-76.png differ diff --git a/XMShare/XMShare/Images.xcassets/AppIcon.appiconset/Icon-76@2x.png b/XMShare/XMShare/Images.xcassets/AppIcon.appiconset/Icon-76@2x.png new file mode 100644 index 0000000..e57bbaa Binary files /dev/null and b/XMShare/XMShare/Images.xcassets/AppIcon.appiconset/Icon-76@2x.png differ diff --git a/XMShare/XMShare/Images.xcassets/AppIcon.appiconset/Icon-Small.png b/XMShare/XMShare/Images.xcassets/AppIcon.appiconset/Icon-Small.png new file mode 100644 index 0000000..8fcd70b Binary files /dev/null and b/XMShare/XMShare/Images.xcassets/AppIcon.appiconset/Icon-Small.png differ diff --git a/XMShare/XMShare/Images.xcassets/AppIcon.appiconset/Icon-Small@2x.png b/XMShare/XMShare/Images.xcassets/AppIcon.appiconset/Icon-Small@2x.png new file mode 100644 index 0000000..fb2f009 Binary files /dev/null and b/XMShare/XMShare/Images.xcassets/AppIcon.appiconset/Icon-Small@2x.png differ diff --git a/XMShare/XMShare/Images.xcassets/AppIcon.appiconset/Icon-Small@3x.png b/XMShare/XMShare/Images.xcassets/AppIcon.appiconset/Icon-Small@3x.png new file mode 100644 index 0000000..02c9632 Binary files /dev/null and b/XMShare/XMShare/Images.xcassets/AppIcon.appiconset/Icon-Small@3x.png differ diff --git a/XMShare/XMShare/Info.plist b/XMShare/XMShare/Info.plist index 6ede640..b64d5c0 100644 --- a/XMShare/XMShare/Info.plist +++ b/XMShare/XMShare/Info.plist @@ -2,6 +2,21 @@ + NSAppTransportSecurity + + NSAllowsArbitraryLoads + + + LSApplicationQueriesSchemes + + mqq + mqqopensdkapiV2 + weibosdk2.5 + weibosdk + weibo + mqqapi + weixin + CFBundleDevelopmentRegion en CFBundleExecutable diff --git a/XMShare/XMShare/ViewController.h b/XMShare/XMShare/ViewController.h index 83e4a9e..fa5b4cc 100644 --- a/XMShare/XMShare/ViewController.h +++ b/XMShare/XMShare/ViewController.h @@ -7,8 +7,9 @@ // #import +#import "WXApi.h" -@interface ViewController : UIViewController +@interface ViewController : UIViewController @end diff --git a/XMShare/XMShare/ViewController.m b/XMShare/XMShare/ViewController.m index 9e12076..98c1f6a 100644 --- a/XMShare/XMShare/ViewController.m +++ b/XMShare/XMShare/ViewController.m @@ -8,6 +8,7 @@ #import "ViewController.h" #import "XMShareView.h" +#import "CommonMarco.h" @interface ViewController () @@ -38,7 +39,7 @@ - (IBAction)clickShare:(id)sender { self.shareView.shareText = NSLocalizedString(@"分享内容", nil); - self.shareView.shareUrl = @"http://xumeng.github.com"; + self.shareView.shareUrl = @"http://amonxu.com"; [self.view addSubview:self.shareView]; @@ -55,4 +56,21 @@ - (IBAction)clickShare:(id)sender { } } +#pragma mark - 代理回调 +/** + * 处理来自微信的请求 + * + * @param resp 响应体。根据 errCode 作出对应处理。 + */ +- (void)onResp:(BaseResp *)resp +{ + NSString *message; + if(resp.errCode == 0) { + message = @"分享成功"; + }else{ + message = @"分享失败"; + } + showAlert(message); +} + @end diff --git a/XMShare/XMShare/XMShareView/CommonMarco.h b/XMShare/XMShare/XMShareView/CommonMarco.h index 90fbaf6..2040c95 100644 --- a/XMShare/XMShare/XMShareView/CommonMarco.h +++ b/XMShare/XMShare/XMShareView/CommonMarco.h @@ -39,5 +39,6 @@ View.layer.borderColor = BorderColor.CGColor;\ View.layer.borderWidth = BorderWidth; +#define showAlert(_msg){UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:_msg delegate:nil cancelButtonTitle:nil otherButtonTitles:@"确定",nil];[alert show];} #endif diff --git a/XMShare/XMShare/XMShareView/ShareUtil/XMShareQQUtil.h b/XMShare/XMShare/XMShareView/ShareUtil/XMShareQQUtil.h index 7f73a1f..e057235 100644 --- a/XMShare/XMShare/XMShareView/ShareUtil/XMShareQQUtil.h +++ b/XMShare/XMShare/XMShareView/ShareUtil/XMShareQQUtil.h @@ -23,7 +23,7 @@ typedef NS_ENUM(NSInteger, SHARE_QQ_TYPE){ }; -@interface XMShareQQUtil : XMShareUtil +@interface XMShareQQUtil : XMShareUtil /** * 分享到QQ会话 diff --git a/XMShare/XMShare/XMShareView/ShareUtil/XMShareWeiboUtil.m b/XMShare/XMShare/XMShareView/ShareUtil/XMShareWeiboUtil.m index 8948cce..b203a27 100644 --- a/XMShare/XMShare/XMShareView/ShareUtil/XMShareWeiboUtil.m +++ b/XMShare/XMShare/XMShareView/ShareUtil/XMShareWeiboUtil.m @@ -32,23 +32,29 @@ - (void)shareToWeiboBase @"Other_Info_3": @{@"key1": @"obj1", @"key2": @"obj2"}}; [WeiboSDK sendRequest:request]; - } - (WBMessageObject *)messageToShare { WBMessageObject *message = [WBMessageObject message]; + BOOL hadInstalledWeibo = [[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"weibo://"]]; + + if(hadInstalledWeibo){ + WBWebpageObject *webpage = [WBWebpageObject object]; + webpage.objectID = @"identifier1"; + webpage.title = self.shareTitle; + webpage.description = self.shareText; + // 可改为自定义图片 + webpage.thumbnailData = UIImageJPEGRepresentation(SHARE_IMG, SHARE_IMG_COMPRESSION_QUALITY); + webpage.webpageUrl = self.shareUrl; + + message.mediaObject = webpage; + } + + message.text = [NSString stringWithFormat:@"%@ - %@ %@", self.shareTitle, self.shareText, self.shareUrl]; - WBWebpageObject *webpage = [WBWebpageObject object]; - webpage.objectID = @"identifier1"; - webpage.title = self.shareTitle; - webpage.description = self.shareText; - // 可改为自定义图片 - webpage.thumbnailData = UIImageJPEGRepresentation(SHARE_IMG, SHARE_IMG_COMPRESSION_QUALITY); - webpage.webpageUrl = self.shareUrl; - message.mediaObject = webpage; return message; } diff --git a/demo.gif b/demo.gif new file mode 100644 index 0000000..506e64e Binary files /dev/null and b/demo.gif differ