|
|
|
iOS - Error แบบนี้เกิดจากอะไรค่ะ ช่วยดูให้หน่อยค่ะขอบคุณคร้าาา |
|
|
|
|
|
|
|
ในบทความผมก็ทำแบบ Step by Step แล้วน่ะครับ ทำไมยังมี Error อีก
|
|
|
|
|
Date :
2013-12-10 18:52:08 |
By :
mr.win |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
เพราะใช้ xcode 5 หรือป่าวไม่แน่ใจ แล้วมันจะแก้ตรงไหน
Viewlogin.h
Code (Objective-C)
#import <UIKit/UIKit.h>
@interface Viewlogin : UIViewController{
IBOutlet UITextField*txtID;
IBOutlet UITextField*txtPassword;
NSMutableData*inputlogin;
UIAlertView*loging;
}
@property (nonatomic,strong)IBOutlet UITextField *txtID;
@property (nonatomic,strong) IBOutlet UITextField *txtPassword;
@property(nonatomic,strong)NSMutableData *inputlogin;
@property(nonatomic,strong)UIAlertView*loging;
@property(nonatomic,strong)UIButton*btnClose;
@property(nonatomic,strong)UIButton*btnLogin;
- (IBAction)btnLogin:(id)sender;
- (IBAction)btnClose:(id)sender;
Viewlogin.m
Code (Objective-C)
#import "Viewlogin.h"
#import "loginViewController.h"
@implementation Viewlogin
@synthesize txtID;
@synthesize txtPassword;
@synthesize inputlogin;
@synthesize btnLogin;
@synthesize loging;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (IBAction)clearInputText:(id)sender {
txtID.text = @"";
txtPassword.text = @"";
}
- (IBAction)btnLogin:(UIBarButtonItem *)sender {
// Show Progress Loading...
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
loging
= [[UIAlertView alloc] initWithTitle:@"" message:@"Login Checking..." delegate:nil cancelButtonTitle:nil otherButtonTitles:nil];
UIActivityIndicatorView *progress= [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(125, 50, 30, 30)];
progress.activityIndicatorViewStyle = UIActivityIndicatorViewStyleWhiteLarge;
[loging addSubview:progress];
[progress startAnimating];
[progress release];
[loging show];
//sUsername&sPassword
NSString *post = [NSString stringWithFormat:@"sID=%@&sPassword=%@",[txtID text],[txtPassword text]];
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];
NSURL *url = [NSURL URLWithString:@"http://localhost:8888/checkLogin.php"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url
cachePolicy:NSURLRequestReloadIgnoringLocalCacheData
timeoutInterval:10.0];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];
NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:request delegate:self];
if (theConnection) {
self.inputlogin = [[NSMutableData data] retain];
} else {
UIAlertView *connectFailMessage = [[UIAlertView alloc] initWithTitle:@"NSURLConnection " message:@"Failed in viewDidLoad" delegate: self cancelButtonTitle:@"Ok" otherButtonTitles: nil];
[connectFailMessage show];
[connectFailMessage release];
}
}
- (IBAction)btnClose:(id)sender {
}
//- (IBAction)btnClose:(id)sender {
//}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
[inputlogin setLength:0];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
sleep(3);
[inputlogin appendData:data];
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
// ปิด Progress
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
[loging dismissWithClickedButtonIndex:0 animated:YES];
[connection release];
[inputlogin release];
// inform the user
UIAlertView *didFailWithErrorMessage = [[UIAlertView alloc] initWithTitle: @"เกิดข้อผิดพลาด " message: @"ไม่สามารถเชื่อมต่อกับเซิร์ฟเวอร์ได้" delegate: self cancelButtonTitle: @"Ok" otherButtonTitles: nil];
[didFailWithErrorMessage show];
[didFailWithErrorMessage release];
//inform the user
NSLog(@"Connection failed! Error - %@", [error localizedDescription]);
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
// ปิด Progress
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
[loging dismissWithClickedButtonIndex:0 animated:YES];
// Return Status E.g : { "Status":"1", "MemberID":"1", "Message":"Login Successfully" }
// 0 = Error
// 1 = Completed
if(inputlogin)
{
id jsonObjects = [NSJSONSerialization JSONObjectWithData:inputlogin options:NSJSONReadingMutableContainers error:nil];
// value in key name
NSString *strStatus = [jsonObjects objectForKey:@"Status"];
NSString *strID = [jsonObjects objectForKey:@"ID"];
NSString *strMessage = [jsonObjects objectForKey:@"Message"];
NSString *strName = [jsonObjects objectForKey:@"FNameE"];
NSLog(@"Status = %@",strStatus);
NSLog(@"ID = %@",strID);
NSLog(@"postID = %@",txtID.text);
NSLog(@"Message = %@",strMessage);
NSLog(@"Name = %@",strName);
//ดึงID,PasswordจากUserDefaults
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
// saving an NSString
[prefs setObject:txtID.text forKey:@"ID"];
[prefs setObject:txtPassword.text forKey:@"password"];
[prefs synchronize];
// Login Completed
if( [strStatus isEqualToString:@"1"] ){
[self performSegueWithIdentifier:@"LoginSuccess" sender:self];
}
else // Login Failed
{
UIAlertView *error =[[UIAlertView alloc]
initWithTitle:@"เกิดข้อผิดพลาด"
message:strMessage delegate:self
cancelButtonTitle:@"OK" otherButtonTitles: nil];
[error show];
}
}
// release the connection, and the data object
[connection release];
[inputlogin release];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)dealloc {
[txtID release];
[txtPassword release];
[super dealloc];
}
//- (IBAction)btnLogin:(id)sender {
//}
@end
|
|
|
|
|
Date :
2013-12-10 19:32:20 |
By :
fern_105 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ได้กำหนด File's Owner และ Inspector ในส่วนของ Outlet หรือเปล่าครับ
เพราะบทความพี่วิวส่วนมากจะ copy โค้ดไปดื้อๆไม่ได้นะครับ เราต้องกำหนด Outlet ก่อนอ่ะครับ
อันนี้ผมเดาเอานะ เพื่อลืมกันครับเพราะเจอหลายเคสที่ copy ไปวางแล้ว Run สคริปเลย
|
|
|
|
|
Date :
2013-12-12 23:04:07 |
By :
somparn |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
เชคตัว sdk ของ simulator ดูครับว่ามันใช่ sdk เดียวกันหรือป่าว ถ้าไม่ใช่ลองเปิด preference แล้ว update ดู
|
|
|
|
|
Date :
2013-12-12 23:34:59 |
By :
j4kkapongz |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ลองเช็คในส่วนของ outlet ให้ดีๆครับ
หลังๆผม เอาแค่
{
IBOutlet UITextField *txtN;
}
แค่นี้พอครับ
@property (strong,nonatomic) UITextField *txtN; อันนี้ผมไม่ค่อยใช้ละ
|
|
|
|
|
Date :
2013-12-13 13:57:00 |
By :
kwampien |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ขอบคุณผู้รู้ทุกท่านมากค่ะ ตอนนี้ login ผ่านแล้วค่ะ ขอบคุณค่ะ
|
|
|
|
|
Date :
2013-12-18 17:58:02 |
By :
fern105 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 04
|