Develop the iOS App using the Swift - Part #1

in #utopian-io7 years ago (edited)

New Features

  • What feature(s) did you add?

I am developing steemthink's iOS app and I've got homepage list data.

  • How did you implement it/them?

I got the Homepage data from the following three API

//GET /get_discussions_by_trending
let discussions_by_trending = "https://api.steemjs.com/get_discussions_by_trending?"

//GET /get_discussions_by_created
let discussions_by_created = "https://api.steemjs.com/get_discussions_by_created?"

//GET /get_discussions_by_hot
let discussions_by_hot = "https://api.steemjs.com/get_discussions_by_hot?"

Respectively obtained New, Hot, Trending three modules of data.

We display the username, post date, title, content, SBD, comments, likes, etc. on the home page.

// (Name)
nameLab.text = trending?.author
// (Date)
let timeStr = trending?.last_update.replacingOccurrences(of: "T", with: " ")
timeLab.text = NSDate_STExtension.getDateLong(fromDate: timeStr!)       
// (Title)
titleLab.text = trending?.root_title
// (Content)
contentLab.text = trending?.body
// Avatar (Get a list of personal information by authorName)(UserIcon)
let url = "https://steemitimages.com/u/"+(trending?.author)!+"/avatar"
self.userIconImgV.sd_setImage(with: URL.init(string: url), placeholderImage: UIImage.init(named: "user_icon_placeholder"), completed: nil)

let sbd = trending?.pending_payout_value.components(separatedBy: " ").first
functionView.dollarLab.text = " " + "\(String(describing: sbd!))"
functionView.voteLab.text = String(format: "%d",(trending?.active_votes_arr?.count)!)
functionView.commentLab.text = String(format: "%d",(trending?.children)!)

The avatar is taken separately from the

https://steemitimages.com/u/"+(trending?.author)!+"/avatar

path and it took us quite a while to find this path.

Earlier we considered obtaining from

https://api.steemjs.com/get_accounts?names[]=

but due to the reusability of cell in iOS development, it is very difficult for us to get the personal information data in cell and then to pull Take the head like address.

//
//  STMainTableViewCell.swift
//  SteemThink
//
//  Created by zhouzhiwei on 2018/2/24.
//  Copyright © 2018年 zijinph. All rights reserved.
//
import UIKit
import SDWebImage
    class STMainTableViewCell: STBaseTableViewCell {
    @IBOutlet weak var userIconImgV: UIImageView!
    @IBOutlet weak var nameLab: UILabel!
    @IBOutlet weak var timeLab: UILabel!
    @IBOutlet weak var titleLab: UILabel!
    @IBOutlet weak var contentLab: UILabel!
    @IBOutlet weak var functionView: STMainCellFunctionView!
    var task:URLSessionTask?
    var trending:STContent?{
        didSet{
            //Name
            nameLab.text = trending?.author
            // Date
            let timeStr = trending?.last_update.replacingOccurrences(of: "T", with: " ")
            timeLab.text = NSDate_STExtension.getDateLong(fromDate: timeStr!)
            // Title
            titleLab.text = trending?.root_title
            // Content
            contentLab.text = trending?.body
           // Avatar (Get a list of personal information by authorName)(UserIcon)
            let url = "https://steemitimages.com/u/"+(trending?.author)!+"/avatar"
            self.userIconImgV.sd_setImage(with: URL.init(string: url), placeholderImage: UIImage.init(named: "user_icon_placeholder"), completed: nil)
            let sbd = trending?.pending_payout_value.components(separatedBy: " ").first
            functionView.dollarLab.text = " " + "\(String(describing: sbd!))"
            functionView.voteLab.text = String(format: "%d",(trending?.active_votes_arr?.count)!)
            functionView.commentLab.text = String(format: "%d",(trending?.children)!)
        }
    }
    override func awakeFromNib() {
        super.awakeFromNib()
    }
    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
    }
    func getIconUrl(authorName:String,handler:@escaping (_ iconUrl:String)-> Void) {
        // Cancel the download task
        task?.cancel()
        let author = [authorName]
        let jsonStr = NSArray_STExtension.getJSONStringFromArray(array:author as [AnyObject])
        let url = get_accounts + "names[]=" + jsonStr.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)!
        task = STAFNetworkTools.sharedTools.requestBack(method: .GET, urlString: url, parameters: nil) { (response: Any?,error: Error?) in
            if response is NSArray{
                for dic:Dictionary in response as! [Dictionary<String, Any>]{
                    let user:STUser = STUser.init(dict: dic)
                    if let url = user.json_meta?.user_profile?.profile_image {
                        handler(url)
                    }else{
                        handler("")
                    }
                    break;
                }
            }
        }
    }   
}
//
//  SteemClient.swift
//  SteemThink
//
//  Created by zhouzhiwei on 2018/2/22.
//  Copyright © 2018 zijinph. All rights reserved.
//
import UIKit
let baseURL:String = "https://v2.steemconnect.com"
let appId:String = ""
//GET /get_discussions_by_trending
let discussions_by_trending = "https://api.steemjs.com/get_discussions_by_trending?"
//GET /get_discussions_by_created
let discussions_by_created = "https://api.steemjs.com/get_discussions_by_created?"
//GET /get_discussions_by_hot
let discussions_by_hot = "https://api.steemjs.com/get_discussions_by_hot?"
//GET /get_accounts
let get_accounts = "https://api.steemjs.com/get_accounts?"
class STClient: NSObject {
    typealias STClientCallBack = (_ response: Any?,_ error: Error?)->()
    func post(url:String,body:Dictionary<String, Any>?,cb:String?) -> Void {
        let client = STAFNetworkTools.sharedTools;
//        Set the requested encoding type
        client.requestSerializer.setValue("", forHTTPHeaderField: "")
//        var url:String = baseURL
//        var r = (new Date).toISOString().replace(/[^a-zA-Z0-9]+/g, "").toLowerCase();
//        return e = e.replace(/(-\d{8}t\d{9}z)/g, ""), "re-" + t + "-" + e + "-" + r
    }
    class func get(url:String!,parameters:AnyObject?,to:UIView?,finished:@escaping STClientCallBack){
        STAFNetworkTools.sharedTools.request(method: .GET, urlString: url, parameters: parameters) { (response: Any?,error: Error?) in
            finished(response,error)
        }
    }
}



Posted on Utopian.io - Rewarding Open Source Contributors

Sort:  

Thank you for the contribution. It has been approved.

You can contact us on Discord.
[utopian-moderator]

Hey @codingdefined, I just gave you a tip for your hard work on moderation. Upvote this comment to support the utopian moderators and increase your future rewards!

Upvoted ☝ Have a great day!

thanks~

Hey @steemthinkcom I am @utopian-io. I have just upvoted you!

Achievements

  • You have less than 500 followers. Just gave you a gift to help you succeed!
  • This is your first accepted contribution here in Utopian. Welcome!

Community-Driven Witness!

I am the first and only Steem Community-Driven Witness. Participate on Discord. Lets GROW TOGETHER!

mooncryption-utopian-witness-gif

Up-vote this comment to grow my power and help Open Source contributions like this one. Want to chat? Join me on Discord https://discord.gg/Pc8HG9x