目錄:[Swift]Xcode實際操做html
完成開發包的安裝和配置以後,本文將演示社會化分享功能的具體開發步驟。swift
在項目導航區,打開並編輯程序代理文件【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】app
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 }
若是設備中沒有安裝相關的程序,點擊分享後,在列表中看不到對應的分享平臺。ide