更新時間:2020-09-23 17:34:16 來源:動力節(jié)點 瀏覽1060次
對于IOS大家都不陌生,是由蘋果公司開發(fā)的一款優(yōu)秀的移動操作系統(tǒng),考慮到我們最近正在學(xué)習(xí)Web Service相關(guān)知識,那么如何寫Web Service的IOS應(yīng)用呢?
下面我們?yōu)榇蠹乙徊揭徊絹碚故網(wǎng)eb Service的IOS應(yīng)用的具體步驟:
一、 前期準備工作:
下載需要使用的第三方庫:JSON,ASIHTTPRequest和MBProgressHUD
接下來就開始創(chuàng)建項目并導(dǎo)入:
1.開啟Xcode創(chuàng)建一個項目,項目類型選擇Single View Application。
2.創(chuàng)建三個Group,并導(dǎo)入上述三個庫。
JSON:將JSON\Classes目錄的文件托入剛才創(chuàng)建的JSON GROUP。
ASIHTTPRequest:將ASIHTTPRequest\Classes目錄的所有文件拖入創(chuàng)建的ASIHTTPRequest GROUP(注意,只要當前目錄的文件,CloudFiles之類的目錄不需要)
ASIHTTPRequest\External\Reachability這里的文件也要加進來
MBProgressHUD:將MBProgressHUD.m和MBProgressHUD.h拖入MBProgressHUD GROUP
以上三個操作,拖入的時候,記得勾選Copy items into destination group’s folder (if needed)選項,意思是把目錄復(fù)制到你的項目中,而不是只引用。
3.導(dǎo)入一些必要的frameworks:點擊左側(cè)導(dǎo)航欄中你的項目->選中target->再選擇build phases欄0->Link Binary with Libraries。點擊+按鈕,搜索CFNetwork.framework and SystemConfiguration.framework,MobileCoreServices.framework, and libz.1.2.3.dylib四個庫。
以上三個大步驟完成后,點擊編譯。完成第一個階段。
二、實現(xiàn)Interface
創(chuàng)建UI:1.label
2.textfield
3.textview
三、與WebService交互
我們的Web Service需要三個參數(shù):
rw_app_id: 應(yīng)用的唯一標識號. If you’ve been following along with the previous tutorial, there should be only one entry so far, App ID #1.
code: The code to attempt to redeem. This should be a string that’s entered by the user.
device_id: The device ID that is attempting to redeem this code. We can get this with an easy API call
我們需要使用POST機制請求WebService。ASIHTTPRequest將使這一過程變得很便捷。
1.創(chuàng)建一個ASIFormDataRequest實例與URL
2.使用setPostValue方法指定各個參數(shù)
3.設(shè)置viewcontroller為request的delegate,之后調(diào)用startAsynchronous來發(fā)起異步請求
4.當請求完畢后,requestFinished或者requestFailed會被回調(diào)
5.requestFinished無論webservice相應(yīng)一個錯誤的代碼,或者正確響應(yīng),都會被調(diào)用,所以在這個函數(shù)里要檢查請求成功或者失敗
6.如果一切順利,再解析收到的JSON數(shù)據(jù)
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
NSLog(@"Want to redeem: %@", textField.text);
// Get device unique ID
UIDevice *device = [UIDevice currentDevice];
NSString *uniqueId= [device uniqueIdentifier];
// Start request
NSString *code = textField.text;
NSURL *url = [NSURL URLWithString:@"http://www.wildfables.com/promos/"];
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
[request setPostValue:@"1" forKey:@"rw_app_id"];
[request setPostValue:code forKey:@"code"];
[request setPostValue:uniqueId forKey:@"device_id"];
[request setDelegate:self];
[request startAsynchronous];
// Hide keyword
[textField resignFirstResponder];
// Clear text field
textView.text = @"";
//狀態(tài)指示器
MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
hud.labelText = @"Redeeming code...";
return TRUE;
}
/*請求完成后回調(diào)*/
- (void)requestFinished:(ASIHTTPRequest *)request
{
[MBProgressHUD hideHUDForView:self.view animated:YES];
if (request.responseStatusCode == 400) {
textView.text = @"Invalid code";
} else if (request.responseStatusCode == 403) {
textView.text = @"Code already used";
} else if (request.responseStatusCode == 200) {
NSString *responseString = [request responseString];
NSDictionary *responseDict = [responseString JSONValue];
NSString *unlockCode = [responseDict objectForKey:@"unlock_code"];
if ([unlockCode compare:@"com.razeware.test.unlock.cake"] == NSOrderedSame) {
textView.text = @"The cake is a lie!";
} else {
textView.text = [NSString stringWithFormat:@"Received unexpected unlock code: %@", unlockCode];
}
} else {
textView.text = @"Unexpected error";
}
}
/*請求失敗后回調(diào)*/
- (void)requestFailed:(ASIHTTPRequest *)request
{
[MBProgressHUD hideHUDForView:self.view animated:YES];
NSError *error = [request error];
textView.text = error.localizedDescription;
}
為了讓用戶感受到,在請求數(shù)據(jù)的時候,程序在運行,而不是假死,所以要添加狀態(tài)指示器。
三個步驟
// Add at the top of the file#import "MBProgressHUD.h"
// Add right before return TRUE in textFieldShouldReturn
MBProgressHUD *hud =[MBProgressHUD showHUDAddedTo:self.view animated:YES];
hud.labelText =@"Redeeming code...";
// Add at start of requestFinished AND requestFailed
[MBProgressHUD hideHUDForView:self.view animated:YES];
編譯運行,大功告成。一個使用Web Service的IOS應(yīng)用就編寫成功啦,當然真正的一款合格的IOS應(yīng)用還需要精心雕琢,查漏補缺。為了能夠勝任這些任務(wù),建議小伙伴們觀看本站的Java教程,學(xué)習(xí)更多的優(yōu)質(zhì)知識來豐富自己,提升自己的編程水平。
初級 202925
初級 203221
初級 202629
初級 203743