Hiển thị các bài đăng có nhãn Lập trình IOS. Hiển thị tất cả bài đăng
Hiển thị các bài đăng có nhãn Lập trình IOS. Hiển thị tất cả bài đăng
Hướng dẫn thiết lập Boot từ tập tin ISO cho máy tính

Hướng dẫn thiết lập Boot từ tập tin ISO cho máy tính

iệc thiết lập Boot từ đĩa cứu hộ trong máy tính để sử dụng các công cụ trong đó là công việc thường làm của bạn. Tuy nhiên, bỗng một ngày nào đó bạn phát hiện ổ đĩa CD/DVD máy tính của mình bị hỏng hoặc đĩa cứu hộ không sử dụng được thì lúc đó chắc hẳn bạn sẽ phải gặp một số rắc rối đây.
Với bài viết này, tôi sẽ hướng dẫn bạn đọc cách thiết lập Boot từ ngay tập tin ISO đĩa cứu hộ đã lưu trên máy tính mà không cần phải nhờ đến đĩa CD/DVD cứu hộ hoặc dùng đến ổ DVD của máy tính. Mời bạn đọc theo dõi.
Hướng dẫn thiết lập Boot từ tập tin ISO cho máy tính
Trước tiên, bạn hãy tiến hành tải về và cài đặt phần mềm EasyBCD cho máy tính để chỉnh sửa thiết lập Boot cho Windows.
Hướng dẫn thiết lập Boot từ tập tin ISO cho máy tính
Khởi động EasyBCD. Nhấn chọn "Add New Entry".
Hướng dẫn thiết lập Boot từ tập tin ISO cho máy tính
Tại dòng "Portable/External Media", bạn nhấn chọn tab "ISO".
Hướng dẫn thiết lập Boot từ tập tin ISO cho máy tính
Bây giờ hãy nhập tên cho tùy chọn Boot từ ISO, ở đây lấy "Revo Backup" làm ví dụ. Sau đó ở dòng "Path"bạn hãy tìm đến đường dẫn của tập tin ISO đĩa cứu hộ được lưu trên máy tính. Dòng "Mode" thì bạn hãy chọn"Run from Disk". Sau đó nhấn "Add Entry" để lưu lại.
Hướng dẫn thiết lập Boot từ tập tin ISO cho máy tính
EasyBCD sẽ hiển thị thông báo thêm thành công tùy chọn Boot từ ISO cho Windows.
Hướng dẫn thiết lập Boot từ tập tin ISO cho máy tính
Bây giờ hãy khởi động lại máy tính và kiểm tra kết quả nhé. Bạn có thể sử dụng phương pháp này để tạo thêm nhiều tùy chọn Boot từ các tập tin ISO khác cho Windows.

Hiển thị Google Maps - Xcode - IPhone

Google Maps (thời gian trước còn gọi là Google Local) là một dịch vụ ứng dụng và công nghệ bản đồ trực tuyến trên web miễn phí được cung cấp bởi Google và hỗ trợ nhiều dịch vụ dựa vào bản đồ như Google Ride Finder và một số có thể dùng để nhúng vào các trang web của bên thứ ba thông qua Google Maps API. Nó cho phép thấy bản đồ giao thông, đường đi cho xe đạp, cho người đi bộ (những đường đi ngắn hơn 6.2 dặm) và xe hơi, và những địa điểm kinh doanh trong khu vực cũng như khắp nơi trên thế giới.


Một sản phẩm liên quan, đó là Google Earth, một ứng dụng độc lập dành cho Microsoft Windows, Mac OS X và Linux cho phép xem các tính năng mở rộng khác.

Với sự linh động của thiết bị di động ta có thể mang nó đi một cách dễ dàng, sử dụng vào nhiều mục đích, việc tích hợp Google Maps trên thiết bị di động là rất cần thiết. Và đặc biệt khi kết nối GPS thì ta có thể biết vị trí của mình đang đứng ngay trên bản đồ Google Maps giúp dẫn đường và tìm kiếm những vị trí một cách dễ dàng.

