2012-09-07 49 views
2

我一直在努力将一个NSString传递给php文件,我想要得到的结果是在viewDidload之后传递一个id到php文件,以便我可以使用id来查询mysql数据库并检索数据。有人可以教我如何做到这一点?我看到很多人在下面讨论这些代码,但我不太确定它是什么。将数据从xcode传递到php

NSString * [email protected]"1"; 
NSString * post = [[NSString alloc] initWithFormat:@"&id=%@", myString]; 
NSData * postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:NO]; 
NSString * postLength = [NSString stringWithFormat:@"%d",[postData length]]; 
NSMutableURLRequest * request = [[NSMutableURLRequest alloc] init]; 
[request setURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://localhost/getdata.php"]]]; 
[request setHTTPMethod:@"POST"]; 
[request setValue:postLength forHTTPHeaderField:@"Content-Length"]; 
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"]; 
[request setHTTPBody:postData]; 
NSURLConnection * conn = [[NSURLConnection alloc] initWithRequest:request delegate:self]; 

if (conn) NSLog(@"Connection Successful"); 

这些代码可以在我的情况下工作吗?感谢您的时间和建议。

+0

你有一些似乎合理的代码...为什么不尝试并调整它。或者至少告诉我们什么是行不通的。还要看看NSURLConnections类的方法“sendAsynchronousRequest ...”,它将一个块作为完成处理程序,并绕过“繁琐”的委托调用来处理您可能不需要的细致的响应。 – Mario

回答

0

下面是我做同样的事情了我自己的应用程序之一的方式:

* 注:这将运行一个GET请求,不POST请求,但它确实是在后台和高效运行

创建类从服务器上执行的数据检索(I称为矿online.h /米)

AppDelegate.h

// 
// AppDelegate.h 
// Faith Life Fellowship Streamer 
// 
// Created by David Worth on 7/17/11. 
// Copyright 2011 Math Nerd Productions, LLC. All rights reserved. 
// 

#import <UIKit/UIKit.h> 

@class ViewController; 

@interface AppDelegate : NSObject <UIApplicationDelegate> { 
    NSOperationQueue *queue; 
} 

@property (nonatomic, retain) IBOutlet UIWindow *window; 

@property (nonatomic, retain) IBOutlet ViewController *viewController; 
@property (nonatomic, retain) NSString *receivedURL; 

- (void)stringLoad:(NSString*)str; 
+ (id)shared; 
- (void)addOperation:(NSOperation*)operation; 

@end 

这些作品添加到 AppDelegate.m

@synthesize receivedURL; 

static AppDelegate *shared; 

+ (id)shared; 
{ 
    if (!shared) { 
     [[AppDelegate alloc] init]; 
    } 
    return shared; 
} 

- (id)init 
{ 
    if (shared) { 
     [self autorelease]; 
     return shared; 
    } 
    if (![super init]) return nil; 

    queue = [[NSOperationQueue alloc] init]; 
    shared = self; 
    return self; 
} 

- (void)addOperation:(NSOperation *)operation 
{ 
    [queue addOperation:operation]; 
} 

- (void)stringLoad:(NSString *)str 
{ 
    self.receivedURL = str; 
    [[NSNotificationCenter defaultCenter] postNotificationName:@"string" object:nil]; 
} 

online.h

// 
// online.h 
// FLF Streamer 
// 
// Created by David Worth on 4/30/12. 
// Copyright (c) 2012 Math Nerd Productions, LLC. All rights reserved. 
// 

#import <Foundation/Foundation.h> 
#import "AppDelegate.h" 

@interface online : NSOperation 

@property (nonatomic, retain) NSString* str; 
@property (nonatomic) int type; 

- (id)initWithString:(NSString*)url; 

@end 

online.m

// 
// online.m 
// FLF Streamer 
// 
// Created by David Worth on 4/30/12. 
// Copyright (c) 2012 Math Nerd Productions, LLC. All rights reserved. 
// 

#import "online.h" 

@implementation online 
@synthesize str; 

- (id)initWithString:(NSString *)url 
{ 
    if (![super init]) return nil; 
    [self setStr:url]; 
    return self; 
} 

- (void)dealloc { 
    [str release], str = nil; 
    [super dealloc]; 
} 

- (void)main { 
    NSString *webpageString = [[[NSString alloc] initWithContentsOfURL:[NSURL URLWithString:str] encoding:NSUTF8StringEncoding error:nil] autorelease]; 

    [[AppDelegate shared] performSelectorOnMainThread:@selector(stringLoad:) 
                     withObject:webpageString 
                     waitUntilDone:YES]; 
} 

@end 

然后在你的viewco ntroller.m,调用它(与PHP页面要访问的URL替换mysite.com ...):

online* o = [[online alloc] initWithString:@"http://www.mysite.com/myphppage.php?variable1=blah+blah+blah"]; 
AppDelegate* delegate = [AppDelegate shared]; 
[delegate addOperation:o]; 

而在你viewcontroller.m viewDidLoad方法:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(myMethodToProcessWhatTheServerReturns) name:@"string" object:nil]; 

然后使该方法在viewController.m是什么解析服务器返回:

- (void)myMethodToProcessWhatTheServerReturns 
{ 
    NSString* serverResponse = [AppDelegate shared].receivedURL; 
    //Do stuff with serverResponse 
} 

编码快乐!

+0

不是很好的设计。使用NSOperation的子类和应用程序委托的单例实例(为什么?)只是将URL的内容作为字符串来获取?矫枉过正,不好意思看我的意见,对不起。或者我误解了一些东西。 – Mario

+0

那么如果你有更好的东西,分享它,否则,这种方法有效地工作,并允许前台处理其他事件。我没有遇到任何使用它的问题,如果你只是做了整个stringwithcontentsofurl的事情,而没有在后台运行它,它挂起你的系统。 – David