Swift 2.0 HTTPS请求超时

时间:2015-09-20 22:43:37

标签: json swift https nsurlsession

更新到Swift 2.0后,我的http请求不再有效。由于新的swift 2.0指南,我在将服务器更新为ssl之后将url字符串从http切换到https,但现在我的请求超时了。任何帮助表示赞赏。

func getJSON(){
    activityIndicator.startAnimating()
    let myUrl = NSURL(string: "https://www.example.com/getList.php");
    let request = NSMutableURLRequest(URL:myUrl!);
    request.HTTPMethod = "POST";

    let defaults: NSUserDefaults = NSUserDefaults.standardUserDefaults()
    let userEmail = defaults.objectForKey("email") as? String
    let postString = "email=\(userEmail!)";

    request.HTTPBody = postString.dataUsingEncoding(NSUTF8StringEncoding);

    let task = NSURLSession.sharedSession().dataTaskWithRequest(request) {
        data, response, error in

        if error != nil {
            print("error=\(error)")
            return
        }

        let json = try! NSJSONSerialization.JSONObjectWithData(data!, options: .MutableContainers) as? NSDictionary

        if let parseJSON = json {
            let resultValue = parseJSON["status"] as? String

            print("result: \(resultValue)")

            if(resultValue == "success")
            {
                self.activityIndicator.stopAnimating()
                dispatch_async(dispatch_get_main_queue(), {
                    self.tableData = parseJSON["bList"] as! NSArray
                    self.tableFeatData = parseJSON["fList"] as! NSArray

                    let now = NSDate()
                    let updateString = "Last Updated at " + self.dateFormatter.stringFromDate(now)
                    self.refreshControl.attributedTitle = NSAttributedString(string: updateString)
                    if self.refreshControl.refreshing
                    {
                        self.refreshControl.endRefreshing()
                    }
                    self.tblView!.reloadData()
                })

            }
            else
            {
                self.activityIndicator.stopAnimating()
                let messageToDisplay = parseJSON["message"] as! String!;

                dispatch_async(dispatch_get_main_queue(), {

                    //Display alert message with confirmation
                    let myAlert = UIAlertController(title:"Alert", message:messageToDisplay, preferredStyle: UIAlertControllerStyle.Alert);

                    let okAction = UIAlertAction(title:"Ok", style:UIAlertActionStyle.Default){action in
                        self.dismissViewControllerAnimated(true, completion:nil);
                    }

                    myAlert.addAction(okAction);
                    self.presentViewController(myAlert, animated:true, completion:nil);
                });

            }
        }
    }
    task.resume();
}

0 个答案:

没有答案