Tuesday, July 14, 2015

WatchKit App Architecture / Apple watchkit app development started [4 ]

watchkit app on watch and watchkit extension on iphone.

choosing the scene on watchkit app, WatchKit tells the paired iPhone to launch your WatchKit extension and create the objects needed to manage that scene. When the scene is fully configured, it is displayed on Apple Watch. This transfer of information between the WatchKit app and WatchKit extension happens transparently behind the scenes.

Figure 3-1Communication between a WatchKit app and WatchKit extension

image: ../Art/app_communication_2x.png

Managing Scenes: The Interface Controller

Each scene is managed by a single interface controller object, which is an instance of the WKInterfaceController class which is same purpose as a view controller in iOS.
WatchKit apps typically contain multiple interface controllers, with each one displaying a different type of information. 
Watchkit app life cycle:-
When user launches app on watch it launches watchkit and corresponding  watchkit extension.
After it loads the scene, WatchKit asks the WatchKit extension to create the corresponding interface controller object, which you use to prepare the scene for display to the user. 
Figure 3-2Launching a WatchKit appimage: ../Art/launch_cycle_2x.png
 interface controller’s init and awakeWithContext: methods to load any required data, set the values for any interface objects, and prepare your interface to be displayed. Do not use the willActivate to initialize your interface controller. The willActivate method is called shortly before your interface is displayed onscreen, so you should use that method only to make last-minute changes.


user interactions are handled by your interface controller’s custom action methods.

Glance interfaces do not support action methods. Tapping your app’s glance interface always launches the app.
Your WatchKit extension remains running only while the user is interacting with your app on Apple Watch.  so interface controllers should be lightweight and never perform long-running tasks.
user exits your app explicitly or stops interacting with Apple watch, iOS deactivates the current interface controller and suspends your extension,

Figure 3-3The life cycle of an interface controllerimage: ../Art/watch_app_lifecycle_simple_2x.png

Table 3-1Key methods of WKInterfaceController
Method
Tasks to perform
This method is your first chance to initialize your interface controller.
This method lets you configure the interface controller using any available context data. Use it to load data and update labels, images, tables, and other interface objects in your storyboard scene. The context data is data you provide to assist in the configuration of the new interface controller. For example, when pushing a new interface controller in a hierarchical interface, you specify a context object that contains the next level of data to display. Providing a context object is recommended but not required.
This method lets you know that your interface will soon be visible to the user. Use this method only to make small changes to your interface. For example, you might use this method to update a label based on new data. The bulk of your interface initialization should still occur in the init and awakeWithContext: methods.
Use the didDeactivate method to clean up your interface and put it into a quiescent state. For example, use this method to invalidate timers and stop animations.
You cannot set values for any interface objects from this method. From the time this method is called to the time the willActivatemethod is called again, any attempts to set values for interface objects are ignored.











Sharing Data with Your Containing iOS App
iOS app and WatchKit extension rely on the same data, use a shared app group to store that data. An app group is a secure container that multiple processes can access. Because your WatchKit extension and iOS app run in separate sandbox environments, they normally do not share files or communicate directly with one another. An app group lets the two processes share files or user defaults.

set up a shared app group from the Capabilities tab of your iOS app and WatchKit extension. Enabling the App Groups capability adds an entitlements file (as needed) to each target and adds thecom.apple.security.application-groups entitlement to that file. To share data, both targets must have the same app group selected.


At runtime, you share files between processes by reading and writing those files in the shared container directory. To access the container directory, use the containerURLForSecurityApplicationGroupIdentifier:method of NSFileManager to retrieve the base URL for the directory. Use the provided URL to enumerate the directory contents, or create new URLs for files in the directory.
To share preferences data between apps, create an NSUserDefaults object using the identifier of the shared group. The initWithSuiteName: method of NSUserDefaults creates an object that allows access to the shared user defaults data. Both processes can access this data and write changes to it.

Communicating Directly with Your Containing iOS App