ISO đã xây dựng sẵn một framework giúp cho ứng dụng xây dựng kết nối tới Google Maps một cách dễ dàng. Sau đây sẽ hướng dẫn các bạn tạo ứng dụng trên iPhone kết nối tới Google Maps.
Trong Xcode tạo Project mới với tên dự án là “Mapgoogle”. Sau khi tao Project xong ta Add framework hỗ trợ kết nối tới Google Maps framework đó là “Mapkit.framework”. Bằng cách tích vào dấu “+” trong Build Phases để Add framework này vào.
Hình 1 Tích vào dấu “+” trong Build Phases để Add framework Map
Ø Sau khi bấm vào dấu “+” thì hộp thoại chọn Add framework hiển thị ra ta hãy tìm chọn đến “MapKit.framework”
Hình 2 Chọn Add “MapKit.framework”

Chọn file “MapgoogleViewController.h” chỉnh sửa dòng lệnh như sau:
#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
@interface MapgoogleViewController: UIViewController
<MKMapViewDelegate> {
    MKMapView *mapview;
UIView *view;
}
//Tạo hàm loadView để gọi ứng dụng GoogleMaps
-(void)loadView;
@end
Chọn file “MapgoogleViewController.m” để viết nội dung cho lớp. Ta viết nội dung cho hàm loadView.
- (void)loadView {
      view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 460)];
      view.backgroundColor = [UIColor whiteColor];
      mapview = [[MKMapView alloc] initWithFrame:CGRectMake(0, 44, 320, 416)];
      mapview.delegate = self;
      [view addSubview:mapview];     
      ////Add Toolbar
      UIImageView *imageViewButtonBar = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"buttonbar_grey.png"]];
      imageViewButtonBar.frame = CGRectMake(0, 0, 320, 44);
      [view addSubview:imageViewButtonBar];
      ////Add ButtonBack
    UIButton *buttonBack = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    buttonBack.frame = CGRectMake(5, 4, 100, 35);
    //Thiết lập Tên của button
    [buttonBack setTitle:@"Start" forState:UIControlStateNormal];
    [view addSubview:buttonBack];
      [self setView:view];
}

Vậy là ta đã hoàn thành viết code cho ứng dụng, công việc cuối cùng và cũng quan trọng là liên kết giao diện hiển thị với code.Cần  Liên kết View tới code. Chọn mở file “MapgoogleViewController.xib”, Bấm chuột lên bất kỳ chỗ nào trong view. Bấm Connections Inspector. Bên cạnh chữ New Referencing Outlet bạn sẽ thấy một vòng tròn. Bấm vào vòng tròn đó và kéo đường kẻ màu xanh tới File’s Owner và thả ra. Chữ view sẽ tự động hiện ra. Bấm chuột lên nó. Vậy là bạn đã liên kết cửa sổ view tới đối tượng proxy của bạn. Bây giờ bạn sẽ thấy hình sau:
Hình 3 Liên kết View trong Map

Bây giờ ta có thể bấm Build and Go để chạy chương trình, và cảm nhận nó. Giao diện sẽ như sau:
Hình 4 Giao diện khi chạy ứng dụng kết nối Google Maps

Kết nối SQLite với Xcode - Xcode - IPhone

Cũng như các ứng dụng khác ta có nhu cầu lưu trữ dữ liệu  và lấy dữ liệu trong một cơ sở dữ liệu. Đối với các ứng dụng của iPhone, chúng ta có thể sử dụng cơ sở dư liệu là SQLite hoàn toàn miễn phí. SQLite là một thư viện phần mềm khép kín, không cần kết nối với Server, không cần cấu hình, truy vấn dữ liệu bằng SQL. Lưu ý: các ứng dụng iPhone không thể làm việc với cơ sở dữ liệu từ xa. Sau đây sẽ giới thiệu cách sử dụng SQLite vào trong ứng dụng iPhone.


