dongtan6206 2016-07-15 09:51
浏览 62

无效的JSON需要Swift iOS

I am developing a simple iOS Swift app. I want to make a log-in screen for my app that will connect to already existing user database in Wordpress. I found some PHP scripts and Swift code.

I am trying to post username and login, check it and return the result(isUser = true/false or 1/0)

Here is a PHP script

<?php
// Read request parameters
$username= $_REQUEST["username"];
$password = $_REQUEST["password"];
// Store values in an array
$returnValue = array("username"=>$username, "password"=>$password);
// Send back request in JSON format
echo json_encode($returnValue);
?>

and a Swift function

func getPHPJson() {

    let urlPath: String = "LINK_TO_PHP_FILE?username=\(user)&password=\(pass)"
    let url: NSURL = NSURL(string: urlPath)!
    let request1: NSMutableURLRequest = NSMutableURLRequest(URL: url)

    request1.HTTPMethod = "GET"
    let queue:NSOperationQueue = NSOperationQueue()

    NSURLConnection.sendAsynchronousRequest(request1, queue: queue, completionHandler:{ (response: NSURLResponse?, data: NSData?, error: NSError?) -> Void in

    do 
    {
        if let jsonResult = try NSJSONSerialization.JSONObjectWithData(data!, options: []) as? NSDictionary 
        {
            print(jsonResult)
            //print(jsonResult["isUser"] as! Bool)
            let responseString = NSString(data: data!, encoding: NSUTF8StringEncoding)
            print("raw response: \(responseString!)")
        }
    } catch let error as NSError 
    {
        print(error.localizedDescription)
        print("error")
        let responseString = NSString(data: data!, encoding: NSUTF8StringEncoding)
        print("raw response: \(responseString)")
    }
    })
}

it gives me a desired result.

OUTPUT

{
    password = iosappusettest1;
    username = iosappusettest;
}
raw response: {"username":"iosappusettest","password":"iosappusettest1"}

But when I add function to check if user is registered - I always get an invalid JSON

here is PHP function

function authentication ($user, $pass){
  global $wp, $wp_rewrite, $wp_the_query, $wp_query;

  if(empty($user) || empty($pass)){
    return 0;
  } else {
    require_once('../wp-blog-header.php');
    $status = 0;
    $auth = wp_authenticate($user, $pass );
    if( is_wp_error($auth) ) {      
      $status = 0;
    } else {
      $status = 1;
    }
    return $status;
  } 
}

I believe the problem is require_once function. In raw response I get a lot of html tags and other data that make my JSON invalid.

  • Is there any way to clear the page and output in the JSON only? echo "<script> document.documentElement.innerHTML = ''; </script>"; in PHP did't help me. maybe jQuery will help?
  • Maybe I can check if user is registered in another way?
  • Maybe I can store my result in a separate place or temp file?
  • Maybe I should wrap my data not in JSON but something else?

So I need to pass username and password to PHP, check it with authentication function(that uses require_once) and send back the $status to iOS Swift app ass variable.

Question is answered. The problem was in WP plugin, that redirected all unlogined users from all the links in that domain, so when i called require_once('../wp-blog-header.php'); i was always redirected to main page and that was the reason for wrong JSON file with all the HTML markup.

  • 写回答

1条回答 默认 最新

  • douhui4699 2016-07-15 10:03
    关注

    Keep your response in pure JSON, and leave out any html tags as they are not needed by your ios end.

    评论

报告相同问题?

悬赏问题

  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制
  • ¥20 usb设备兼容性问题
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊
  • ¥15 安装svn网络有问题怎么办
  • ¥15 Python爬取指定微博话题下的内容,保存为txt
  • ¥15 vue2登录调用后端接口如何实现
  • ¥65 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