iOS 6 By Tutorials: Ray Wenderlich's Best-Selling Book for iOS Developers
iOS 6 By Tutorials: A Comprehensive Guide for iOS Developers
If you are an iOS developer who wants to learn the latest and greatest features of iOS 6, then you should check out iOS 6 By Tutorials, a book written by Ray Wenderlich and his team of talented authors. This book covers everything you need to know about iOS 6, from Auto Layout to Passbook, from Collection Views to Social Framework, and more. In this article, we will give you a brief overview of what you can expect from this book and why you should read it.
Ios 6 By Tutorials Ray Wenderlich.torrentl
What's New in iOS 6?
iOS 6 is the sixth major release of Apple's mobile operating system, which powers devices such as iPhone, iPad, and iPod touch. It was released in September 2012, along with the iPhone 5, and introduced over 200 new features and improvements. Some of the most notable ones are:
Maps: Apple replaced Google Maps with its own mapping service, which features turn-by-turn navigation, 3D Flyover, Siri integration, and more.
Siri: Apple's voice assistant got smarter and more capable, with support for more languages, sports, movies, restaurants, Facebook, Twitter, and more.
Facebook: Apple integrated Facebook into iOS 6, allowing users to sign in once and share content from various apps, such as Photos, Safari, Maps, etc.
Shared Photo Streams: Apple added a new feature that lets users share photos with their friends and family via iCloud.
Passbook: Apple introduced a new app that stores digital passes for things like boarding passes, movie tickets, coupons, loyalty cards, etc.
FaceTime: Apple enabled FaceTime over cellular networks, allowing users to make video calls without Wi-Fi.
Do Not Disturb: Apple added a new feature that lets users silence incoming calls and notifications during a specified time period.
These are just some of the highlights of iOS 6. There are many more features and enhancements that you can discover by reading iOS 6 By Tutorials. The book covers 16 chapters, each focusing on a different aspect of iOS 6. In each chapter, you will learn by doing, following clear and detailed instructions, code snippets, screenshots, and diagrams. You will also get access to the full source code of the projects, which you can download and run on your own devices. The book assumes that you have some basic knowledge of Objective-C and iOS development, but it does not require any prior experience with iOS 6. You can start from the beginning or jump to any chapter that interests you.
Auto Layout
One of the most important and powerful features of iOS 6 is Auto Layout. Auto Layout is a system that lets you define how your user interface elements should be arranged and resized according to various factors, such as device orientation, screen size, localization, etc. Auto Layout makes your app more adaptable and responsive, without requiring you to write a lot of code or use multiple nib files.
What is Auto Layout?
Auto Layout is based on the concept of constraints. Constraints are rules that describe the relationships between views in your user interface. For example, you can specify that a button should always be centered horizontally, or that a label should always have a fixed width, or that an image view should always have the same height as another image view. Constraints can also involve constants and multipliers, which allow you to fine-tune the position and size of your views. By using constraints, you can define a layout that works for any screen size and orientation, without having to manually adjust frames or use springs and struts.
How to Use Auto Layout in Interface Builder
The easiest way to use Auto Layout is to use Interface Builder, which is part of Xcode. Interface Builder lets you create and edit constraints visually, using a graphical editor and various tools. To use Auto Layout in Interface Builder, you need to do the following steps:
Create a new project in Xcode and choose a template that supports Auto Layout, such as Single View Application.
Open the main storyboard file and select the view controller scene.
In the Attributes inspector, make sure that the Use Auto Layout checkbox is checked.
Add some views to your view controller's view and arrange them as you like.
Select one or more views and click on the Pin button at the bottom right corner of the editor. This will open a menu that lets you add constraints related to the size and position of the selected views.
Select one or more constraints from the menu and click on the Add Constraints button. This will create the constraints and show them as blue lines in the editor.
Repeat steps 5 and 6 until you have added enough constraints to fully define your layout. You can also edit or delete existing constraints by selecting them and using the Attributes inspector or the Size inspector.
Click on the Resolve Auto Layout Issues button at the bottom right corner of the editor. This will open a menu that lets you fix any errors or warnings related to your constraints.
Select one or more options from the menu and click on the Apply button. This will update your layout according to your constraints.
Run your app on different devices or simulators and see how your layout adapts automatically.
How to Use Auto Layout Programmatically
If you prefer to use code instead of Interface Builder, you can also use Auto Layout programmatically. To use Auto Layout programmatically, you need to do the following steps:
Create a new project in Xcode and choose a template that supports Auto Layout, such as Single View Application.
Open the main storyboard file and select the view controller scene.
In the Attributes inspector, make sure that the Use Auto Layout checkbox is checked.
Add some views to your view controller's view programmatically in your viewDidLoad method. For example:
```objective-c - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. // Create a red view UIView *redView = [[UIView alloc] init]; redView.backgroundColor = [UIColor redColor]; redView.translatesAutoresizingMaskIntoConstraints = NO; // Disable autoresizing mask [self.view addSubview:redView]; // Add it as a subview // Create a blue view protocol to provide the number of items and the content of each item. For example:
```objective-c // Return the number of items in the collection view - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section return [self.data count]; // Return the cell for each item in the collection view - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath // Dequeue a reusable cell with the reuse identifier UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath]; // Get the data for the current item NSString *text = [self.data objectAtIndex:indexPath.item]; // Create a label to display the text UILabel *label = [[UILabel alloc] initWithFrame:cell.bounds]; label.text = text; label.textAlignment = NSTextAlignmentCenter; // Add the label as a subview of the cell [cell addSubview:label]; // Return the cell return cell; ``` Create a new file in Xcode and choose a template that supports Collection View Layouts, such as Collection View Layout.
In the Identity inspector, give your layout class a name. For example, "CustomLayout".
In your layout class's header file, import UIKit framework and declare any properties or methods you need for your layout. For example:
```objective-c #import
@interface CustomLayout : UICollectionViewLayout @property (nonatomic) CGFloat itemSize; // The size of each item @property (nonatomic) CGFloat spacing; // The spacing between items @property (nonatomic) CGFloat insets; // The insets around the collection view @end ``` In your layout class's implementation file, implement the required methods of UICollectionViewLayout protocol to provide the layout attributes for each item and supplementary view. For example:
```objective-c #import "CustomLayout.h" @implementation CustomLayout // Prepare the layout before displaying it - (void)prepareLayout [super prepareLayout]; // Set some default values for the properties self.itemSize = 100; self.spacing = 10; self.insets = 20; // Return the size of the collection view content - (CGSize)collectionViewContentSize // Get the number of items in the collection view NSInteger itemCount = [self.collectionView numberOfItemsInSection:0]; // Calculate the number of columns and rows based on the item size, spacing, and insets NSInteger numberOfColumns = floor((self.collectionView.bounds.size.width - self.insets * 2 + self.spacing) / (self.itemSize + self.spacing)); NSInteger numberOfRows = ceil(itemCount / (CGFloat)numberOfColumns); // Calculate the width and height of the content based on the number of columns and rows, item size, spacing, and insets CGFloat contentWidth = self.insets * 2 + (numberOfColumns - 1) * self.spacing + numberOfColumns * self.itemSize; CGFloat contentHeight = self.insets * 2 + (numberOfRows - 1) * self.spacing + numberOfRows * self.itemSize; // Return the content size as a CGSize object return CGSizeMake(contentWidth, contentHeight); // Return an array of layout attributes for each item in the collection view - (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect // Create an empty array to store the layout attributes NSMutableArray *attributesArray = [NSMutableArray array]; // Get the number of items in the collection view NSInteger itemCount = [self.collectionView numberOfItemsInSection:0]; // Loop through each item and create a layout attribute object for it for (NSInteger i = 0; i In your master view controller's implementation file, import your layout class's header file and create an instance of your layout class in your viewDidLoad method. For example:
```objective-c #import "MasterViewController.h" #import "CustomLayout.h" @interface MasterViewController () @end @implementation MasterViewController - (void)viewDidLoad [super viewDidLoad]; // Initialize data array with some dummy values self.data = @[@"One", @"Two", @"Three", @"Four", @"Five", @"Six", @"Seven", @"Eight", @"Nine", @"Ten"]; // Create an instance of your custom layout class CustomLayout *layout = [[CustomLayout alloc] init]; // Set the layout of your collection view to your custom layout self.collectionView.collectionViewLayout = layout; ``` Run your app on different devices or simulators and see how your collection view displays your data in a custom layout.
As you can see, creating a custom collection view layout requires more code and logic than using a default layout. However, it also gives you more freedom and creativity to design your own layout. You can use either method or a combination of both, depending on your preference and needs.
Passbook
Another new and useful feature of iOS 6 is Passbook. Passbook is an app that stores digital passes for things like boarding passes, movie tickets, coupons, loyalty cards, etc. Passbook makes it easy and convenient for users to access their passes without having to open different apps or print paper copies. Passbook also updates the passes automatically when there are any changes, such as gate changes, expiration dates, etc.
What is Passbook?
Passbook is an app that comes pre-installed on iOS 6 devices. Users can add passes to Passbook by scanning a barcode, tapping a link, or using an app that supports Passbook. Passes are files that contain information about the pass type, content, appearance, and behavior. Passes can also include images, logos, barcodes, locations, dates, etc. Passes are signed with a certificate to ensure their authenticity and security.
How to Create a Pass for Passbook
To create a pass for Passbook, you need to do the following steps:
Create a new project in Xcode and choose a template that supports Passes, such as Pass Library.
In the Project navigator, select the pass.json file and edit it according to your pass specifications. For example:
```json "formatVersion" : 1, "passTypeIdentifier" : "pass.com.example.coupon", "serialNumber" : "123456", "teamIdentifier" : "ABCDEF", "organizationName" : "Example Inc.", "description" : "A coupon for 10% off", "logoText" : "Example", "foregroundColor" : "rgb(255, 255, 255)", "backgroundColor" : "rgb(0, 0, 0)", "barcode" : "message" : "1234567890", "format" : "PKBarcodeFormatPDF417", "messageEncoding" : "iso-8859-1" , "coupon" : "primaryFields" : [ "key" : "offer", "label" : "Offer", "value" : "10% off" ], "auxiliaryFields" : [ "key" : "expires", "label" : "Expires", " : "2023-06-06" ] ``` In the Project navigator, select the Images.xcassets file and add your own images for the pass icon, logo, strip, thumbnail, etc.
In the Project navigator, select the PassKit folder and open the PassKit.h file. In this file, you can define some constants for your pass type identifier and team identifier. For example:
```objective-c #define kPassTypeIdentifier @"pass.com.example.coupon" #define kTeamIdentifier @"ABCDEF" ``` In the Project navigator, select the Pass.m file and edit the init method to initialize your pass object with your pass type identifier and serial number. For example:
```objective-c - (id)init self = [super init]; if (self) // Initialize pass object with pass type identifier and serial number self.passTypeIdentifier = kPassTypeIdentifier; self.serialNumber = @"123456"; return self; ``` In the Project navigator, select the ViewController.m file and import PassKit framework and Pass.h file. For example:
```objective-c #import
#import "Pass.h" ``` In the ViewController.m file, implement the viewDidLoad method to create an instance of your pass object and add it to a PKAddPassesViewController object. For example:
```objective-c - (void)viewDidLoad [super viewDidLoad]; // Create an instance of your pass object Pass *pass = [[Pass alloc] init]; // Create an error object to handle any errors NSError *error = nil; // Create a PKPass object from your pass object PKPass *pkPass = [[PKPass alloc] initWithData:[pass passData] error:&error]; // Check if there are any errors if (error != nil) // If yes, display an alert view with the error message UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Error" message:[error localizedDescription] delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil]; [alertView show]; else // If no, create a PKAddPassesViewController object with your PKPass object PKAddPassesViewController *addPassesViewController = [[PKAddPassesViewController alloc] initWithPass:pkPass]; // Set the delegate of the add passes view controller to self addPassesViewController.delegate = self; // Present the add passes view controller modally [self presentViewController:addPassesViewController animated:YES completion:nil]; ``` In the ViewController.m file, implement the pkAddPassesViewControllerDidFinish: method to dismiss the add passes view controller when the user taps Done or Cancel. For example:
```objective-c // Dismiss the add passes view controller when the user taps Done or Cancel - (void)pkAddPassesViewControllerDidFinish:(PKAddPassesViewController *)controller [self dismissViewControllerAnimated:YES completion:nil]; ``` Run your app on a device or simulator that supports Passbook and see how your pass is displayed and added to Passbook.
How to Update a Pass for Passbook
To update a pass for Passbook, you need to do the following steps:
Create a web service that can generate and deliver updated pass files to Passbook. The web service should follow the specifications described in Apple's documentation.
Register your web service URL in your pass.json file by adding a key-value pair for "webServiceURL". For example:
```json "formatVersion" : 1, "passTypeIdentifier" : "pass.com.example.coupon", "serialNumber" : "123456", "teamIdentifier" : "ABCDEF", "organizationName" : "Example Inc.", "description" : "A coupon for 10% off", "logoText" : "Example", "foregroundColor" : "rgb(255, 255, 255)", "backgroundColor" : "rgb(0, 0, 0)", "barcode" : "message" : "1234567890", "format" : "PKBarcodeFormatPDF417", "messageEncoding" : "iso-8859-1" , "coupon" : "primaryFields" : [ "key" : "offer", "label" : "Offer", "value" : "10% off" ], "auxiliaryFields" : [ "key" : "expires", "label" : "Expires", "value" : "2023-06-06