Để sử dụng SQLite trong ứng dụng của bạn, bạn không phải cài đặt bất kỳ phần mềm mới trên OSx68 của bạn. Bạn có thể sử dụng dòng lệnh để tạo ra một cơ sở dữ liệu SQL hoặc sử dụng được thêm vào trình duyệt web Firefox (bạn có thể vào địa chỉ web sau để thêm SQLite Manager: https://addons.mozilla.org/en-US/firefox/addon/sqlite-manager/), đây là phần mềm quản lý tạo cơ sở dữ liệu trong bài này sẽ dùng.
Hình 1 SQLite Manager quản lý tạo cơ sở dữ liệu SQLite

Tiếp theo là tạo cơ sở dữ liệu SQLite với tên là “AnimalDatabase.sqlite”. Tạo bảng dữ liệu với tên là “animals”. Cấu trúc các trường của bảng này được thể hiện ở hình 4-26. Sau khi tạo bảng xong ta nhập dữ liệu vào bảng.
Hình 2 Nhập dữ liệu vào bảng “animals”

Bây giờ ta tạo Project để kết nối với SQLite. Tương tự như tạo Project ở các bài trên ta tạo ứng dụng kiểu “Navigation-Based Application”. Đặt tên là “SQLiteTutorial”.

Sau khi tạo Project xong ta thêm các file và các lớp cần thiết cho dự án như sau:
Ø Bây giờ chúng ta cần tạo hai lớp, lớp đầu tiên sẽ thể hiện cho “animal”, chọn File >New File, hộp thoại New File hiển thị ra ta chọn “Cocoa Touch” ở dưới iPhone OS, chọn Objective-C class. Chọn kiểu lớp là “NSObject”. Lưu lại với tên là “Animal” và chọn Add vào Project. Lớp thứ hai cũng được tạo tương tự nhưng khác là kiểu lớp là “UIViewController” đặt tên lại là “AnimalsViewController”, và Add vào Project.
Ø Chúng ta cần tạo một file kiểu View để hiển thị chi tiết thông tin khi ta tích chọn vào một Cell trong TableView. Vào File>New File, hộp thoại hiển thị ra ta chọn User Interface dưới iPhone OS. Chọn kiểu tạo là View XIB. Đặt tên như sau: “AnimalViewController.xib”. Add vào Project.
Hình 3 Chọn tạo file View XIB với tên “AnimalViewController.xib.

Để có thể lien kết được Project với SQLite thì ta cần Add frameworks hỗ trợ kết nối với SQLite đã được IOS xây dựng sẵn. Bằng cách tích vào dấu “+” trong Build Phases để Add framework này vào.
Hình 4 Tích vào dấu “+” trong Build Phases để Add framework SQLite
Ø Sau khi bấm vào dấu “+” thì hộp thoại chọn Add framework hiển thị ra ta hãy tìm chọn đến “libsqlite3.0.dylib”
Hình 5 Chọn Add “libsqlite3.0.dylib”

Tiếp theo ta cần Add file AnimalDatabase.sqlite mà ta đã tạo ở trên vào Project.
Sau khi hoàn thành tất cả các bước trên ta chuyển sang viết code cho Project.
Ø Chọn làm việc với file “Animal.h”, ta thêm dòng lệnh sau:
#import <UIKit/UIKit.h>
@interface Animal : NSObject {
      NSString *name;
      NSString *description;
      NSString *imageURL;
}
@property (nonatomic, retain) NSString *name;
@property (nonatomic, retain) NSString *description;
@property (nonatomic, retain) NSString *imageURL;
-(id)initWithName:(NSString *)n description:(NSString *)d url:(NSString *)u;
@end
Ø Chuyển sang file “Animal.m” để viết nội dung của lớp với phần code như sau:
#import "Animal.h"
@implementation Animal
@synthesize name, description, imageURL;
-(id)initWithName:(NSString *)n description:(NSString *)d url:(NSString *)u {
      self.name = n;
      self.description = d;
      self.imageURL = u;
      return self;
}
@end
Ø Bây giờ ta thiết lập để truy cập vào cơ sở dữ liệu. Chọn file làm việc tiếp là “SQLiteTutorialAppDelegate.h” và thay đổi nội dung như sau:
#import <UIKit/UIKit.h>
#import <sqlite3.h>
@interface SQLiteTutorialAppDelegate : NSObject  {
    UIWindow *window;
    UINavigationController *navigationController;
    NSString *databaseName;
    NSString *databasePath;
    NSMutableArray *animals;
}
@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet UINavigationController *navigationController;
@property (nonatomic, retain) NSMutableArray *animals;
@end
Ø Ở trên khai báo các thư viện và biến để kết nối với cơ sở dữ liệu SQLite. Bây giờ chọn file "SQLiteTutorialAppDelegate.m" và thay đổi nội dung như sau:
#import "SQLiteTutorialAppDelegate.h"
#import "RootViewController.h"
#import "Animal.h"
@implementation SQLiteTutorialAppDelegate
@synthesize window;
@synthesize navigationController;
@synthesize animals; // Synthesize the aminals array
- (void)applicationDidFinishLaunching:(UIApplication *)application {
      databaseName = @"AnimalDatabase.sql";
      NSArray *documentPaths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES);
      NSString *documentsDir = [documentPaths objectAtIndex:0];
      databasePath = [documentsDir stringByAppendingPathComponent :databaseName];
      [self checkAndCreateDatabase];
      [self readAnimalsFromDatabase];
      [window addSubview:[navigationController view]];
      [window makeKeyAndVisible];
}
- (void)dealloc {
      [animals release];
      [navigationController release];
      [window release];
      [super dealloc];
}
-(void) checkAndCreateDatabase{
      BOOL success;
      NSFileManager *fileManager = [NSFileManager defaultManager];
      success = [fileManager fileExistsAtPath:databasePath];
      if(success) return;
      NSString *databasePathFromApp = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:databaseName];
      [fileManager copyItemAtPath:databasePathFromApp toPath:databasePath error:nil];
      [fileManager release];
}

