How to publish ios app on apple store

how to publish application on apple store and how to publish a magazine in apple store and how to publish my app in apple store how to publish your app on apple store
Dr.KiranArora Profile Pic
Published Date:27-10-2017
Your Website URL(Optional)
Using iCloud and Security Services In this chapter, you will learn how to store your data on cloud, that is, on iCloud and security services for iOS, through which we can secure our data, passwords, and so on. In this chapter, we will cover the following topics: • Working with iCloud • Saving data using the keychain process • iOS Touch ID authentication Working with iCloud Basically, iCloud is a service that helps users synchronize their data across devices. The main purpose is to let users easily store their data, whether it's a file or document, so that they can access it on any of their iOS devices. While you can use other cloud services to save files or data, the core idea behind iCloud is to eliminate explicit or wired connection between devices. Apple does not want users to think of the cloud servers and the syncing. Everything simply works seamlessly. The same design philosophy also applies to developers. When we adopt iCloud, we do not need to know how to interact with the cloud server or upload data to iCloud. The iOS handles all the heavy lifting. Our focus is on the content such as managing the change of data or developing a connection between a cloud and a device. iCloud offers three kinds of storage: • Key-value storage: This is used to store content such as settings, preferences, and app states. iCloud and Security Services • Document storage: This is used to store file type content such as WordPress documents, drawings, and complex app states. • Core data storage: This is used for multi-device database solutions for structured content. iCloud core data storage is built on document storage and employs the same iCloud APIs. Let's understand how iCloud actually works. To use iCloud, we need an iOS developer account. Assuming that we have an iOS developer account, proceed with the following steps: 1. Go to 417a7776362562d2197f89480a8547b108fd934911bcbea0110d07f757&pat h=%2F%2Faccount%2Findex.action. We will first create the app ID with the iCloud feature available. 2. Sign in to the iOS Provisioning Portal, select the app IDs, and then create a new app ID. 3. Enable the iCloud service for your app by selecting the iCloud option. Select the Xcode that you are using (if you are using Xcode 5, then select Xcode 5) and click on Continue, as shown in the following screenshot: 4. Now, give the name of our project in the Name text field, write the bundle ID in the Bundle ID text field, and click on Continue, as shown in the following screenshot: 126 6 5. Now, it will show the enabled services for our app. Make sure iCloud is enabled or configurable, as shown in the following screenshot: 127 iCloud and Security Services 6. Then, go to Xcode and build a new single-view application. After that, go to Capabilities in Xcode and turn on iCloud; this will include iCloud entitlements, iCloud containers, and the link to the Cloud framework, as shown in the following screenshot: 7. Make sure that the entitlements are created automatically. The following screenshot shows the entitlements created: 128 6 8. Delete the ViewController class and existing view in the storyboard. 9. Now, we will empty the storyboard and create a new objective-C class, a subclass oftableViewController, and name it (for example, NoteListViewController). 10. Drag one table view controller to the storyboard and embed it in Navigation Controller. Now, give a name to this table view controller from the Attribute Inspector; it should be the same as the class name. Do the same thing one more time for a new class and name it (for example, AddNoteViewController). 129 iCloud and Security Services 11. Now, we have two table views in the storyboard. For NoteListViewController, drag a button to the top-right part of the navigation bar and set the identifier as add. This will automatically change the button to a + button, as shown in the following screenshot. Next, select the prototype cell and change its style to Basic. Navigation Controller and Note List View Controller 12. ForAddNoteViewController, drag the bar buttons into the navigation bar. Name one asCancel and the other asSave. 130 6 13. Then, select the table view and set the content to Static Cells. Finally, add one static row to the table view and add a text field, as shown here: Navigation Controller and Note List View Controller 14. AddNoteViewController will be shown when the user taps on the + button. So, press and hold the Ctrl key, click on the + button, and drag it to Add Note View Controller. Select Modal as the Segue action. Our storyboard should now look like this: The table view showing Add Note View Controller 131 iCloud and Security Services 15. Finally, link up the text field to AddNoteViewController and create two action methods:Cancel andSave. Our code inAddNoteViewController.h should like this: import UIKit/UIKit.h interface AddNoteViewController : UITableViewController - (IBAction)cancel:(id)sender; - (IBAction)save:(id)sender; property (weak, nonatomic) IBOutlet UITextField noteTextField; 16. We need to implement theNoteListViewController class so that we can display the notes available on the cloud in the table view. Open NoteListViewController.h and add this property to it: property (strong, nonatomic) NSMutableArray notes; 17. The purpose of the property is to save notes locally. We'll display the notes in the table view when needed. Next, we need to make a lazy instantiation of this property. Lazy instantiation is a good technique if we have an object that only needs to be configured once and has some configuration involved that we don't want to clutter in ourinit method. Add the following code in NoteListViewController.m: - (NSArray )notes if(_notes) _notes = NSMutableArray array; _notes = NSUbiquitousKeyValueStore defaultStore arrayForKey:"AVAILABLE_NOTES" mutableCopy; return _notes; 18. InviewDidLoad, we need to create the notification method as follows: - (void)viewDidLoad super viewDidLoad; self.navigationItem.leftBarButtonItem = self.editButtonItem; // Observer to catch changes from iCloud NSUbiquitousKeyValueStore store = NSUbiquitousKeyValueStore defaultStore; NSNotificationCenter defaultCenter addObserver:self selector:selector(storeDidChange:) name: NSUbiquitousKeyValueStore DidChangeExternallyNotification object:store; NSUbiquitousKeyValueStore defaultStore synchronize; // Observer to catch the local changes 132 6 NSNotificationCenter defaultCenter addObserver:self selector:selector(didAddNewNote:) name:"New Note" object:nil; iCloud data synchronization is achieved using the NSUbiquitousKeyValueStore class.NSUbiquitousKeyValueStore is a subclass ofNSObject, and it is available in iOS 5.0 and later. An NSNotificationCenter object provides a mechanism to broadcast information or messages within a program. It is also a subclass ofNSObject, and it is available in iOS 2.0 and later. OurviewDidLoad method will probably look like this: 19. Now, we have to implement methods that are executed when the preceding notifications are called. ThedidAddNewNote method will be invoked when users save a new note: pragma mark - Observer New Note - (void)didAddNewNote:(NSNotification )notification NSDictionary userInfo = notification userInfo; NSString noteStr = userInfo valueForKey:"Note"; self.notes addObject:noteStr; // Update data on the iCloud NSUbiquitousKeyValueStore defaultStore setArray:self.notes forKey:"AVAILABLE_NOTES"; // Reload the table view to show changes self.tableView reloadData; 133 iCloud and Security Services pragma mark - Observer - (void)storeDidChange:(NSNotification )notification // Retrieve the changes from iCloud _notes = NSUbiquitousKeyValueStore defaultStore arrayForKey:"AVAILABLE_NOTES" mutableCopy; // Reload the table view to show changes self.tableView reloadData; 20. We need to display the notes in table view. We have already retrieved the notes saved in iCloud; the rest of the implementation is to display the notes in the table view. InNoteListViewController.m, add the following code: - (NSInteger)numberOfSectionsInTableView:(UITableView )tableView return 1; - (NSInteger)tableView:(UITableView )tableView numberOfRowsInSection:(NSInteger)section return self.notes count; - (UITableViewCell )tableView:(UITableView )tableView cellForRow AtIndexPath:(NSIndexPath )indexPath static NSString CellIdentifier = "Cell"; UITableViewCell cell = tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath; NSString note = self.notes objectAtIndex:indexPath.row; cell.textLabel setText:note; return cell; 21. We have now come to the last part of the process: implementing AddNoteViewController to add notes to the cloud. Go to the implementation file of AddNoteViewController and add the following code: - (IBAction)cancel:(id)sender self dismissViewControllerAnimated:YES completion:nil; - (IBAction)save:(id)sender 134 6 // Notify the previous view to save the changes locally NSNotificationCenter defaultCenter postNotificationName:"New Note" object:self userInfo:NSDictionary dictionaryWithObject: self.noteTextField.text forKey:"Note"; self dismissViewControllerAnimated:YES completion:nil; 22. To test the note app, we have to compile and deploy it onto an actual device. If you are using a simulator that supports iOS 7.1 or later, then you will be able to use the simulator for testing. Make sure that you enable iCloud on both devices. Launch the app, add a note on one device, and you'll see the note appear on the other device. Keychain Services Keychain Services is a programming interface that enables developers to add, find, modify, and remove keychain items. In iOS and OS X, a keychain is an encrypted container that stores passwords and other private data that need to be secured. In iOS, each application has its own keychain to which it has access. This ensures that our data is secured by the third party and other users. Keychain provides a small space on which we can only store specific data such as passwords, account numbers, private numbers, and so on. With this article, I hope to convince you of the value of using the keychain in iOS and OS X instead of, for example, the application's user-defaults database, which stores its data in plain text without any form of security. Saving our data in keychain is better than the default database, because keychain is far more secure and robust. In iOS, an application can use the keychain through the Keychain Services API. This API provides a number of functionalities to manipulate the data stored in the application's keychain. The APIs are as follows: • SecItemAdd: This API is used to add data in keychain • SecItemCopyMatching: This API is used to find the existing data in keychain • SecItemDelete: This API is used to remove the data from the application's keychain • SecItemUpdate: This API is used to update the data in the application's keychain The Keychain Services API is a C-based API, but this doesn't prevent us from using it. Each of the preceding functions accepts a dictionary (CFDictionaryRef). 135 iCloud and Security Services Encryption and decryption Most of us know about two types of encryption: symmetric and asymmetric encryption. Symmetric encryption, on the one hand, uses one shared key to encrypt and decrypt data. Asymmetric encryption, on the other hand, uses one key to encrypt data and another separate, but related, key to decrypt data. In iOS, to encrypt and decrypt data, a Security framework is available. This process takes place under the hood, so we won't be directly interacting with this framework. We'll use symmetric encryption in our example application. The Security framework offers a number of other services, such as Randomize services to generate cryptographically-secure random numbers; Certificate, Key and Trust services to manage certificates; public and private keys; and trust policies. The Security framework is a low-level framework available in both iOS and OS X with C-based APIs. iOS keychain concepts and structure The keychain is a secure and encrypted way to store our precious data. It is important that your app, and all subsequent versions of it, are signed by the same mobile-provision profile. If they aren't, you will have many troubles later on. A keychain is a unit of sensitive data stored in your app. Keychain items are accompanied by one or more attributes. The attributes describe the keychain item, and which attributes we can use depends on the item class of the keychain item. The item class refers to the type of data we are going to store. This can be a username/password combination, a certic fi ate, a generic password, and so on. Understanding the application flow Before we start building the application, we need to know about the application flow, which is explained here: 1. When the user launches the application, it presents the user with a view to sign in. 2. If it hasn't created an account yet, its credentials are added to the keychain and signed in. If it has an account but enters an incorrect password, an error message is shown. 136 6 3. Once it has signed in, it has access to the data collected with the application. The data is securely stored by the application. 4. Whenever it takes data with the text field, this data is encrypted and stored in the application'sDocuments directory. 5. Whenever it switches to another application or the device gets locked, it automatically signs out. Let's start an activity on the Keychain. Just follow these steps to accomplish the activity: 1. Open Xcode and make a single-view application. Then, go to KeyChainSample Capabilities and turn on the Keychain Sharing option, as shown in the following screenshot: 137 iCloud and Security Services 2. After turning on the Keychain Sharing option, it will automatically create Entitlements File for us, as shown in the following screenshot: 3. Don't forget to add Security framework (shown in the following screenshot); without it, the keychain will not work: 138 6 4. Now, make a user interface in the storyboard like this: 5. Add a new Objective-C file to the project subclass of NSObject. Then, write the following code in the interface file of the new file. Also, import security.h on the top of the file: import security.h interface KeyChain : NSObject NSString service; NSString group; -(id) initWithService:(NSString ) service_ withGroup:(NSString)group_; -(BOOL) insert:(NSString )key : (NSData )data; -(NSData) find:(NSString)key; 139 iCloud and Security Services Our interface file will look like this: 6. Write the following code in the implementation file of the new file that we added: implementation KeyChain -(id) initWithService:(NSString ) service_ withGroup:(NSString)group_ self =super init; if(self) service = NSString stringWithString:service_; if(group_) group = NSString stringWithString:group_; return self; -(NSMutableDictionary) prepareDict:(NSString ) key NSMutableDictionary dict = NSMutableDictionary alloc init; dict setObject:(__bridge id)kSecClassGenericPassword forKey:(__bridge id)kSecClass; NSData encodedKey = key dataUsingEncoding:NSUTF8StringEncoding; 140 6 dict setObject:encodedKey forKey:(__bridge id)kSecAttrGeneric; dict setObject:encodedKey forKey:(__bridge id)kSecAttrAccount; dict setObject:service forKey:(__bridge id)kSecAttrService; dict setObject:(__bridge id) kSecAttrAccessibleAlwaysThisDeviceOnly forKey:(__bridge id)kSecAttrAccessible; if(group = nil) dict setObject:group forKey:(__bridge id)kSecAttrAccessGroup; return dict; -(BOOL) insert:(NSString )key : (NSData )data NSMutableDictionary dict =self prepareDict:key; dict setObject:data forKey:(__bridge id)kSecValueData; OSStatus status = SecItemAdd ((__bridge CFDictionaryRef)dict, NULL); if(errSecSuccess = status) NSLog("Unable add item with key =% error:%ld",key,status); return (errSecSuccess == status); -(NSData) find:(NSString)key NSMutableDictionary dict = self prepareDict:key; dict setObject:(__bridge id)kSecMatchLimitOne forKey:(__bridge id)kSecMatchLimit; dict setObject:(id)kCFBooleanTrue forKey:(__bridge id)kSecReturnData; CFTypeRef result = NULL; OSStatus status = SecItemCopyMatching ((__bridge CFDictionaryRef)dict,&result); if( status = errSecSuccess) NSLog("Unable to fetch item for key % with error:%ld",key,status); return nil; return (__bridge NSData )result; 141 iCloud and Security Services The_bridge object is used to transfer a pointer between Objective-C and the core foundation, with no transfer of ownership. Our interface file will probably look like this: 7. Connect the UI components to ourviewController.h file. Import KeyChain (the newly added Objective-C file) on top of the file and create an object of theKeyChain class in the interface as follows: 142 6 8. Add the code in theIBAction methods of our button with some more code as follows in theviewController.m file: define SERVICE_NAME "ANY_NAME_FOR_YOU" define GROUP_NAME "iOS" interface ViewController () end implementation ViewController - (void)viewDidLoad super viewDidLoad; keychain = KeyChain allocinitWithService:SERVICE_NAME withGroup:nil; -(void) showMessage:(NSString)text UIAlertView alert =UIAlertView alloc initWithTitle:"Info"message:text delegate:nil cancelButtonTitle:"Cancel" otherButtonTitles: nil; alert show; - (IBAction)save:(id)sender NSString keyString =self.keyTextField.text; NSData data = self.dataTextField.text dataUsingEncoding:NSUTF8StringEncoding; if(keychain insert:keyString :data) self showMessage:"Successfully added data"; else self showMessage:"Failed to add data"; self.dataTextField.text =""; self.keyTextField.text =""; - (IBAction)find:(id)sender NSString keyString =self.keyTextField.text; NSData data =keychain find:keyString; if(data) self showMessage:NSString alloc initWithData:data encoding:NSUTF8StringEncoding; else 143 iCloud and Security Services self showMessage:"Failed to get Data"; - (BOOL)textFieldShouldReturn:(UITextField )textField textField resignFirstResponder; return YES; Our interface file will probably look like this: 144