Php 选项卡栏代理的标准模式是什么
使用tabbar传输数据的标准模式是什么Php 选项卡栏代理的标准模式是什么,php,iphone,ios,nsurlconnection,nsmutabledata,Php,Iphone,Ios,Nsurlconnection,Nsmutabledata,使用tabbar传输数据的标准模式是什么 在您的计算机上设置一个本地服务器(最终这将是网站上托管的服务器,等等)。为此,我建议如下: 下载XAMPP()并按照说明进行操作 在应用程序中安装 使应用程序中XAMPP文件夹中的htcdocs为所有应用程序读写 创建一个名为Tutorials的新文件夹(在htcdocs中) 添加一个名为index.php的新php文件 编写php代码(注意这不安全,只是一个非常基本的示例) 在index.php中编写: //非常不安全,但这会找到函数参数名称 if
- 在应用程序中安装
- 使应用程序中XAMPP文件夹中的htcdocs为所有应用程序读写
- 创建一个名为Tutorials的新文件夹(在htcdocs中)
- 添加一个名为index.php的新php文件
- 编写php代码(注意这不安全,只是一个非常基本的示例)
if(function_exists($_GET['f'])) {
//If found, call the function with value as a parameter
$_GET['f']($_GET['value']);
}
//actual function that takes in a number and finds the square of the number
function getLabel($number){
//This variable response with parameter name is equal to the number times the number
$response['name']=$number*$number;
//Return the data to the caller (in JSON encoded format)
echo json_encode($response);
}
-保存并关闭index.php文件
“连接到PHP”连接到PHP实际上只是加载一个网页。如果您发布此网页只是供其他人以后参考,您需要将其设置为问答格式并接受自己的答案。这是一个问题还是一个教程?无论如何,感谢分享您的经验这是一个非常基本的粗略教程,可以帮助您从iOS应用程序调用PHP方法。将您的问题表述为一个格式良好的问题非常重要,即使您自己正在回答它。以这样一种方式提问,如果有人愿意,他们可以回答。
//Step 1, add NSURLConnectionDataDelegate
//.h
@interface ViewController : UIViewController<NSURLConnectionDataDelegate>
@property (strong, nonatomic) IBOutlet UILabel *answer;
@end
#import "ViewController.h"
@interface ViewController ()
{//step 2 local data objects
NSMutableData*webData;
NSURLConnection*connection;
}
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
//Step 8 call (send) the request
[self getData];
// Do any additional setup after loading the view, typically from a nib.
//NSDictionary*dict=[NSJSONSerialization se]
}
//Step 3 implement this method
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{
[webData setLength:0];
}
//Step 4 append the connection data to your object
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
[webData appendData:data];
}
//Step 5 Process results from request (called automatically when the data arrives)
-(void)connectionDidFinishLoading:(NSURLConnection *)connection{
//Main parse
//Web data
NSError*error;
//Dictionary serialized (processed) using JSON (Way of encoding data from a web request)
NSDictionary*data=[NSJSONSerialization JSONObjectWithData:webData options:0 error:&error];
//If data is nil, then print error
if (data==nil) {
NSLog(@"%@",error);
}
//Print the data
NSLog(@"%@",data);
//Get the numerical result from the dictionary key name
NSNumber*num=[data valueForKey:@"name"];
//Convert number to string
NSString*label=[num stringValue];
//Set the label to this result
_answer.text=label;
}
//Step 7, actually initialize the request
-(void)getData{
//I will break this down as if it where a generic method
//Connect to the index.php file via this URL
//Localhost/tutorials is the XAMPP folder on your computer
//index.php is the php file
//getLabel(int number){}
//?f=getLabel (calling this method in the php file)
//number=900
//&value=900 is the parameter
NSURL*url=[NSURL URLWithString:@"http://localhost/tutorials/index.php?f=getLabel&value=900"];
//In the php file it does number*number and returns the results
//URL request
NSURLRequest*request=[NSURLRequest requestWithURL:url];
//Set the connection
connection = [NSURLConnection connectionWithRequest:request delegate:self];
if (connection) {
webData=[[NSMutableData alloc]init];
}
//*****Results of this request are processed by step 5
}//Step 6, in case data connection fails
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{
NSLog(@"fail");
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end