-(void) readAnimalsFromDatabase {
      sqlite3 *database;
      animals = [[NSMutableArray alloc] init];
      if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK) {
                  const char *sqlStatement = "select * from animals";
                  sqlite3_stmt *compiledStatement;
                  if(sqlite3_prepare_v2(database, sqlStatement, -1,
&compiledStatement, NULL) == SQLITE_OK) {
                              while(sqlite3_step(compiledStatement) == SQLITE_ROW) {
                                          NSString *aName = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 1)];
                                          NSString *aDescription = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 2)];
                                          NSString *aImageUrl = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 3)];
                                          Animal *animal = [[Animal alloc] initWithName:aName description:aDescription url:aImageUrl];
                                          [animals addObject:animal];
                                          [animal release];
                              }
                  }
                  sqlite3_finalize(compiledStatement);
      }
      sqlite3_close(database);
}
@end
Ø Hàm checkAndCreateDatabase kiểm tra xem cơ sở dữ liệu đã được thêm vào trong thiết bị hay chưa, nếu cơ sở dữ liệu chưa được tạo ra hay nó đã được gỡ bỏ vì lý do nó sẽ được cài đặt lại từ cơ sở dữ liệu mặc định. Tiếp theo hàm readAnimalsFromDatabase để kết nối đến cơ sở dữ liệu được lưu trữ trong thư mục users documents, và sau đó thực thi câu lệnh SQL: "SELECT * FROM animals". Truy vấn để lấy dữ liệu trong bảng dữ liệu “animals”, đưa các dữ liệu này vào mảng lưu trữ animals được khai báo ở lớp “.h”.
Ø Bây giờ ta hiển thị mảng dữ liệu lên TableView. Chọn file "RootViewController.m" và chỉnh sửa numberOfRowsInSection như sau:
SQLiteTutorialAppDelegate *appDelegate = (SQLiteTutorialAppDelegate *)[[UIApplication sharedApplication] delegate];
return appDelegate.animals.count;
Ø Tiếp theo ta thay đổi hàm cellForRowAtIndexPath như sau:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
    }
    SQLiteTutorialAppDelegate *appDelegate = (SQLiteTutorialAppDelegate *)[[UIApplication sharedApplication] delegate];
      Animal *animal = (Animal *)[appDelegate.animals objectAtIndex:indexPath.row];
      [cell setText:animal.name];
      return cell;   }
