Vietnamese (Tiếng Việt) translation by Thùy Trang (you can also view the original English article)
This is the third part in the iOS Multitasking Se
Đây là phần ba của loạt bài về chế độ chạy đa nhiệm trong iOS. So với thông báo nội tuyến (Local Notification), âm thanh chạy nền, vị trí chạy nền, và VOIP (không được hướng dẫn trong loạt bài này), thì các tác vụ chạy nền và chuyển đổi ứng dụng nhanh là các chức năng dễ dàng nhất để thực hiện. Đối với hướng dẫn này, bạn sẽ cần phải hiểu về các khối (block).
Bước 1: Tạo dự án
Vì tính chất đơn giản của bài hướng dẫn này, hãy tạo ra một ứng dụng dạng cửa sổ giúp chúng ta không phải xử lý về View Controller và các vấn đề khác (thực hiện tương tự đối với tất cả các mẫu bạn sử dụng). Đặt tên nó là Background Task và bỏ chọn các mục "Use Core Data" and "Include Unit Tests".

Sau đó mở tệp Background_TasksAppDelegate.m.
Bước 2: Triển khai mã
Bây giờ kéo xuống applicationDidEnterBackground: method và thêm mã sau đây:
if ([[UIDevice currentDevice] respondsToSelector:@selector(isMultitaskingSupported)]) { //Check if our iOS version supports multitasking I.E iOS 4 if ([[UIDevice currentDevice] isMultitaskingSupported]) { //Check if device supports mulitasking UIApplication *application = [UIApplication sharedApplication]; //Get the shared application instance __block UIBackgroundTaskIdentifier background_task; //Create a task object background_task = [application beginBackgroundTaskWithExpirationHandler: ^ { [application endBackgroundTask: background_task]; //Tell the system that we are done with the tasks background_task = UIBackgroundTaskInvalid; //Set the task to be invalid //System will be shutting down the app at any point in time now }]; //Background tasks require you to use asyncrous tasks dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ //Perform your tasks that your application requires NSLog(@"\n\nRunning in the background!\n\n"); [application endBackgroundTask: background_task]; //End the task so the system knows that you are done with what you need to perform background_task = UIBackgroundTaskInvalid; //Invalidate the background_task }); } }
Kết luận
Như bạn có thể thấy, các tác vụ chạy nền rất ngắn và dễ dàng thực hiện. Nếu bạn cần bất kỳ sự trợ giúp nào hoặc chỉ muốn gửi một gợi ý, vui lòng bình luận vào phần dưới đây!
ries. Compared to Local Notifications, Background Audio, Background Location, and VOIP (Not covered in this series), Background Tasks and Fast App Switching are the easiest to implement. For this tutorial, you will need to understand blocks.Step 1: Creating the Project
For the simplicity of this tutorial, create a window based application so we do not have to deal with View Controllers and extras (same implementation is used no matter what template you use). Name it Background Task and uncheck "Use Core Data" and "Include Unit Tests".

Then open up the Background_TasksAppDelegate.m.
Step 2: Implementing the code
Now scroll down to the applicationDidEnterBackground: method and add the following code:
if ([[UIDevice currentDevice] respondsToSelector:@selector(isMultitaskingSupported)]) { //Check if our iOS version supports multitasking I.E iOS 4 if ([[UIDevice currentDevice] isMultitaskingSupported]) { //Check if device supports mulitasking UIApplication *application = [UIApplication sharedApplication]; //Get the shared application instance __block UIBackgroundTaskIdentifier background_task; //Create a task object background_task = [application beginBackgroundTaskWithExpirationHandler: ^ { [application endBackgroundTask: background_task]; //Tell the system that we are done with the tasks background_task = UIBackgroundTaskInvalid; //Set the task to be invalid //System will be shutting down the app at any point in time now }]; //Background tasks require you to use asyncrous tasks dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ //Perform your tasks that your application requires NSLog(@"\n\nRunning in the background!\n\n"); [application endBackgroundTask: background_task]; //End the task so the system knows that you are done with what you need to perform background_task = UIBackgroundTaskInvalid; //Invalidate the background_task }); } }
Wrap Up
As you can see, Background Tasks are extremely short and easy to implement. If you have any need for help or just want to post a suggesion, then comment below!
Envato Tuts+ tutorials are translated into other languages by our community members—you can be involved too!
Translate this post