在我的應用程序中我使用的是web服務(restful webservice),任何使用一個服務登錄操作的身份驗證用戶的方式,我有一個IBAction的UIButton將調用Web服務,這裏是調用此Web服務的方法:使用多線程登錄iphone?
-(void)LogInMethod{
NSString * password=passwordTextField.text;
NSString * mobileNumber=MobileTextField.text;
if (password.length==0 || mobileNumber.length==0) {
if (mobileNumber.length==0) {
[wrongNoteLable setText:@"please enter a valid mobile number"];
[wrongNoteLable setHidden:NO];
}
else if (password.length==0){
[wrongNoteLable setText:@"please enter a valid password"];
[wrongNoteLable setHidden:NO];
}
}
else{
NSString*UrlString=[[NSString alloc]initWithFormat:@"http://192.168.1.1:8080/test2/eattel/customers/signin/%@/123/%@",mobileNumber,password];
NSURL *url = [[NSURL alloc] initWithString:UrlString ];
NSError *error = nil;
NSStringEncoding encoding = 0;
customerID =[[NSString alloc]initWithContentsOfURL:url encoding:encoding error:&error];
if (customerID) {
if (![customerID isEqualToString: @"-1"]) {
[self performSegueWithIdentifier:@"toMainMenuViewController" sender:self];
NSLog(@"customer loged in with ID :%@",customerID);
}
else if ([customerID isEqualToString:@"-1"]){
[wrongNoteLable setText:@"neither mobile number or password is wrong"];
[wrongNoteLable setHidden:NO];
NSLog(@"customer tried to log in with wrong password or phone Number :%@",customerID);
}
}
else{
[wrongNoteLable setText:@"no connection to the server"];
[wrongNoteLable setHidden:NO];
NSLog(@"customer tried to log in but there is no server connection :%@",customerID);
}
}
// NSLog([dispatch_get_main_queue() description])
}
,我試圖調用使用一個線程前面的方法在IBAction爲這樣的:
- (IBAction)signInAction:(id)sender {
NSThread* myThread = [[NSThread alloc] initWithTarget:self
selector:@selector(LogInMethod)
object:nil];
[myThread start]; // Actually create the thread
}
,但我有此錯誤:
WebThreadLockFromAnyThread(bool), 0xa08cf60: Obtaining the web lock from a thread other than the main thread or the web thread. UIKit should not be called from a secondary thread.
注意:您的代碼和Web服務API對於登錄任務來說並不合適:1.將登錄名和密碼設置爲資源的* path *元素不是一個好主意:URI路徑aren即使在https中也不加密,因此您始終以明文形式發送。 2.您可能需要POST請求,3.您不應該使用'initWithContentsOfURL'來加載遠程資源。使用NSURLConnection或使用委託方法的NSURLSession。 – CouchDeveloper