Ø Tiếp theo ta cần thiết lập AnimalViewController để khi bấm chọn vào một cell trên TableView thì sẽ chuyển sang View chi tiết hơn. Chọn file “AnimalViewController.h” sửa nội dung như sau:
#import <UIKit/UIKit.h>
@interface AnimalViewController : UIViewController {
      IBOutlet UITextView *animalDesciption;
      IBOutlet UIImageView *animalImage;  }
@property (nonatomic, retain) IBOutlet UITextView *animalDesciption;
@property (nonatomic, retain) IBOutlet UIImageView *animalImage;
@end
Ø Chọn file “AnimalViewController.m” để khởi tạo biến cho lớp vừa khai báo ở trên.
#import "AnimalViewController.h"
@implementation AnimalViewController
@synthesize animalDesciption, animalImage;
Ø Bây giờ ta cần làm xuất hiện trang chi tiết khi bạn chọn một cell trên TableView. Chọn file "AnimalViewController.xib", thiết lập thuộc tính class của File’s Owner  ClassAnimalViewController, bằng cách chọn lên biểu tượng “File’s Owner” bấm vào Identity Inspector tương tự như Attributes giới thiệu ở bài trên, và chọn AnimalViewController từ class thả xuống.
Hình 6 Chọn thuộc tính class của File’s Owner là AnimalViewController
Ø Tiếp theo ta thiết kế View hiển thị gồm: 1 UITextView, 1 UIImageView để hiển thị thông tin chi tiết. Ta kéo thả các UI cần thiết vào View.
Hình 7 Thiết kế giao diện hiển thị chi tiết
Ø Sau khi thiết kế giao diện như ở trên thì ta tiến hành kéo thả các UI tới File’s Owner để trình điều khiển hiển thị View. Từ “File’s Owner” kéo thả tới đối tượng View. Từ “File’s Owner” kéo thả đến UITextView và chọn “animalDescription” trong danh sách hiển thị ra. Thực hiện với UIImageView và chọn “animalImage” trong danh sách hiển thị ra.
Ø Chọn file “RootViewController.h” để chỉnh sửa nội dung như sau:

#import <UIKit/UIKit.h>
#import "AnimalViewController.h"
@interface RootViewController : UITableViewController {
      AnimalViewController *animalView;
}
@property(nonatomic, retain) AnimalViewController *animalView;
@end

Ø Chọn file “RootViewController.m” để khởi tạo biến và lấy giá trị mảng lưu trữ từ cơ sở dữ liệu
#import "RootViewController.h"
#import "SQLiteTutorialAppDelegate.h"
#import "Animal.h"
@implementation RootViewController
@synthesize animalView;
Ø Tiếp theo ta cần bỏ ghi chú ở trước hàm viewDidLoad để thiết lập tiêu đề cho View chi tiết. Và sửa như sau:
- (void)viewDidLoad {
    [super viewDidLoad];
self.title = @"My Zoo";
}
Ø Cuối cùng ta cần chỉnh sửa ở didSelectRowAtIndexPath” để khi ta bấm vào cell trên TableView thì sẽ chuyển sang View chi tiết.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath :(NSIndexPath *)indexPath {
      SQLiteTutorialAppDelegate *appDelegate = (SQLiteTutorialAppDelegate *)[[UIApplication sharedApplication] delegate];
      Animal *animal = (Animal *)[appDelegate.animals objectAtIndex :indexPath.row];
      if(self.animalView == nil) {
                  AnimalViewController *viewController = [[AnimalViewController
alloc] initWithNibName:@"AnimalViewController" bundle:nil];
                  self.animalView = viewController;
                  [viewController release];
      }
      [self.navigationController pushViewController:self.animalView animated :YES];
      self.animalView.title = [animal name];
      [self.animalView.animalDesciption setText:[animal description]];
      NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:[animal imageURL]]];
      UIImage *animalImage = [[UIImage alloc] initWithData:imageData cache :YES];
      self.animalView.animalImage.image = animalImage;
}
Project đã được hoàn thành và bạn có thể bấm Build and Go để chạy chương trình, và cảm nhận nó. Giao diện sẽ như sau:
Hình 8 Kết quả khi chạy Project kết nối SQLite

