2015-06-23 31 views
-1

我的網頁www.example.com/hello.php,PHP代碼爲:如何從應用程序的Xcode網頁信息

echo "Hello"; 

我怎樣才能從我的iPhone應用程序在我的網頁上顯示的信息(使用目標C)。

+1

使用'NSURLSession'來下載它。 SO是關於幫助代碼,而不是提供代碼。盡最大努力連同任何樣本數據,預期結果和問題。 – zaph

+0

我正在嘗試NSString * strURL = [NSString stringWithFormat:@「http://www.example.com/hello.php?var1=%@」,txtUser.text]; NSData * dataURL = [NSData dataWithContentsOfURL:[NSURL URLWithString:strURL]; NSString * strResult = [[NSString alloc] initWithData:dataURL encoding:NSUTF8StringEncoding]; if([strResult isEqualToString:@「Hello」]){ NSLog(「Finally Works」); }但它不起作用:( – XCode4S

+0

將你的代碼添加到問題中,而不是在評論中 – zaph

回答

-1

請在您的評論中發佈代碼,以編輯格式化您的問題!然後,也許我們可以幫助

並嘗試這個代替

NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"YOUR URL"]]; 
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) { 

     NSString *strResult = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; 
     NSLog(@"%@", strResult); 

    }]; 

我知道這會爲工作URL工作,但是當我去你似乎問題發佈網址,要打破這可能是你的問題

+0

@downvoter,我無法評論,因爲我沒有足夠的代表我只是想幫助,我知道這不是一個答案 –

+0

一個問題是代碼忽略'答覆(包括404狀態代碼)(不是down voteer) – zaph

+0

@ zaph那是真的,但是我應該怎麼做所有的im我認爲這是一個非常好的開始 –

0

使用OP的代碼和糾正語法,增加方案的URL和改變dataWithContentsOfURL:添加一個錯誤參數,用於替代xxtxtUser.text

NSString *strURL = [NSString stringWithFormat:@"http://example.com/hello.php?var1=%@", @"xx"]; 
NSURL *url = [NSURL URLWithString:strURL]; 
NSLog(@"url: %@", url); 
NSError *error; 
NSData *dataURL = [NSData dataWithContentsOfURL:url options:0 error:&error]; 
if (dataURL == nil) { 
    NSLog(@"error: %@", error); 
} 
else { 
    NSLog(@"dataURL: %@", dataURL); 
    NSString *strResult = [[NSString alloc] initWithData:dataURL encoding:NSUTF8StringEncoding]; 
    if ([strResult isEqualToString:@"Hello"]) { 
     NSLog(@"Finally Works"); 
    } 
} 

輸出:

url: http://example.com/hello.php?var1=xx
error: Error Domain=NSCocoaErrorDomain Code=256 "The file 「hello.php」 couldn’t be opened." UserInfo=0x1005036d0 {NSURL= http://example.com/hello.php?var1=xx }

注意的錯誤。

如果返回響應的方法(如sendSynchronousRequest:returningResponse:error:)將出現「HTTP錯誤404 - 文件或目錄未找到」錯誤消息。

始終使用返回最多信息的方法調用。

相關問題