Apps that work closely with their containing iOS app can use the openParentApplication:reply: method to send requests to that app and receive a response. WatchKit extensions do not support background execution modes; they run only while the user is interacting with the corresponding app on Apple Watch. Their containing iOS app has fewer restrictions and can be configured to run in the background or to gather information on behalf of the WatchKit extension. Activities that might require extra time to complete, such as fetching a user’s location, should therefore be performed by the iOS app and communicated back to the WatchKit extension.
When you call the openParentApplication:reply: method, iOS launches or wakes the containing iOS app in the background and calls the application:handleWatchKitExtensionRequest:reply: method of its app delegate. The app delegate performs the request using the provided dictionary and then returns a reply to the WatchKit extension.



Apple watchkit app development started [3 ] / Configuring Your Xcode Project for watchkit

Watchkit is extension to you iOS app.
To add watchkit to app follow below steps(1.File new target watchkit app which will add wathkitapp and  WatchKit extension to your app.
 add a new WatchKit app target, which configures the bundles and initial resources for your WatchKit app and WatchKit extension. 
Those bundles are then delivered as part of your iOS app on the App Store.):-

1. In Xcode, open the project for your iOS app.
2. Select File > New > Target, and navigate to the Apple Watch section.
3. Select WatchKit App, then click Next.
4. If you plan to implement a glance or custom notification interface, select the appropriate checkboxes.
For notification interfaces, it is recommended that you select the Include Notification Scene checkbox, even if you do not plan on implementing that interface right away. Selecting that checkbox adds an additional file to your project for debugging your notification interfaces. If you do not select that option, later you must create the file manually.
5. Click Finish.

WatchKit development requires the iOS 8.2 SDK or later. 

The bundle IDs for both new targets are configured automatically, based on the bundle ID of your iOS app. The base IDs for all three bundles must match; if you change your iOS app’s bundle ID, you must update the other bundle IDs accordingly.

Adding a WatchKit App target to your Xcode project creates two new executables and updates your project’s build dependencies. Building your iOS app builds all three executables (the iOS app, WatchKit extension, and WatchKit app) and packages them together. Xcode also creates a build scheme specifically for building and debugging only your WatchKit app.



The Build, Run, and Debug Process select watchkitapp as as target and run 
If you are using notification then follow below steps

To configure custom build schemes for glances and notifications

  1. Select your existing WatchKit app scheme.
  2. From the scheme menu, select Edit Scheme. image: ../Art/edit_scheme_menu_2x.png
  1. The Build, Run, and Debug Process select watchkitapp as as target and run 
    If you are using notification then follow below steps

    To configure custom build schemes for glances and notifications
    1. Select your existing WatchKit app scheme.
    2. From the scheme menu, select Edit Scheme.


    image: ../Art/duplicate_scheme_2x.pngClose the scheme editor to save your changes.
  2. Specifying a Notification Payload for Testing
    The payload itself is a file that you create with a .apns filename extension.
    The PushNotificationPayload.apns file contains most of the keys you need to simulate a remote notification, and you can add more keys as needed. Figure 2-2 shows the default JSON file that comes with your project.

    Fgure 2-2A simulated remote notification payloadimage: ../Art/PushNotificationPayload.apns_2x.png




WatchKit App / Glance Interfaces / Local and Remote Notifications / Apple watchkit app development started [2] /

1.WatchKit App:- ( contains only the storyboards and resource files associated with your app’s user interface.)
It exists on  the Apple Watch home screen. 
A WatchKit app acts as the public face of your app but it works in tandem with your WatchKit extension, which is the brains of the operation. 

The WatchKit extension contains the code for managing content, responding to user interactions, and updating your user interface. And because the extension runs on the user’s iPhone, it can coordinate with your iOS app as needed to perform more sophisticated tasks.

2. Glance Interfaces:-  Glance are  used to display your app’s most important information. Glances are nonscrolling; the entire glance interface must fit on a single screen.
 Glances are read-only and cannot contain buttons, switches, or other interactive controls. Tapping a glance launches your WatchKit app. To create a glance,  you  have to create a specialized set of objects inside your existing WatchKit app and WatchKit extension. 