Thu tín hiệu GPS - Xcode - IPhone

CoreLocation là một framework của IOS  là rất mạnh mẽ, cho phép truy cập GPS của iPhone một cách dễ dàng. Ta có thể truy cập vào tọa độ hoặc tam giác di động, la bàn, hướng và tốc độ di chuyển của các thiết bị qua GPS. Sau đây sẽ là hướng dẫn các bạn tạo ra một Project kết nối tới GPS của iPhone.


Hình 1 Kết quả cần đạt được khi thu GPS của iPhone

Đầu tiên ta phải tạo một Project mới. Trong Xcode, File-> New Project. Hộp thoại hiển thị ra ta cần lưu ý chọn Application  của iPhone OS. Chọn tạo kiểu ứng dụng là View-based Application và lưu lại với tên CoreLocationDemo hoặc một tên bất kỳ bạn muốn.
Hình 2 Tạo Project “CoreLocationDemo” kiểu View-based Application

Sau khi tạo xong Project ta Add framework “CoreLocation.framework” vào trong dự án của chúng ta. Bằng cách tích vào dấu “+” trong Build Phases để Add framework này vào.
Hình 3 Tích vào dấu “+” trong Build Phases để Add framework
Ø Sau khi bấm vào dấu “+” thì hộp thoại chọn Add framework hiển thị ra ta hãy tìm chọn đến “CoreLocation.framework”
Hình 4 Chọn Add “CoreLocation.framework”

Tiếp theo ta cần tạo một lớp để quản lý truy cập “CoreLocation”. Lớp này sẽ nhận thông điệp từ framework  “CoreLocation” là chứa dữ liệu thô và sau đó sẽ chuyển thông tin này đến lớp ViewController(điều khiển hiển thị).
Ø Tạo một file mới (File-> New File) và tạo ra một lớp Objective-C mới (iPhone OS-> Cocoa Touch Class-> Objective-C) và nhớ chọn subclass là  “NSObject”. Đặt tên file này là “CoreLocationController” và chọn Add vào Project “CoreLocationDemo” của ta.
Hình 5 Tạo file mới “CoreLocationController”

Sau khi tạo file mới xong ta viết mã lệnh vào file này.
Ø Trong file CoreLocationController.h thêm mã lệnh sau vào:
#import <CoreLocation/CoreLocation.h> //Chèn thư viện CoreLocation
Ø Khai báo giao thức để cho các lớp khác có thể truy cập vào lấy thông tin lớp này thông qua giao thức ta tạo ra. Vẫn ở trong lớp CoreLocationController.h thêm lệnh sau:
@protocol CoreLocationControllerDelegate
@required
- (void)locationUpdate:(CLLocation *)location;
- (void)locationError:(NSError *)error;
@end
Ø Bây giờ chúng ta cần tạo cho lớp vừa mới thêm vào tuân thủ theo giao thức “CLLocationManagerDelegate”. Trong file CoreLocationController.h:
@interface CoreLocationController : NSObject <CLLocationManager Delegate> {
      CLLocationManager *locMgr;
      id delegate;
}
@property (nonatomic, retain) CLLocationManager *locMgr;
@property (nonatomic, assign) id delegate;
@end
Ø Sau khi khai báo lớp xong ở file “.h” ta chuyển sang phần nội dung của lớp ở file. Trong file CoreLocationController.m:
@synthesize locMgr, delegate; //Khởi tạo 2 biến đã được khai báo từ trước
Ø Bây giờ ta cần bắt đầu với phương thức “init”để được quyền truy cập vào giao thức CLLocationManager.(Bạn có thể tìm hiểu thêm ý phương thức “init” trong Objective-C). Trong file CoreLocationController.m:
- (id)init {
      self = [super init];
      if(self != nil) {
                  self.locMgr = [[[CLLocationManager alloc] init] autorelease];
                  self.locMgr.delegate = self;
      }
      return self;
}
Ø Tiếp theo là hai phương thức gọi lại từ giao thức CLLocationManagerDelegate. Chúng sẽ cập nhật dữ liệu tọa độ GPS, tốc độ…của “CoreLocation”, cũng như xử lý các thông báo lỗi. Cuối cùng, chúng ta cần xóa bỏ locMgr khi không cần dùng đến nữa. Trong file CoreLocationController.m:
- (void)locationManager:(CLLocationManager *)manager
 didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
