自定义友盟分享界面

来源:互联网 发布:东邪西毒黄药师知乎 编辑:程序博客网 时间:2024/06/04 19:42

友盟界面丑吗? 还行吧,但是和需求有点出入,那就让我们自己定义一个吧,把友盟的分享,自己封装到一个VIew上,自己想让它如何出现就怎么出现。


先看看,放一个View上,横着排列,效果图如下;






就把需要分享的平台摆在你自己定义的View上面,随便加,随便点击。弹跳出对应的平台。

好了,定义好了,一直点击没反应。什么鬼,报错是,说没有设置回调的URL 然后我设置了如图所以:



设置好了,然后又报什么没装qq客户端,微信客户端等等端。然后在Info里面设置LSApplicationQueriesSchemes

<array>
<string>wechat</string>
<string>weixin</string>
<string>sinaweibohd</string>
<string>sinaweibo</string>
<string>sinaweibosso</string>
<string>weibosdk</string>
<string>weibosdk2.5</string>
<string>mqqapi</string>
<string>mqq</string>
<string>mqqOpensdkSSoLogin</string>
<string>mqqconnect</string>
<string>mqqopensdkdataline</string>
<string>mqqopensdkgrouptribeshare</string>
<string>mqqopensdkfriend</string>
<string>mqqopensdkapi</string>
<string>mqqopensdkapiV2</string>
<string>mqqopensdkapiV3</string>
<string>mqzoneopensdk</string>
<string>wtloginmqq</string>
<string>wtloginmqq2</string>
<string>mqqwpa</string>
<string>mqzone</string>
<string>mqzonev2</string>
<string>mqzoneshare</string>
<string>wtloginqzone</string>
<string>mqzonewx</string>
<string>mqzoneopensdkapiV2</string>
<string>mqzoneopensdkapi19</string>
<string>mqzoneopensdkapi</string>
<string>mqzoneopensdk</string>
<string>renrenios</string>
<string>renrenapi</string>
<string>renren</string>
<string>renreniphone</string>
<string>yixin</string>
<string>instagram</string>
<string>whatsapp</string>
<string>line</string>
<string>fbapi</string>
<string>fb-messenger-api</string>
<string>fbauth2</string>
<string>fbshareextension</string>
</array>
</plist>

是的你看的没错,我加了一个41个元素的 数组。

因为我用swift写的,所以在调

- (void)postSNSWithTypes:(NSArray *)platformTypes

                 content:(NSString *)content

                   image:(id)image

                location:(CLLocation *)location

             urlResource:(UMSocialUrlResource *)urlResource

     presentedController:(UIViewController *)presentedController

              completion:(UMSocialDataServiceCompletion)completion;

这个方法的时候,一直出不来,什么鬼。

查了查资料,在桥文件里面加了

#import <CoreLocation/CoreLocation.h>

这就完美了。

这个是demo的地址:http://download.csdn.net/detail/koocui/9879004

不想用积分下载,就留下你的邮箱。


或者看下面的代码吧;

1.桥文件

//

//  Header.h

//  自定义友盟分享界面

//

//  Created by CJW on 17/6/15.

//  Copyright © 2017 cjw. All rights reserved.

//

//

#import "UMSocial.h"

#import "UMSocialWechatHandler.h"

#import "UMSocialQQHandler.h"

#import "UMSocialSnsPlatformManager.h"

#import <CoreLocation/CoreLocation.h>


2,分享的View


//

//  XCShareView.swift

//  自定义友盟分享界面

//

//  Created by CJW on 17/6/15.

//  Copyright © 2017 cjw. All rights reserved.

//


import UIKit



typealias shareBlack = (inter:NSInteger) ->()

private let LABEL_HEIGHT:CGFloat  =45

private let  LINE_HEIGHT:CGFloat  =74

class XCShareView: UIView {


    var proStr:String?//头部提示文字

    var duration:CGFloat?//设置弹框背景蒙版灰度

    var btntitleArr:[String]?

    var btnimageArr:[String]?

    var proTitle:String?

    var black:shareBlack?

    

    init(frame:CGRect,titleArr:[String],imageArr:[String],proTitle:String) {

        super.init(frame: frame)

        self.btntitleArr = titleArr

        self.btnimageArr = imageArr

        self.proTitle = proTitle

        loadUIConfig()

        

    }

    