2.Local and Remote Notifications :
Apple Watch works with its paired iPhone to display local and remote notifications.Initially, Apple Watch uses a minimal interface to display incoming notifications. When the user’s movement indicates a desire to see more information, the minimal interface changes to a more detailed interface displaying the contents of the notification.You can customize this detailed interface and add custom graphics or arrange the notification data differently from the default interface provided by the system.

Apple Watch support for the actionable notifications introduced in iOS 8. Actionable notifications are a way to add buttons to your notification interface that reflect actions the user might take.

example, a notification for a meeting invite might include buttons to accept or reject the invitation.

Apple watchkit app development started [1]

Apple Watch must have the presence of an iPhone to run third-party .

To create a third-party app, you need two separate bundles: 
1.a WatchKit app (that runs on Apple Watch)  :- contain storyboard and resources only. Code will be in WatchKit extension running on phone.
2. WatchKit extension (that runs on the user’s iPhone) :- code for managing the WatchKit app’s user interface and for responding to user interactions. 

glance:- for displaying static screens
 you can optionally offer users a read-only interface, known as a glance, which displays timely and relevant information from your app.

notification interfaces:- for local and remote notification
 you can change the way local and remote notifications are displayed to your users by providing custom notification interfaces.

WatchKit app extends the behavior of your existing iOS app, the WatchKit app and WatchKit extension are bundled together and packaged inside your iOS app bundle. During installation of your iOS app, the system prompts the user to install the WatchKit app when a paired Apple Watch is present.


Wednesday, May 6, 2015


1
down voteaccepted
Have you tried selecting your xcode with xcode-select?
xcode-select -switch /Applications/Xcode.app/Contents/Developer
In most cases…
You could also try to renew the xcodebuild via
$sudo /usr/bin/xcode-select -switch /Applications/Xcode.app/Contents/Developer
$cd /usr/bin
$sudo rm xcodebuild
$ln -s /Applications/Xcode.app/Contents/Developer/usr/bin/xcodebuild xcodebuild

Sunday, February 15, 2015

app data loss on version upgrade

When not using Core Data, the only way to preserve user data is to store your database in the user Documents directory as well as the Library directory.
Documents
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"UserData.sqlite"]];
Library
[NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) objectAtIndex:0];
These are also the only directories write-accessible to your app. If you have include a "blank" database in your Resources folder, it is copied to the bundle. From there, you need to check whether the db exists in the Documents directory, if not, then copy it from the bundle.
This is the only way to ensure that data is protected on app upgrades.
On another note, if the user does not backup their device, uninstalls the app then reinstalls the app, the data will be gone.
In regards to the user not being notified they will lose their data, the only thing that will prompt the user about loss of data is when they decide to remove the app from the device, or if you implement some way to notify the user of any data change.

If you would have set the version number to your database for your iphone could have been easily handle, save your version number into your db and whenever database is called, compare the version against the expected version
If new version > older version change the schema (this is needed if you would have changed the schema of your database) with using SQL ALTER statements and update the app version number.
So whenever user is going to update or fresh installation, it will check the new version with your older version, if it differ then update schema, and if its same no need to make any changes.

If you would not have made any schema related changes (for example adding new column..) then you do not need to worry, user will not lose the data.

Source:- http://stackoverflow.com/questions/8459216/app-data-loss-on-version-upgrade



Wednesday, January 28, 2015

dequeueReusableCellWithIdentifier usage


The purpose of dequeueReusableCellWithIdentifier is to use less memory. If the screen can fit 4 or 5 table cells, then with reuse you only need to have 4 or 5 table cells allocated in memory even if the table has 1000 entries.In the second way there is no reuse. There is no advantage in the second way over just using an array of table cells. If your table has 1000 entries then you will have 1000 cells allocated in memory. If you are going to do that you would put them in an array and just index the array with the row number and return the cell. For small tables with fixed cells that may be an reasonable solution, for dynamic or large tables it is not a good idea.

