先總結一下遇到的坑:
1.分享面板無法彈出
由於 1. 創建Xcode項目會默認添加Main.storyboard
作為Main Interface
(General - Deployment Info),也就是項目的主Window。 2. 如果沒使用Main.storyboard而又另外在AppDelegate中創建了UIWindow對象,如
self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds]
如果項目中同時出現Main Interface
以及代碼創建UIWindow
會導致分享面板無法正常彈出,解決方法是移除其一即可。如果移除了Main.storyboard
,需要clean工程后再重新運行。
2.推薦cocoapods實現就不需要再導入依賴庫,會省很多事
下面實現自定義分享界面:
有時候我們做第三方分享時,不希望用系統的sheet或者alet樣式,該怎么辦呢,下面是本人實現的代碼;跳轉控制器中實現即可:
1.在控制器中要引用頭文件
#import <UMSocialCore/UMSocialCore.h>/** 友盟頭文件 */
#import <UShareUI/UShareUI.h>/** 友盟頭文件 */
#import "ActionButton.h"/** 自定義button:實現上圖片,下文字 */
#import "Masonry.h"/** 用到自動布局 */
2.由於用到了九宮格排布定義每行顯示多少個的宏
#define Count 4
3.具體實現代碼:
- (void)viewDidLoad {
[super viewDidLoad];
self.view.layer.contents = [UIImage imageNamed:@"dl_bjt"];
self.title = @"分享";
UIImageView *imgView = [[UIImageView alloc]initWithFrame:self.view.bounds];
imgView.userInteractionEnabled = YES;
imgView.image = [UIImage imageNamed:@"dl_bjt"];
[self.view addSubview:imgView];
[self setUI];/**自定義界面*/
}
-(void)setUI{
/**這里實現了新浪,微信朋友圈,微信聊天,QQ聊天四個*/
[UMSocialUIManager setPreDefinePlatforms:@[@(UMSocialPlatformType_Sina),@(UMSocialPlatformType_QQ),@( UMSocialPlatformType_WechatTimeLine ),@(UMSocialPlatformType_WechatSession)]];
NSArray *titleArr = @[@"新浪微博",@"QQ好友",@"微信朋友圈",@"微信好友"];
NSArray *imgArr = @[@"umsocial_sina",@"umsocial_qq",@"umsocial_wechat_timeline",@"umsocial_wechat"];
UIView *shareView = [[UIView alloc]init];/**分享按鈕界面,方便位置移動*/
[self.view addSubview:shareView];
[shareView mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerX.mas_equalTo(self.view.mas_centerX);
make.width.mas_equalTo(kScreenW);
make.height.mas_equalTo(100);
make.bottom.mas_equalTo(self.view).offset(-50);
}];
for (int i = 0; i<titleArr.count; i++)
{
ActionButton *button = [ActionButton buttonWithType:UIButtonTypeCustom];
int row = (int)i/Count;
int col = i%Count;
float btnW = 60;
float btnH = 80;
float magrin = (kScreenW-Count*btnW)/(Count+1);
button.frame = CGRectMake(magrin+col*(magrin+btnW), 10+row*(btnH+10),btnW, btnH);
[button setTitle:titleArr[i] forState:UIControlStateNormal];
[button setImage:[UIImage imageNamed:imgArr[i]] forState:UIControlStateNormal];
button.tag = i;
[button addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
[shareView addSubview:button];
}
}
-(void)click:(UIButton *)sender{
switch (sender.tag) {
case 0:
[self runShareWithType:UMSocialPlatformType_Sina];;
break;
case 1:
[self runShareWithType:UMSocialPlatformType_QQ];
break;
case 2:
[self runShareWithType:UMSocialPlatformType_WechatTimeLine];
break;
default:
[self runShareWithType:UMSocialPlatformType_WechatSession];
break;
}
}
-(void)systemUI{
/**友盟自帶界面*/
[UMSocialUIManager setPreDefinePlatforms:@[@(UMSocialPlatformType_Sina),@(UMSocialPlatformType_QQ),@( UMSocialPlatformType_WechatTimeLine ),@(UMSocialPlatformType_WechatSession)]];
// //設置用戶自定義的平台
//
// [UMSocialUIManager addCustomPlatformWithoutFilted:UMSocialPlatformType_UserDefine_Begin+2
//
// withPlatformIcon:[UIImage imageNamed:@"zhongjianggonggao_xyzj"]
//
// withPlatformName:@"復制鏈接"];
[UMSocialShareUIConfig shareInstance].sharePageGroupViewConfig.sharePageGroupViewPostionType = UMSocialSharePageGroupViewPositionType_Bottom;/**顯示位置底部*/
[UMSocialShareUIConfig shareInstance].sharePageScrollViewConfig.shareScrollViewPageItemStyleType = UMSocialPlatformItemViewBackgroudType_IconAndBGRadius;/**分享按鈕顯示樣式*/
[UMSocialUIManager showShareMenuViewInWindowWithPlatformSelectionBlock:^(UMSocialPlatformType platformType, NSDictionary *userInfo) {
//在回調里面獲得點擊的
if (platformType == UMSocialPlatformType_UserDefine_Begin+2)
{
NSLog(@"你點擊了復制鏈接按鈕");
}
else
{
[self runShareWithType:platformType];
}
}];
}
- (void)runShareWithType:(UMSocialPlatformType)type
{
//創建分享消息對象
UMSocialMessageObject *messageObject = [UMSocialMessageObject messageObject];
//設置文本
messageObject.text = @"#燕南科技#";
//調用分享接口
[[UMSocialManager defaultManager] shareToPlatform:type messageObject:messageObject currentViewController:self completion:^(id data, NSError *error) {
if (error) {
UMSocialLogInfo(@"************Share fail with error %@*********",error);
}else{
if ([data isKindOfClass:[UMSocialShareResponse class]]) {
UMSocialShareResponse *resp = data;
//分享結果消息
UMSocialLogInfo(@"response message is %@",resp.message);
//第三方原始返回的數據
UMSocialLogInfo(@"response originalResponse data is %@",resp.originalResponse);
}
else
{
UMSocialLogInfo(@"response data is %@",data);
}
}
[self alertWithError:error];
}];
}
- (void)alertWithError:(NSError *)error
{
NSString *result = nil;
if (!error) {
result = [NSString stringWithFormat:@"分享成功"];
}else{
NSMutableString *str = [NSMutableString string];
if (error.userInfo) {
for (NSString *key in error.userInfo) {
[str appendFormat:@"%@ = %@\n", key, error.userInfo[key]];
}
}
if (error) {
result = [NSString stringWithFormat:@"Share fail with error code: %d\n%@",(int)error.code, str];
}
else{
result = [NSString stringWithFormat:@"分享失敗"];
}
}
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"分享"
message:result
delegate:nil
cancelButtonTitle:@"確定"
otherButtonTitles:nil];
[alert show];
}
4.自定義button:
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
self.titleLabel.textAlignment = NSTextAlignmentCenter;
self.titleLabel.font = [UIFont systemFontOfSize:11];
// [self setTitleColor:[UIColor colorWithRed:0.44f green:0.44f blue:0.44f alpha:1.00f] forState:UIControlStateNormal];
[self setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[self setTitleColor:[UIColor colorWithRed:0.64f green:0.64f blue:0.64f alpha:1.00f] forState:UIControlStateHighlighted];
}
return self;
}
-(void)layoutSubviews {
[super layoutSubviews];
self.titleLabel.textAlignment = NSTextAlignmentCenter;
self.imageView.frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.width);
self.imageView.layer.cornerRadius = self.frame.size.width/2;
self.imageView.backgroundColor = [UIColor whiteColor];
self.imageView.clipsToBounds = YES;
self.titleLabel.frame = CGRectMake(0, CGRectGetMaxY(self.imageView.frame), self.frame.size.width, self.frame.size.height-self.frame.size.width);
}
最終顯示樣式: