Thursday, November 28, 2013

Ứng dụng đăng nhập trong Iphone


  1. Tạo mới project và chọn Single View Application, sau đó chọn Next


2. Ở đây ta tiến hành đặt tên cho ứng dụng của mình. Đặt tên xong thì ấn Next rồi chọn nơi lưu ứng dụng của mình. Cuối cùng là ấn Finish.


3.  Sau đó Xcode sẽ tạo cho ta một khung để làm việc.


Thông tin khái quát:
  • 1. AppDelegate: được tạo mặc định, khai báo cho lớp Application Delegate cho ứng dụng của bạn.
  • 2. Thiết thế các giao diện. 
  • 3. ViewController: Nơi code chính cho 1 view nào đó.
  • 4. Unit test
  • 5. Các thư viện cần sử dụng.

4.  Bây giờ ta sẽ tiến hành thiết kế giao diện
     Ở phía bên phải của Tool ta sẽ thấy được nơi mà ta có thể tuỳ chọn các control tương ứng để thiết        kế cho Form đăng nhập của ta. 
     Form đăng nhập thông thường sẽ có các control (component) kiểu như: Label, Textfield và  Button.

   Ta sẽ kéo Label đầu tiên. Chỉ việc chọn và kéo thả đơn thuần rồi để ở nơi tuỳ thích trên view.

   



Tương tự ta cũng sẽ kéo 2 Textfield tương ứng cho ô nhập Username và password

Cuối cùng ta sẽ kéo thả thêm 1 nút Button. Để xử lý hành động đăng nhập.

Xong thì sẽ có 1 view tương tự như sau:




Ấn nút Build để hiển thị kết quả.

5. Tiếp theo ta sẽ tuỳ chỉnh cho form login cho đúng logic.


Đầu tiên ta sẽ xử lý ô nhập mật khẩu.

                Ở phía tay phải trên cùng của màn hình. Ta sẽ nhìn thấy những tuỳ chỉnh kiểu như thế này.
               Ta phải chọn Textfield nhập password và tìm tới chỗ có thông tin như sau:
               Check vào ô Secure.


Build lại ứng dụng và xem kết quả.



6. Tiến hành gắn kết các control từ view tới các biến trong các Controller để ta có thể thao tác dễ dàng.

Vẫn ở phía trên cùng bên phải của Tool. Ta chọn biểu tượng sau:


Sau khi chọn biểu tượng này, Tool của ta sẽ có giao diện như sau:


Một nửa bên trái sẽ là view, còn 1 nửa bên phải là file Controller tương ứng của view đó. 
Lưu ý là ta sẽ phải chọn cho đúng file Controller tương ứng cho view hiện tại.



Như ở hình trên ta sẽ chọn Recent Files  là ViewController.h, khi chọn xong sẽ có dạng như sau:



  Ở đây ta đã có thể bắt đầu liên kết các control từ view tới Controller:

Chọn control ta muốn làm, ví dụ là Texfield Username:
Ta chọn nó rồi ấn chuột phải 


Tiếp theo ta sẽ khai báo tên biến.


Cuối cùng ta ấn nút Connect

Kết quả.

Tương tự ta cũng Control Textfield và button Login cũng làm tương tự như trên.

Hoàn tất công đoạn sẽ có hình như trên

  Từ bây giờ ta sẽ có thể lấy được dữ liệu từ người dùng nhập vào trên view xuống Controller để xử lý.

Ta se tạo 1 sự kiện button click vào nút Login để lấy thông tin từ 2 ô username và password để xử lý.

Khai báo 1 action có tên như sau trong file ViewController.h

- (IBAction)actionLogin:(id)sender;

Tiếp theo ta sẽ mở lại file Main.storyboard


 Sau đó sẽ có giao diện như sau:





Tiếp đến sẽ hiện như sau:
Ta sẽ chọn sự kiện: Touch Up Inside (Click vào)

Xong bước này ta sẽ mở file ViewController.m ra và tiến hành implement cho action đó.

//action Login
- (IBAction)actionLogin:(id)sender {
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Message" message:[NSString stringWithFormat:@"Username: %@\n Password: %@",self.txtUsername.text, self.txtPassword.text] delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
    [alertView show];
}

Đoạn code trên mình sẽ hiển thị  Username và pasword khi người dùng nhập vào.

      Tiếp theo, khi ta tiến hành nhập dữ liệu cho các ô username và password, mặc định bàn phím sẽ không có cách nào để ẩn đi.



Bước đầu tiên ta phải mở file ViewController.h và thêm vào đoạn code sau:

           Trước:
@interface ViewController : UIViewController
           Sau:
@interface ViewController : UIViewController<UITextFieldDelegate>
 Bước tiếp theo ta implement 1 phương thức của  UITextFieldDelegate, mở file ViewController.m

thêm phương thức sau:

// hidden keyboard
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}
Tiếp theo ta phải gán delegate cho các control từ view vào controller.

Mở file Main.storyboard ra và chọn 2 Textfield: Username và password.



Hiển thị như này là ok.

Tương tự như vậy ta cũng làm cho Textfield Password.

Build lại app và kiểm tra kết quả.

Đến đây là hoàn tất công đoạn tạo ứng dụng Login và hiển thị lên 1 message thông tin.
Những bài hướng dẫn sau mình sẽ làm 1 ứng dụng khác để có thể hướng dẫn cho mọi người kĩ càng và làm quen với nhiều control khác nhau của Iphone hơn.

Chúc mọi người thành công!

Thanks! 





Saturday, September 14, 2013

Học Objective-C với Java Swing


Hôm nay mình sẽ hướng dẫn các bạn từng bước làm quen với ngôn ngữ Objective-C thông qua ngôn ngữ Java (Swing) để mọi người có cách tiếp cận dễ dàng và trực quan nhất.

Dưới đây sẽ là từng bước để tạo ra một ứng dụng HelloWorld bằng: Java Swing (sử dụng NetBeans) và Objective-C (Iphone) (sử dụng XCode) :

Bước 1: Tạo một java application thông thường với tên gọi: HelloWorldJavaAndObjectiveC
Bước 2: Tạo một JFrame Form để kéo thả giao diện



Bước 3: Ở đây mình đặt tên là : "MainFrame" với Package có tên là : "helloworld"







Bước 4: Kéo thả một jLabel để hiện thị nội dung nào đó.



Bước 5: Đổi tên jLabel1 thành lblHelloWorld.



Bước 6: Ở menu Source | Design | History  ta chọn sang menu Source để làm như sau:

Tại hàm khởi tạo ta sẽ gán giá trị cho Label HelloWorld vừa rồi với nội dung là kiểu String : "Hello World"



Bước 7: Chạy app để hiện thị kết quả:





Bước 7: Chạy app để hiện thị kết quả:




Hello World trên XCode:

Bước 1: Tạo một ứng dụng thông thường, ở đây ta chọn Single View Application
Ấn Next để thực hiện bước tiếp theo:





Bước 2: Đặt tên chỗ Product Name là "HelloWorldJavaAndObjectiveC" 
Để mặc định check box: Use Automatic Reference Counting
Và ấn Next rồi ấn Create



Bước 3: Đây chính là file để mình làm giao diện cũng tương tự như JFrame ở swing ở trên


Bước 4: Ta cũng kéo thả 1 Label để hiện chữ Hello World.








Bước 5: Để tiện kéo thả, ta chọn tab "Show the Assistant editor" 



Bước 6: Click chuột phải vào Label và kéo thả ở phần "New Referencing Outlet" sang màn hình bên phải và đặt tên cho nó. Cách làm này sẽ liên kết Label ta kéo ở file "xib" với biến "lblHelloWorld" trong class ViewController.h



Bước 7: Ta mở file có tên: "ViewController.m" ra. Objective-C thông thường sẽ gợi ý cho ta nên tạo class với 2 file đó là file: h và file m. (Phần này mình sẽ đề cập sâu hơn ở phần sau.)
File "ViewController.m" sẽ là nơi ta viết code xử lý chính cho file giao diện "ViewController.xib"

Ở đây các bạn sẽ nhìn thấy một hàm có tên là:
- (void) viewDidLoad {
                [super viewDidLoad];

           }