What are the things need to take care while designing app for 2 different resolutions ?

It all depends on your image:
  • If your Image can be stretched, UIImageView will do all the work.
  • If only a part of you image should be stretched you should use this:
    • code - imageView.image = [imageView.image resizableImageWithCapInsets:UIEdgeInsetsMake(top, left, bottom, right)];
  • If your image can't be stretched you should then do different images for the phones and change them in runtime.


IPA creation from Xcode

1) Click on project you will see the project settings change the name of bundle identifier for whom we want to make an IPA.
2) Change the signing certificate as per bundle identifier.
3) Edit the scheme and set this set Build configuration for Archive scheme to Release/Distribution [Default Distribution]
4) Select the Build for iOS Device and clean the build.
5) Now select Archive option from Product menu of Xcode.
6) After successful completion it will show you the Archive tab of Xcode from here you can validate your application by selecting latest archive and pressing Validate button or you can make IPA by clicking Distribute button.
7) When you click Distribute button you will get 3 option
a. Submit to App store
b. Save for enterprise or Ad-Hoc deployment - Used to create IPA
c. Export as Xcode archive

8) Select Save for enterprise or Ad-Hoc deployment option, press Next button it will validate and will show you the Certificate you have selected for signing if its ok then press Next. Now IPA is ready and you can save this on HDD to use.

Structure of the IPA

An IPA has a built-in structure for iTunes and AppStore to recognize, The example below shows the structure of an IPA,
/Payload/
/Payload/Application.app
/iTunesArtwork
/iTunesMetadata.plist

As shown above, the Payload folder is what contains all the app data. The iTunes Artwork file is a 512×512 pixel PNG image, containing the app's icon for showing in iTunes and the App Store app on the iPad. The iTunesMetadata.plist contains various bits of information, ranging from the developer's name and ID (e.g., Google), the bundle identifier, copyright information, genre, the name of the app, release date, purchase date, etc.

IPA ?

An .ipa file is an iPhone application archive file which stores an iPhone app. It is usually encrypted with Apple's FairPlay DRM technology. Each .ipa file is compressed with a binary for the ARM architecture and can only be installed on an iPhone, iPod Touch, or iPad. Files with the .ipa extension can be uncompressed by changing the extension to .zip and unzipping.

.IPA files cannot be installed on the iPhone Simulator because they do not contain a binary for the x86 architecture. To run applications on the simulator, original project files which can be opened using the Xcode SDK are required. Occasionally, however, some .IPA files can be opened on the simulator by extracting and copying over the .app file found in the Payload folder. Some simple apps are able to run on the simulator through this method.

CoreVideo Framework


Core Video provides a pipeline model for digital video in iOS. Partitioning the processing into discrete steps makes it simpler for developers to access and manipulate individual frames without having to worry about translating between data types such as QuickTime. It also provides buffer and buffer pool support for Core Media. Applications that do not need to manipulate individual frames should never need to use Core Video directly.

CFNetworkFRamework

CFNetwork is a framework in the Core Services framework that provides a library of abstractions for network protocols. These abstractions make it easy to perform a variety of network tasks, such as: 1. Working with BSD sockets.
2. Creating encrypted connections using SSL or TLS.

  • 3. Resolving DNS hosts
  • 4. Working with HTTP, authenticating HTTP and HTTPS servers
  • 5. Working with FTP servers

Difference between "strong" and "weak"

Strong means that as long as this property points to an object, that object will not be automatically released.

Weak instead, means that the object the property points to, is free to release but only if it sets the property to NULL

NSOperation


The NSOperation class is an abstract class you use to encapsulate the code and data associated with a single task. Because it is abstract, you do not use this class directly but instead subclass or use one of the system-defined subclasses (NSInvocationOperation or NSBlockOperation) to perform the actual task.

Location Based Services


Location Services allows location-based apps and websites (including Maps, Camera, Safari, and other Apple and third-party apps) to use information from cellular, Wi-Fi1, and Global Positioning System (GPS)2 networks to determine your approximate location.