if([self.delegate conformsToProtocol:@protocol
(CoreLocationControllerDelegate)]) {
                  [self.delegate locationUpdate:newLocation];
      }
}
- (void)locationManager:(CLLocationManager *)manager didFailWithError: (NSError *)error {
      if([self.delegate conformsToProtocol:@protocol
(CoreLocationControllerDelegate)]) {
                  [self.delegate locationError:error];
      }
}
- (void)dealloc {
      [self.locMgr release];
      [super dealloc];
}

Ta đã tạo ra lớp thu nhận tín hiệu GPS của iPhone, giờ chúng ta cần lien kết file này tới các file View Controller để hiển thị thông tin GPS thu nhận được, ta cần tuân thủ theo giao thức mà đã tạo ra ở lớp trên. Mở file “CoreLocationDemoViewController.h” trong file này ta định nghĩa 4 IBOutlet UILabel để hiện thị thông tin thu nhận GPS.
#import <UIKit/UIKit.h>
#import "CoreLocationController.h"
@interface CoreLocationDemoViewController : UIViewController <CoreLocationControllerDelegate> {
      CoreLocationController *CLController;
      IBOutlet UILabel *speedLabel;
      IBOutlet UILabel *latitudeLabel;
      IBOutlet UILabel *longitudeLabel;
      IBOutlet UILabel *altitudeLabel;
}
@property (nonatomic, retain) CoreLocationController *CLController;
@end

Chọn file CoreLocationDemoViewController.xib và kéo thả từ Object Library 4 IBOutlets UILabel. Ta thiết kế như hình đầu Project giới thiệu. Sau đó liên kết 4 Label này với mã lệnh(làm tương tự như phần 4.3). Tiếp theo ta chọn file “CoreLocationDemoViewController.m” viết code sau vào:
@synthesize CLController;
- (void)viewDidLoad {
[super viewDidLoad];
      CLController = [[CoreLocationController alloc] init];
      CLController.delegate = self;
      [CLController.locMgr startUpdatingLocation];
}
- (void)locationUpdate:(CLLocation *)location {
      speedLabel.text = [NSString stringWithFormat:@"SPEED: %f", [location speed]];
      latitudeLabel.text = [NSString stringWithFormat:@"LATITUDE: %f", location.coordinate.latitude];
      longitudeLabel.text = [NSString stringWithFormat:@"LONGITUDE: %f", location.coordinate.longitude];
      altitudeLabel.text = [NSString stringWithFormat:@"ALTITUDE: %f", [location altitude]];
}
- (void)locationError:(NSError *)error {
      speedLabel.text = [error description];
}
- (void)dealloc {
      [CLController release];
    [super dealloc];
}


Vậy là ta đã hoàn thành việc kết nói GPS iPhone và hiển thị các thông tin này lên giao diện của chương trình. Bạn có thể bấm Build and Go để chạy chương trình. (Các bạn lưu ý muốn có kết quả hiển thị thì ta phải sử dụng kết nối thiết bị iPhone)