我是iphone的初学者。我想为我的应用程序创建一个登录页面。我无法弄清楚如何连接到php页面并从mysql数据库中检索相应的数据到iphone.could任何人指导我如何去做吧。
答案 0 :(得分:0)
iphone与php和mysql之间的连接有什么关系?
PHP将在Web服务器上运行,可能在某些计算机上安装了apache并且它将连接到MySQL数据库..并且您将使用浏览器从您的iphone访问该php页面。除了提供浏览器之外,不确定iphone中除了提供浏览器之外还有哪些部分
答案 1 :(得分:0)
您可能需要查看NSURLRequest,您可以将其与NSURLConnection一起发送,例如GET- URL的参数。然后,您可以实施NSURLConnectionDelegate以响应传入的数据:
1)设置连接
receivedData =[NSMutableData data];
NSURLRequest *theRequest=[NSURLRequest requestWithURL:[NSURL URLWithString:url]
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:20.0];
NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
2)在self中设置委托方法:
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse*)response {
NSHTTPURLResponse * httpResponse = (NSHTTPURLResponse *) response;
if([httpResponse statusCode]==200)
[receivedData setLength:0];
else
NSLog(@"Http-Reponse %u",[httpResponse statusCode]);
// HANDLE ERROR!
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
// append the new data to the receivedData
// receivedData is declared as a method instance elsewhere
[receivedData appendData:data];
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
// HANDLE THE CONNECTION ERROR
// release the connection, and the data object
[connection release];
// receivedData is declared as a method instance elsewhere
[receivedData release];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
// receivedData contains the data
// convert to string:
NSLog(@"finished loading: %@",[[[NSString alloc] initWithData:receivedData encoding:NSUTF8StringEncoding] autorelease]);
[connection release];
[receivedData release];
}
答案 2 :(得分:0)
您希望将身份验证功能公开为Web服务,然后使用Felix L.发布的URL加载代码启动与Web服务的实际连接。
您可能希望以XML格式从服务器发送响应,如果是这样,您将使用NSXMLParser解析该响应,否则您只需以您喜欢的任何格式发送响应并进行适当的解析。