iphone于php通讯

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

iPhone与php通信简单例子
iPhone是⼀一个非常厉害的新兴平台,值得我们去学习。

如果有兴趣的话,可以长期关注本博客。

今天我们演示⼀一个与php通信的简单例子,效果如下图所示:
下面我们开始制作这个程序:
1.创建⼀一个名为HttpReqExample的类型为View-based Application的xcode工程,如果还不会创建Xcode工程,请参考:iPhone按钮的使用。

2.打开HttpReqExampleViewController.h文件,修改其中的代码如下:
1.//
2.// HttpReqExampleViewController.h
3.// HttpReqExample
4.//
5.// Created by jiaqiu on 10-9-28.
6.// Copyright __MyCompanyName__ 2010. All rights reserved.
7.//
8.
9.#import <UIKit/UIKit.h>
10.
11.@interface HttpReqExampleViewController : UIViewController {
12. IBOutlet UITextField *yourNameTxt;
13. IBOutlet UIButton *sendBtn;
14. IBOutlet UITextView *outTxt;
15. IBOutlet UILabel *yourNameTxtAlertLabel;
16.}
17.
18.#define SERVER_URL @"http://localhost/aptanawork/
iPhoneServer/iphoneserver.php?"
19.
20.-(IBAction)btnPressed:(id)sender;
21.
22.@end
其中的SERVER_URL常量你可以根据自己的需要修改成对应的php文件的路径。

3.双击HttpReqExampleViewController.xib文件打开Interface Builder,设计界面如效果图所示,并将对应的组件分别绑定为yourNameTxt, sendBtn, outTxt, yourNameTxtAlertLabel,如果还不会绑定组件,请参考:iPhone按钮的使用。

4.打开文件HttpReqExampleViewController.m,修改其中的代码如下:
1.//
2.// HttpReqExampleViewController.m
3.// HttpReqExample
4.//
5.// Created by jiaqiu on 10-9-28.
6.// Copyright __MyCompanyName__ 2010. All rights reserved.
7.//
8.
9.#import "HttpReqExampleViewController.h"
10.
11.@implementation HttpReqExampleViewController
12.
13./*
14.// The designated initializer. Override to perform setup that is required before the view is loaded.
15.- (id)initWithNibName:(NSString *)nibNameOrNil bundle: (NSBundle *)nibBundleOrNil {
16. if ((self = [super initWithNibName:nibNameOrNil
bundle:nibBundleOrNil])) {
17. // Custom initialization
18. }
19. return self;
20.}
21.*/
22.
23./*
24.// Implement loadView to create a view hierarchy programmatically, without using a nib.
25.- (void)loadView {
26.}
27.*/
28.
29./*
30.// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
31.- (void)viewDidLoad {
32. [super viewDidLoad];
33.}
34.*/
35.
36./*
37.// Override to allow orientations other than the default portrait orientation.
38.- (BOOL)shouldAutorotateToInterfaceOrientation: (UIInterfaceOrientation)interfaceOrientation {
39. // Return YES for supported orientations
40. return (interfaceOrientation == UIInterfaceOrientationPortrait);
41.}
42.*/
43.
44.-(void)btnPressed:(id)sender{
45. [yourNameTxt resignFirstResponder];
46.
47. if ([yourNameTxt.text compare:@""]) {
48. NSString *reqUrl=[NSString stringWithFormat:@"%@%@%@",SERVER_URL,@"name=",yourNameTxt.text];
49. outTxt.text=[NSString stringWithContentsOfURL:[NSURL URLWithString:reqUrl]
encoding:NSUTF8StringEncoding error:nil];
50. yourNameTxtAlertLabel.text=@"";
51. }else {
52. yourNameTxtAlertLabel.text=@"名字不能为空";
53. }
54.
55.}
56.
57.-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent
*)event{
58. [yourNameTxt resignFirstResponder];
59.}
60.
61.- (void)didReceiveMemoryWarning {
62. // Releases the view if it doesn't have a superview.
63. [super didReceiveMemoryWarning];
64.
65. // Release any cached data, images, etc that aren't
in use.
66.}
67.
68.- (void)viewDidUnload {
69. // Release any retained subviews of the main view.
70. // e.g. self.myOutlet = nil;
71.}
72.
73.- (void)dealloc {
74. [super dealloc];
75. [sendBtn release];
76. [yourNameTxt release];
77. [outTxt release];
78. [yourNameTxtAlertLabel release];
79.}
80.
81.@end
5.以上是准备客户端,那么下面就该准备服务器了,在你的php服务器(如果你还没有php服务器,请下载⼀一个xampp来安装,安装后 /Applications/XAMPP/xamppfiles/ htdocs 目录就是你的web根目录)的相应的站点目录中建立⼀一个名为iphoneserver.php的php文件,在其中输入以下代码:
1.<?php
2.if ($_GET['name']){
3. echo 'Hello '.$_GET['name'];
4.}else {
5. echo '0';
6.}
6.运行iPhone程序,尝试操作⼀一下,就可以看到效果了。

相关文档
最新文档