Today, we will talk about implementing the 3D Touch for iOS Devices using “OBJECTIVE C“.
“I will take couple of minutes (please bear :)) to vent out my frustration at Apple KB for giving Objective C a step-motherly treatment for promoting their retarded Swift language for documenting new features.”
The reason I have emphasized Objective C above is because of a reason; there isn’t any proper documentation available written in Objective C for using / implementing 3D Touch. I did spend time to understand and implement 3D Touch in Objective C and I don’t want others to through the same process. OK – So Let’s Get Started !!
This blog assumes the reader has a knowledge of Objective C, XCode IDE and Apple’s newest feature – 3D Touch!
There are 5 Steps to implement this!
Open Xcode and create a Single View Application, with the name “test3D”
You need to remember, that the Quick Launch feature of 3D Touch, is actually executed from your Home Screen. So, you need to keep the following attributes ready:
- Image for the Quick Launch
- Quick Launch Shortcut Title
- Quick Launch Sub-Title
Step 3: In your XCode Project, open the Assets.xcassets image catalog file, and right click on vacant area, you will be shown a context menu option. Click the “New Image Set” menu.
After clicking the New Image Set menu, you will be shown an area, where-in you can drag and drop your image on the three image image placeholders, as shown below:
You are required to drop three images, one onto each placeholder.
I dropped in an image for 1x at 35px X 35px and for 2x – 70px X 70px and for 3x – 105px X 105px
Remember: For each menu option, there would be a new Image Catalogue that needs to be created.
For now lets use only a single menu for our 3D Touch Quick Launch example. An appropriate / meaningful name must be given to each of the image set catalogue.
Now lets shift our focus on the Info.plist file we have in our project.
Here you need to create an Array of Dictionary type for each of the Quick Launch menu option.
In the Info.plist file, right click the row and click “Add Row”
And then add the UIApplicationShortcutItems Key with the Type Array.
Now add a new Dictionary within the newly created UIApplicationShortcutItems array. Right click on the UIApplicationShortcutItems array and click Add Row as shown below:
Now add Item 0 with type Dictionary
You need to add 4 attributes into this Dictionary:
UIApplicationShortcutItemTitle: Here is where you add give title to your Quick Launch menu item
UIApplicationShortcutItemSubtitle: Here is where you add a “meaningful” subtitle to your Quick Launch Shortcut Item
UIApplicationShortcutItemType: You must give a name to your ShortCut item. This name will be identified in this application, when the QuickLaunch Item will be pressed. So again, please give a meaningful name to it
UIApplicationShortcutItemIconFile: It is here that you refer the name of your Icon Catalogue in Step 2. In our example it was just by the name “Image”.
As shown below, we add the four Quick Launch attributes to the Dictionary item:
Now we will shift our focus to the AppDelete file in our project. Click open the AppDelegate.m file and add the completionHandler for the ShortcutItem
*NOTE: I have declared a global variable by the name
Firstly, Add the Below code into application:didFinishLaunchingWithOptions function
* I have removed few boiler plate code here from the AppDelegate.m
Add Global Variable with the name asShortCutType
Please note that in the shortcutItem.type check, we are specifying the name that we had added into the UIApplicationShortcutItemType attribute – <Product Bundle Identifier>.Square
By now, your application should be having a Quick Launch short cut item popping out, when you press on the application icon in the Home Screen
Now when you press the Square Quick Launch menu item, the completion handler in the AppDelete.m gets fired. It is here that, if you have multiple menu item, then you can identify it by using Global Variable declared in the AppDelegate file.
In our example, the global variable is by name asShortCutType
Add the reference to AppDelegate.h file. And add the code in the viewDidLoad event of the ViewController. It is here that the value of the variable in the AppDelete.h file gets passed to the ViewController, and the ViewController gets notified, which menu option was clicked.
Step 5: (Last but very important!)
Now that you are enjoying that the Quick Launch is getting fired, there is one more thing to apply the 3D Touch Quick Launch completely.
There is a stage in the application, where in the application is in the background state, when you press the home button.
In that case, the viewDidLoad event will not fire at all and your ViewController will not get to know, which menu option got clicked.
There is a workaround for this too 🙂 . In your viewDidLoad event handler, add the below line of code:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(check) name:UIApplicationDidBecomeActiveNotification object:nil];
.. and add a method by the name “check” as mentioned in the quote above.
THAT’S ALL FOR YOU TO IMPLEMENT 3D TOUCH USING OBJECTIVE C