我正在尝试为iOS创建社交媒体Feed,其中包括instagram照片和评论,facebook照片和评论以及twitter提要。我在为社交媒体插入API密钥时遇到了困难,这是我第一次尝试集成社交媒体供稿。我确实构建了一个我想要的外观模型,但是现在我准备实现我的Social API密钥。
任何指导或有用的提示,将不胜感激。
import UIKit
import SideMenu
class SocialViewController: MainViewControllerFromSideMenu {
private var postCellHeight = CGFloat(44.0)
private var socialAccountMenuSize = CGFloat(261.0)
var postsArray: [Post] = [
Post(socialNetwork: SocialNetwork.twitter, postText: " Post content will display here. Post content will display here.", socialNetworkUser: "Test User"),
Post(socialNetwork: SocialNetwork.instagram, postText: "This is a test picture", postImage: "Cans", socialNetworkUser: "John Doe"),
Post(socialNetwork: SocialNetwork.facebook, postTitle: "This is a test post" ,postText: "This is a test post information with a very long summary...", socialNetworkUser: "Jane Doe"),
Post(socialNetwork: SocialNetwork.twitter, postText: "Post content will display here. Post content will display here. ", socialNetworkUser: "Test User"),
Post(socialNetwork: SocialNetwork.twitter, postText: "Post content will display here. Post content will display here. ", socialNetworkUser: "User 33"),
Post(socialNetwork: SocialNetwork.facebook, postTitle: "This is a test post with picture" , postText: "This is a test post information with a nice picture... here we have the description", postImage: "Cans2", socialNetworkUser: "Jane Doe"),
Post(socialNetwork: SocialNetwork.twitter, postText: "Post content will display here. Post content will display here.", socialNetworkUser: "Peter Doe"),
Post(socialNetwork: SocialNetwork.twitter, postText: "Post content will display here. Post content will display here.", socialNetworkUser: "Test User")]
override func viewDidLoad() {
super.viewDidLoad()
setIPadUI()
socialTableView.delegate = self
socialTableView.dataSource = self
socialTableView.rowHeight = postCellHeight
let nib = UINib(nibName: "SocialTableViewCell", bundle: nil)
socialTableView.register(nib, forCellReuseIdentifier: "socialTableViewCell")
}
private func setIPadUI(){
if (UIDevice.current.userInterfaceIdiom == .pad){
setHeightsForIpad()
sideMenuButton.setImage(UIImage(named: "hamburgerMenuIpad"), for: .normal)
backButton.setImage(UIImage(named: "goBackBlackIpad"), for: .normal)
setFontsForIpad()
setSocialNetworksMenuForIpad()
}
}
private func setHeightsForIpad(){
postCellHeight = CGFloat(154.0)
socialAccountMenuSize = CGFloat(331.0)
}
private func setFontsForIpad(){
socialMediaLabel.font = UIFont(name: "SourceSansPro-Bold", size: 32.0)
subscriptionLabel.font = UIFont(name: "SourceSansPro-Bold", size: 20.0)
}
private func setSocialNetworksMenuForIpad(){
facebookButton.layer.cornerRadius = 8.0
instagramButton.layer.cornerRadius = 8.0
twitterButton.layer.cornerRadius = 8.0
facebookLabel.font = UIFont(name: "SourceSansPro-Semibold", size: 20.0)
twitterLabel.font = UIFont(name: "SourceSansPro-Semibold", size: 20.0)
instagramLabel.font = UIFont(name: "SourceSansPro-Semibold", size: 20.0)
}
@IBAction func sideMenuButtonPressed(_ sender: Any) {
present(SideMenuManager.default.menuLeftNavigationController!, animated: true, completion: nil)
}
@IBAction func addPostButtonPressed(_ sender: Any) {
if !isSocialNetworkMenuShowing {
isSocialNetworkMenuShowing = !isSocialNetworkMenuShowing
updateBackgroundColorWhenPostMenuIsShowing(isMenuShowing: true)
updateAddPostButtonUI(alreadyPressed: true)
activateTapGestureInBackgroundView(whenMenuIsShowing: true)
} else {
isSocialNetworkMenuShowing = !isSocialNetworkMenuShowing
updateBackgroundColorWhenPostMenuIsShowing(isMenuShowing: false)
updateAddPostButtonUI(alreadyPressed: false)
activateTapGestureInBackgroundView(whenMenuIsShowing: false)
}
}
private func updateAddPostButtonUI(alreadyPressed: Bool){
if !alreadyPressed {
addPostButtonImage.image = UIImage(named: "newPublicationSocialButton")
socialNetworksMenuBottomConstaint.constant = -socialAccountMenuSize
addPostButtonWidthConstraint.constant = 23.0
addPostButtonHeightConstraint.constant = 23.0
UIView.animate(withDuration: 0.3) {
self.view.layoutIfNeeded()
}
} else {
addPostButtonImage.image = UIImage(named: "cancelSocialButton")
socialNetworksMenuBottomConstaint.constant = 0.0
addPostButtonWidthConstraint.constant = 18.0
addPostButtonHeightConstraint.constant = 18.0
UIView.animate(withDuration: 0.3) {
self.view.layoutIfNeeded()
}
}
}
private func updateBackgroundColorWhenPostMenuIsShowing(isMenuShowing: Bool) {
if isMenuShowing {
headerView.alpha = 0.5
socialTableView.alpha = 0.5
leftViewTable.alpha = 0.5
rightViewTable.alpha = 0.5
} else {
headerView.alpha = 1.0
socialTableView.alpha = 1.0
leftViewTable.alpha = 1.0
rightViewTable.alpha = 1.0
}
}
private func activateTapGestureInBackgroundView(whenMenuIsShowing isMenuShowing: Bool) {
if isMenuShowing {
enableInteractionView.isHidden = false
let tap = UITapGestureRecognizer(target: self, action: #selector(self.addPostButtonPressed(_:)))
tap.delegate = self as? UIGestureRecognizerDelegate
enableInteractionView.addGestureRecognizer(tap)
} else {
enableInteractionView.isHidden = true
let tap = UITapGestureRecognizer(target: self, action: #selector(self.addPostButtonPressed(_:)))
tap.delegate = self as? UIGestureRecognizerDelegate
enableInteractionView.removeGestureRecognizer(tap)
}
}
@IBAction func subscribeButtonPressed(_ sender: Any) {
}
@IBAction func backButtonPressed(_ sender: Any) {
showHomeView()
}
}
extension SocialViewController: UITableViewDelegate, UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return postsArray.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if let cell = tableView.dequeueReusableCell(withIdentifier: "socialTableViewCell", for: indexPath) as? SocialTableViewCell {
let post = postsArray[indexPath.row]
cell.configuureCellWithPost(post: post)
return cell
}
return UITableViewCell()
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}
}