美文网首页
2 oauth vfl

2 oauth vfl

作者: DavidFeng_swift | 来源:发表于2015-11-13 16:08 被阅读0次
    • 布局第一个控件

    • VFL
      字典中的V和H一定要大写

      let dict : [String:AnyObject] = ["iconView":iconView,"loginView":loginBTN]
        iconView.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 0))
        loginBTN.translatesAutoresizingMaskIntoConstraints = false
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:[iconView]-(10)-[loginView]", options: [], metrics: nil, views:dict ))
      
    • 背景颜色

    • 首页动画

      //lazy必须要用计算属性 有等号 = 就是计算属性  
      //并且    在尾部要加()
      private lazy var iconView : UIImageView = {
        let imageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        anim.toValue = 2 * M_PI
        anim.repeatCount = MAXFLOAT
        anim.duration = 20
        //不让动画释放
        anim.removedOnCompletion = false
        imageView.layer.addAnimation(anim, forKey: nil)   
        return imageView
      }()
      
    • 不同控制器显示不同图片
      在visitor中创建loadInfo 方法 为外部提供修改内部字和图片的方法
      在baseController中 添加visitor属性
      每个一级控制器添加loadInfo方法,并赋值

    • 监听方法
      给两个按钮添加代理 监听方法

    • 加载oauth视图
      加载oauth的网址 放在网络工具类中,定义一个属性
      var authURL : NSURL {
      let url = "https://api.weibo.com/oauth2/authorize?client_id=(clientId)&redirect_uself.self.ri=(redirectUri)"

        return NSURL(string: url)!
      }
      
    • 封装AFN

    • 自动填充
      // 自动填充用户信息
      @objc private func autoFill() {
      let js = "document.getElementById('userId').value = 'xxxxxxxx@sina.cn';" +
      "document.getElementById('passwd').value = 'xxxxxx';"

      webView.stringByEvaluatingJavaScriptFromString(js)
      }
      
    • 实现代理方法,跟踪重定向 URL
      返回

      **<NSMutableURLRequest: 0x7fca5ae4a4a0> { URL: https://api.weibo.com/oauth2/authorize?client_id=2075091997&redirect_uri=http://www.baidu.com }**
      **<NSMutableURLRequest: 0x7fca5d0545e0> { URL: https://api.weibo.com/oauth2/authorize }**
      **<NSMutableURLRequest: 0x7fca5af41e00> { URL: https://api.weibo.com/oauth2/authorize# }**
      **<NSMutableURLRequest: 0x7fca5add0950> { URL: https://api.weibo.com/oauth2/authorize }**
      **<NSMutableURLRequest: 0x7fca5aead420> { URL: http://www.baidu.com/?code=76a5c99f5e12685e18f760679eaff44a }**
      **<NSMutableURLRequest: 0x7fca5aebef80> { URL: https://m.baidu.com/?code=76a5c99f5e12685e18f760679eaff44a&from=844b&vit=fps }**
      **<NSMutableURLRequest: 0x7fca5d123d00> { URL: about:blank }**
      

    可以发现code后面就是accesstoken
    获取请求码

      func webView(webView: UIWebView, shouldStartLoadWithRequest request: NSURLRequest, navigationType: UIWebViewNavigationType) -> Bool {
        
        let urlString = request.URL!.absoluteString
        //1.判断URL开头是否是回调地址,如果是 则继续加载
        if !urlString.hasPrefix(NetworkingTools.sharedTools.redirectUri){
            return true
        }
        //2.是回调地址,截取code
        //query是回调URL 问号后面的所有字符串
        if let query = request.URL!.query where query.hasPrefix("code=") {
            let code = query.substringFromIndex("code=".endIndex)
            print(code)
        }else
        {
            //取消
        }
        return false
      }
    
    • accesstoken
      得到code后 再发送给服务器 得到accesstoken
      在网络工具类中写loadaccestoken

      func loadAccessToken(code : String)->RACSignal{
        let urlstring = "https://api.weibo.com/oauth2/access_token"
      
        let param = ["client_id": clientId,
            "client_secret": appSecret,
            "grant_type": "authorization_code",
            "code": code,
            "redirect_uri": redirectUri]
        //提交 参数 用post
       return request(.post, urlString: urlstring, param: param)
      }
      

    在oauth控制器中 订阅信号
    func webView(webView: UIWebView, shouldStartLoadWithRequest request: NSURLRequest, navigationType: UIWebViewNavigationType) -> Bool {

        let urlString = request.URL!.absoluteString
        //1.判断URL开头是否是回调地址,如果是 则继续加载
        if !urlString.hasPrefix(NetworkingTools.sharedTools.redirectUri){
            return true
        }
        //2.是回调地址,截取code
        //query是回调URL 问号后面的所有字符串
        if let query = request.URL!.query where query.hasPrefix("code=") {
            let code = query.substringFromIndex("code=".endIndex)
            print(code)
            //发送请求 获取accesstoken
            //订阅信号
            NetworkingTools.sharedTools.loadAccessToken(code).subscribeNext({ (result) -> Void in
                print(result)
                //成功获得accesstoken
                }, error: { (error) -> Void in
                    print(error)
                })

    相关文章

      网友评论

          本文标题:2 oauth vfl

          本文链接:https://www.haomeiwen.com/subject/nuxkhttx.html