    requiredinit?(coder aDecoder: NSCoder) {

        fatalError("init(coder:) has not been implemented")

    }

    

    //创建分享按钮

    func loadUIConfig() {

        for (index,title)in (self.btntitleArr?.enumerate())! {

            print(index)

            print(title)

            

            let  x:CGFloat = (self.bounds.size.width / 4) * CGFloat(index)

            let  y:CGFloat =0

            let  w:CGFloat =self.bounds.size.width /4;

            let  h:CGFloat =70;

            

            let  frame:CGRectCGRectMake(x, y, w, h);

            let imageWithLable:imageVIewLabel =imageVIewLabel(frame: frame, imageName:self.btnimageArr![index], title: title)

            imageWithLable.tag =100+index

            let tag:UITapGestureRecognizer =UITapGestureRecognizer(target:self, action: #selector(itemClick(_:)))

            imageWithLable.addGestureRecognizer(tag)

            

            self.addSubview(imageWithLable)

           

         }

    }

   


    func itemClick(tap:UITapGestureRecognizer){


        iflet _ =self.black {

          self.black!(inter: tap.view!.tag-100)

        }

      

    }

}

//

//  imageVIewLabel.swift

//  自定义友盟分享界面

//

//  Created by CJW on 17/6/15.

//  Copyright © 2017 cjw. All rights reserved.

//


import UIKit


class imageVIewLabel: UIView {


    privatelazy var imageV:UIImageView = {

        let imageV=UIImageView()

        imageV.contentMode = .ScaleAspectFit

    

        return imageV

    }()

    privatelazy var titleLabel:UILabel = {

        let label =UILabel()

        label.font =UIFont.systemFontOfSize(12)

        label.textAlignment = .Center

        return label

    }()

    

    init(frame:CGRect,imageName:String,title:String) {

        super.init(frame: frame)

        imageV.image =UIImage(named: imageName)

        titleLabel.text = title

        addSubview(imageV)

        addSubview(titleLabel)

    }

    overridefunc layoutSubviews() {

        super.layoutSubviews()

        self.imageV.frame =CGRect(x: 0, y:0, width: self.bounds.width, height: self.bounds.height-30)

        let y =self.imageV.frame.height

        self.titleLabel.frame =CGRect(x: 0, y: y, width:self.bounds.width, height:30)

    }

    

    requiredinit?(coder aDecoder: NSCoder) {

        fatalError("init(coder:) has not been implemented")

    }


}



3,分装友盟分享方法;


//

//  ShareTool.swift

//  自定义友盟分享界面

//

//  Created by CJW on 17/6/23.

//  Copyright © 2017 cjw. All rights reserved.

//


import UIKit

private let DefalutShareUrl ="http://www.baidu.com/"

class ShareTool: NSObject {


    ///单利

    staticlet sharedInstance = ShareTool()

    ///分享

    func share(platformName:String,shareText text:String?,shareImage image:UIImage?,shareUrl url:String?,handler:UIViewController,finished:()->()){

        let shareUrl = url ??DefalutShareUrl

        var shareContent = text

        //分享的文字必须小于140个汉子

        var contextLength = shareContent?.characters.count ??0

        if contextLength >140 {

            shareContent = (shareContent! asNSString).substringToIndex(139)

        }

        if platformName ==UMShareToWechatSession{

            UMSocialData.defaultData().extConfig.wechatSessionData.url = shareUrl

        }elseif platformName == UMShareToWechatTimeline {

            UMSocialData.defaultData().extConfig.wechatTimelineData.url = shareUrl

        }elseif platformName == UMShareToSina {

            contextLength = shareContent?.characters.count ??0

            let urlLenth = url?.characters.count ??0

            if contextLength + urlLenth >140{

                shareContent = (shareContent! asNSString).substringToIndex(139-urlLenth)

            }

            shareContent = shareContent! + "\(shareUrl)"

        }

        let shareImage = image ??UIImage(named: "AppIcon")

        let dataService =UMSocialDataService.defaultDataService()

        // swift中不能直接打出下面的代码,需要在桥接文件中加入#import <CoreLocation/CoreLocation.h>

        dataService.postSNSWithTypes([platformName], content: shareContent, image: shareImage, location:nil, urlResource: nil, presentedController: handler) { (response)in

            finished()

        }

    }

    

    

}























原创粉丝点击