hàm này cũng tương tự  như hàm khởi tạo của Java Swing trên.
Do đó ta sẽ làm tương tự như ở phần Java ở trên.
Từ khoá: self chính là this trong java. Do đó ai làm java chắc sẽ không quá khó để hiểu nó. ^^ 












Bước 8: Chạy app lên và xem kết quả.




Với ví dụ "hello world " này hy vọng đã giúp mọi người thấy được phần nào cách làm việc và phong cách code ở 2 ngôn ngữ. Cũng có nhiều khá nhiều khác lạ khi chuyển từ code java sang code objective-c bởi vì đó là cảm giác từ chính bản thân mình.

Sẽ bắt nhịp với nó nhanh thôi. :)

Cheer!




Tuesday, May 14, 2013

Config GData-objective-c-client to a Project


  1. Checking Out Sources
  2. Create Project to Test
  3. Add Source code GData to my Project
  4. Build Project and Copy Folder Headers
  5. Linking to the iPhone Static Library
  6. Removing Unneeded Code
  7. Config to Product/Archive


Checking Out Source

                Check out with link : http://gdata-objectivec-client.googlecode.com/svn/trunk/ 

Create Project To Test 

Copy Source code to my Project on folder:
              ex: GData

Add Source Code to Project.
              
    
Add a new target dependency:
             a.  Click your target -> Build Phases -> Target Dependencies -> the + button
             

             b. Choose GDataTouchStaticLib and click Add
             
             b.   Build Phases -> Link Binary With Libraries,  the + button 
                   add the libGDataTouchStaticLib.a
                   
             c. Also add the following frameworks at the same place, they are needed by GData.
    • SystemConfiguration.framework
    • Security.framework
    • libxml2.2.dylib
    • libz.1.2.5.dylib                   
            
           d. In Build Settings,  add the following entries under “Other Linker Flags”
               
          e. Also in Build Settings, set /usr/include/libxml2 under “Header Search Paths”
            
          f. Add some settings for Project GData
              Select GData.xcodeproj -> Select GDataTouchStaticLib - > Build Settings : type "Other C"
              Add to Debug and Release : 
                     -DGDATA_REQUIRE_SERVICE_INCLUDES=1
                     -DGDATA_INCLUDE_YOUTUBE_SERVICE=1
                
             
      
Build Project and Copy Folder Headers
          Build your project (⌘B). When build success.
         In your derived data directory, browse to Build/Products/Debug-iphonesimulator
           
           

        Copy Folder Headers to my Folder GData in Project and Add Files to ..                                                                
           
             

Removing Unneeded Code

However you build the library, it is worthwhile to remove code for the services that your application will not be using. To remove unneeded code, set these compile flags in the project or target build settings of the GData.xcodeproj project file. When compiling the sources directly into your application, set these flags in your project's settings.
The conditional flags in most source files of the library look something like this:
#if !GDATA_REQUIRE_SERVICE_INCLUDES || GDATA_INCLUDE_CONTACTS_SERVICE
So in either the project's settings for compile flags or in your project's config file, set GDATA_REQUIRE_SERVICE_INCLUDES to 1, along with the services you are using. For example,
Other C Flags: -DGDATA_REQUIRE_SERVICE_INCLUDES=1 -DGDATA_INCLUDE_CONTACTS_SERVICE=1
If your project compiles the OAuth 2 classes for authentication, also define the conditional -DGTM_INCLUDE_OAUTH2=1 in Other C Flags.
Do remember to set build settings so they apply to both Debug and Release configurations.
There is a conditional set in the static library target as a reminder to developers to define the needed services. For your project, replace or delete the definition
-DGDATA_INCLUDE_nameServiceHere_SERVICE=1
in the Other C Flags section of the static library target's Release configuration.

Config to Product/Archive

          


*Note: You can comment some code:
#if !DEBUG
  #if GDATA_INCLUDE_nameServiceHere_SERVICE && !GDATA_SUPPRESS_INCLUDE_WARNING
    #error The service placeholder compiler flag should be replaced by actual service specifications
  #endif
#endif

and 

Select GData.xcodeproj -> Select GDataTouchStaticLib - > Build Settings : type "skip" and change to Yes.



Ok done.
Good luck!