2012-09-07 81 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