__block


When variable is marked with __block, the modifications done inside the block are also visible outside of it.

Block

Block objects are a C-level syntactic and runtime feature. They are similar to standard C functions, but in addition to executable code they may also contain variable bindings to automatic (stack) or managed (heap) memory.

You can use blocks to compose function expressions that can be passed to API, optionally stored, and used by multiple threads. Blocks are particularly useful as a callback because the block carries both the code to be executed on callback and the data needed during that execution.

layoutSubviews


layoutSubviews is a method that is called automatically during view size changes. It can be used by a custom view to perform additional manual changes above and beyond any autoresizing behaviors

Autolayout


Auto layouts introduced in iOS6 with which we can support different screen resolution. Also it makes it easier with internationalization we don't need different xib or storyboard for every language.

Grand Central Dispatch


Grand Central Dispatch (GCD) comprises language features, runtime libraries, and system enhancements that provide systemic, comprehensive improvements to the support for concurrent code execution on multicore hardware in iOS and OS X.

iOS Design Patterns

A design pattern is a template for a design that solves a general, recurring problem in a particular context. It is a tool of abstraction that is useful in fields like architecture and engineering as well as software development.

  • a. Abstract Factory Pattern
  • b. Adaptor Pattern
  • c. Object Modeling Pattern
  • d. Chain of Responsibility Pattern
  • e. Command Pattern
  • f. Composite Pattern
  • g. Decorator Pattern
  • h. Facade Pattern
  • i. Iterator Pattern
  • j. Mediator Pattern
  • k. Memento Pattern
  • l. Model-View-Controller Pattern
  • m. Observer Pattern
  • n. Proxy Pattern
  • o. Receptionist Pattern
  • p. Singleton Pattern
  • q. Template Method Pattern

Design patterns are separated into three categories: creational, structural, and behavioral.
  • Creational patterns are design patterns specifically used for dealing with the creation of objects. The abstract factory and singleton are considered creational patterns.
  • Structural patterns are used to define structures of objects. The adaptor, composite, decorator, and proxy patterns are types of structural design patterns.
  • Behavioral patterns identify communication between objects. The chain of responsibility, command, iterator, memento, observer, and template method are examples of behavioral design patterns.

The Model-View-Controller (MVC) design pattern is considered the cornerstone design pattern for iOS app development. The MVC design pattern consists of three parts: model, view, and controller. The model contains data, information, logic, or objects considered part of the business layer of an iOS app. The view contains all of the user information components-such as text areas, buttons, and sliders considered the presentation layer of an iOS app. The controller is the liaison, or communication layer, of an iOS app
Similarly Delegation, and Target-action design patterns. The delegation pattern is based on the concept of one object acting on behalf of another, while target-action is a design pattern where an object stores information that will be sent to a specific object when a certain event happens.

These three design patterns are considered fundamental building blocks for iOS app development.

View's life cycle

Creation
1a -> initWithCoder for initializing view via Storyboard
1b -> initWithNibName for initializing view via Nib file
1c -> init if we do not use none of the above 2 methods and use simple View init method to initialize View.
2   -> loadView
3   -> viewDidLoad
4   -> viewWillAppear
5   -> viewWillLayoutSubviews
6   -> viewDidLayoutSubviews
7   -> viewDidAppear
Destruction
1   -> viewWillDisappear
2   -> viewDidDisappear
3   -> viewWillAppear
4   -> viewWillLayoutSubviews
5   -> viewDidLayoutSubviews

6   -> viewDidAppear

JSON and XML difference and Usage

1. XML is easier to understand than JSON
2. XML is markup language so it is used for hierachiecal element which can not be done in JSON
3. JSON is very simple and can easily be used for text format objects and its serialization.
4. XML used too much bandwidth than JSON

5. JSON is used widely on Web coz its easier to parse from JavaScript as JSON stands for JavaScript Object Notation but to the same for XML with JavaScript it requires to use different library.

About

Powered by Blogger.