zoukankan      html  css  js  c++  java
  • [Xcode 实际操作]八、网络与多线程-(25)实现ShareSdk的社会化分享功能

    目录:[Swift]Xcode实际操作

    完成开发包的安装和配置之后,本文将演示社会化分享功能的具体开发步骤。

    在项目导航区,打开并编辑程序代理文件【AppDelegate.swift】

     1 import UIKit
     2 
     3 @UIApplicationMain
     4 class AppDelegate: UIResponder, UIApplicationDelegate {
     5 
     6     var window: UIWindow?
     7 
     8     func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
     9         //首先在应用程序加载完成的代理方法中,添加并配置分享平台
    10         window?.makeKeyAndVisible()
    11         
    12         //使用社会化分享的秘钥,初始化分享功能
    13         ShareSDK.registerApp("1db7f78a841a4", activePlatforms:[
    14             SSDKPlatformType.typeSinaWeibo.rawValue,
    15             SSDKPlatformType.typeWechat.rawValue,
    16             SSDKPlatformType.typeMail.rawValue,
    17             SSDKPlatformType.typeSMS.rawValue,
    18             SSDKPlatformType.typeQQ.rawValue],
    19             //注册三个需要用到的分享平台
    20              onImport: { (platform : SSDKPlatformType) in
    21                 //根据各自的平台类型,连接各自的分享平台
    22                 switch platform
    23                 {
    24                     //连接微博平台
    25                     case SSDKPlatformType.typeSinaWeibo:
    26                         ShareSDKConnector.connectWeibo(WeiboSDK.classForCoder())
    27                     //连接微信平台
    28                     case SSDKPlatformType.typeWechat:
    29                         ShareSDKConnector.connectWeChat(WXApi.classForCoder())
    30                     //连接QQ平台
    31                     case SSDKPlatformType.typeQQ: 
    32                         ShareSDKConnector.connectQQ(QQApiInterface.classForCoder(), tencentOAuthClass: TencentOAuth.classForCoder())
    33                     default:
    34                         break
    35                 }
    36         //连接各分享平台后,接着对各分享平台进行配置操作                        
    37         }) { (platform : SSDKPlatformType, appInfo : NSMutableDictionary?) in
    38             
    39             //根据平台类型进行各自的操作
    40             switch platform
    41             {
    42                 //根据上一篇文章的内容讲述的方法,
    43                 //获得微博一样的相关秘钥信息,对微博分享平台进行配置
    44                 case SSDKPlatformType.typeSinaWeibo:
    45                     appInfo?.ssdkSetupSinaWeibo(byAppKey: "3112623439",
    46                                                 appSecret : "4c69a099cda6104d3f9edac95ea2cc1a",
    47                                                 redirectUri : "http://www.sharesdk.cn",
    48                                                 authType : SSDKAuthTypeBoth)
    49                 
    50                 //对微信分享平台进行配置
    51                 case SSDKPlatformType.typeWechat:
    52                     appInfo?.ssdkSetupWeChat(byAppId: "wxc82577f08316d1b8", appSecret: "c6ea4756136f81c5fb8ceabebeea9fc1")
    53                 
    54                 //对QQ分享平台进行配置
    55                 case SSDKPlatformType.typeQQ:
    56                     appInfo?.ssdkSetupQQ(byAppId: "1106079593",
    57                                          appKey : "tjawfv7ipd2inTcV",
    58                                          authType : SSDKAuthTypeWeb)
    59                 default:
    60                     break
    61             }
    62         }
    63         return true
    64     }
    65     
    66     func applicationWillResignActive(_ application: UIApplication) {
    67         // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    68         // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
    69     }
    70     
    71     func applicationDidEnterBackground(_ application: UIApplication) {
    72         // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    73         // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
    74     }
    75     
    76     func applicationWillEnterForeground(_ application: UIApplication) {
    77         // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
    78     }
    79     
    80     func applicationDidBecomeActive(_ application: UIApplication) {
    81         // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
    82     }
    83     
    84     func applicationWillTerminate(_ application: UIApplication) {
    85         // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
    86     }
    87 }

    在项目导航区,打开视图控制器的代码文件【ViewController.swift】

     1 import UIKit
     2 
     3 class ViewController: UIViewController {
     4     
     5     override func viewDidLoad() {
     6         super.viewDidLoad()
     7         // Do any additional setup after loading the view, typically from a nib.
     8         
     9         //添加一个按钮对象,当用户点击按钮时,弹出分享功能面板
    10         let btShare = UIButton(frame: self.view.frame)
    11         //设置按钮在正常状态下的标题文字
    12         btShare.setTitle("Share App", for: .normal)
    13         //设置按钮在正常状态下的文字颜色
    14         btShare.setTitleColor(.orange, for: .normal)
    15         //为按钮绑定点击事件
    16         btShare.addTarget(self, action: #selector(ViewController.share(_:)), for: .touchUpInside)
    17         self.view.addSubview(btShare)
    18         //将按钮添加到当前视图控制器的根视图       
    19     }
    20     
    21     //添加一个方法,用来响应按钮的点击事件
    22     @objc func share(_ sender: UIButton)
    23     {
    24         //创建一个字典对象,用来存储分享的各项信息
    25         let shareParames = NSMutableDictionary()
    26         //依次设置各个参数
    27         shareParames.ssdkSetupShareParams(byText: "埋骨何须桑梓地,人生无处不青山!",//内容
    28                                           images : UIImage(named: "share.png"),//图标
    29                                           url : NSURL(string:"https://www.cnblogs.com/strengthen/")! as URL,//网址
    30                                           title : "Strengthen",//标题
    31                                           type : SSDKContentType.auto)//分享类型
    32         
    33         //设置分享面板的界面为简洁风格
    34         SSUIShareActionSheetStyle.setShareActionSheetStyle(.simple)
    35         //最后以动作表单的方式,打开分享面板
    36         ShareSDK.showShareActionSheet(nil, items: nil, shareParams: shareParames, onShareStateChanged: nil)        
    37     }
    38     
    39     override func didReceiveMemoryWarning() {
    40         super.didReceiveMemoryWarning()
    41         // Dispose of any resources that can be recreated.
    42     }
    43 }

     如果设备中没有安装相关的程序,点击分享后,在列表中看不到对应的分享平台。

  • 相关阅读:
    java
    java
    java
    js
    java
    异常之异常处理
    面向对象之元类
    面向对象之内置方法
    面向对象之反射
    面向对象之类方法与静态方法
  • 原文地址:https://www.cnblogs.com/strengthen/p/10084979.html
Copyright © 2011-2022 走看看