Skip to content

tangshi/ZhihuDaily

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

知乎日报

拿知乎日报项目练练手,学习Swift(2.0)。

知乎日报API来源于这里

iOS9.0OS X 10.11开始,苹果引入了App Transport Security(ATS)技术,增加了网络传输的安全性,苹果官方说明文档在这里

练手的项目关心啥安全啊,我选择直接关闭ATS!方法参考自这里

Info.plist文件中添加如下项关闭ATS

<key>NSAppTransportSecurity</key>
<dict>
    <!--Include to allow all connections (DANGER)-->
    <key>NSAllowsArbitraryLoads</key>
    <true/>
</dict>

若要使用ATS,添加如下项:

<key>NSAppTransportSecurity</key>
<dict>
    <key>NSExceptionDomains</key>
    <dict>
        <key>news-at.zhihu.com</key>
        <dict>
            <!--Include to allow subdomains-->
            <key>NSIncludesSubdomains</key>
            <true/>
            <!--Include to allow HTTP requests-->
            <key>NSTemporaryExceptionAllowsInsecureHTTPLoads</key>
            <true/>
            <!--Include to specify minimum TLS version-->
            <key>NSTemporaryExceptionMinimumTLSVersion</key>
            <string>TLSv1.1</string>
        </dict>
    </dict>
</dict>

说明:NSExceptionDomains字典列举出所有使用到的服务器域名,上例只列举了一个,即知乎日报的API服务器域名news-at.zhihu.com

网络库使用Alamofire(swift-2.0)

发送网络请求,获得JSON格式的响应数据:

let url_string = "http://news-at.zhihu.com/api/4/news/latest"
Alamofire.request(.GET, url_string).responseJSON { (_, _, result, _) -> Void in
    if let obj = result.value {
        let dict = obj as! NSDictionary
        // do something ... 
    }   
}

从网络下载图片:

let image_url_string = "http://pic3.zhimg.com/9d22a64c7b3d365025d9d3498501bdca.jpg"
Alamofire.request(.GET, image_url_string).response(completionHandler: { (_, _, data, _) -> Void in 
    if let imagedata = data {
        let image = UIImage(data: imagedata)
        // do something ...
    }
})

About

仿知乎日报,学习使用swift开发iOS应用

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published