• Skip to main content
  • Skip to primary sidebar

Ravi Shankar

Tweaking Apps

  • Swift
  • Tech Tips

Develop

How to only enable portrait orientation for iPhone App

June 14, 2012 By Ravi Shankar 2 Comments

When developing an iPhone app in Xcode, if you have any requirement that only portrait orientation allowed then you can do the following.

Step 1: Open the implementation file for editing.

Step 2. Navigate to shouldAutorotateToInterfaceOrientation method. This methods is auto generated by Xcode while creating the project.

Step 3: replace the return statement with return (interfaceOrientation == UIInterfaceOrientationPortrait);. The update method should be displayed as shown below.

– (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation

{

    return (interfaceOrientation == UIInterfaceOrientationPortrait);

}

This method will return true only when the orientation in Portrait mode.

Step 4: Now navigate to Project Folder on the navigattion bar. In the summary tab under Supported Device Orientations, make sure to un mark the other orientation except portrait.

Portrait Device Orientation

 

Filed Under: Apple, Develop, iPhone Tagged With: device orientation, dsiable, iPhone app, portrait, Xcode

Provisioning iPhone 4S for deploying and testing Apps

March 31, 2012 By Ravi Shankar 1 Comment

Listed below are detail steps for provisioning iPhone 4S for deploying and testing Apps. The steps have been broadly classified in to the following topics.

  • Requesting Development Certificate
  • Submit Certificate in iOS Provisioning Portal
  • Installing Certificate
  • Registering Device to Provisioning Portal

Requesting Development Certificate

Requesting development certificate requires you to generate a Certificate Signing Request (CSR). The CSR can be generated using the KeyChain app available as part of the Mac OS. You can quickly launch keychain using spotlight search. Apart from generating the CSR, the Keychain app also generates the public and private key.

201203311214.jpg

Select Keychain Access preferences from the menu list and navigate to Certificates tab.

201203311227.jpg

Turn off the Online Certificate Status Protocol (OCSP).

201203311229.jpg

Now to request Certificate, click the Keychain Access menu, select Request a certificate from a Certificate Authority under the Certificate Assistant.

201203311244.jpg

In the Certificate Assistant window, enter your email address, name and mark the radio option with caption as Saved to disk also the check box with label as Let me specify key pair information.

201203311250.jpg

Save the generated certificate to your desktop.

201203311318.jpg

Then for the Key Pair information, select Key Size as 2048 bits and Algorithm as RSA.

201203311319.jpg

On clicking the continue button will display the following confirmation message.

201203311439.jpg

The login section under Keychain Access would display the generated Public key and Private key.

201203311440.jpg

Submitting Request in Provisioning Portal

Login to the members account with your Apple user id and password. Click the iOS Provisioning Portal link available under Developer Program Resources section.

201203311444.jpg

In the Provisioning Portal, navigate to Certificates section and click the Request Certificate button under Development.

201203311443.jpg

This would display a screen with option to submit the CSR.

201203311446.jpg

Choose the required CSR and click the Submit button. After submitting the status would initially be displayed as Pending issuance and later a download link would appear.

201203311447.jpg

201203311448.jpg

Now install the WWDR certificate and iOS developer Certificate on your Mac system

201203311449.jpg

201203311450.jpg

You can verify the installation of certificates by navigating to My Certificates in Keychain Access App.

201203311451.jpg

Registering Device to Provisioning Portal

You can use Xcode for registering the Device ID and this process also create the App ID. Launch Xcode, click the Window menu option and select Organizer from the menu list.

201203311454.jpg

This would display the list of available Devices that have been connected to the Mac system,

201203311455.jpg

When the device is connected, a green status light will be displayed. Now select iPhone 4S and click the Use for Development button. If your Device and Mac system are out of sync with the iOS SDK then you will get the message to keep both of them in sync.

201203311501.jpg

Once the device and Mac system are in sync, you can add the device to Provisioning Portal by right clicking on the device and selecting Add Device to Provisioning Portal.

201203311503.jpg

Xcode will request permission for accessing your Keychain, click Always Allow button and sign-in with your Apple Developer Account credentials. Also allow code sign to sign using your key in keychain.

201203311506.jpg

Now you can test your app on iPhone 4S by selecting the device from Active Scheme list.

201203311508.jpg

Filed Under: Develop, ios Tagged With: Apple, Blogging, deploying, iphone, Provisioning

Code Example – Check for Prime Number Objective C

February 10, 2012 By Ravi Shankar 1 Comment

This is a simple Objective C program written to check whether a number is a Prime number.

//

// main.m

// PrimeNumbers

//

// Created by Ravi Shankar on 10/02/12.

// Copyright (c) 2012 rshankar.com. All rights reserved.

//

#import <Foundation/Foundation.h>

int main (int argc, const char * argv[])

{

@autoreleasepool {

  

int number;

BOOL isPrime=YES;

NSLog (@”Enter a number”);

scanf(“%i”,&number);

for (int i=2; i < number –1; i++)

{

if (number % i == 0)

{

isPrime = NO;

break;

}

}

if (isPrime)

{

NSLog (@”%i is a Prime Number”,number);

}

else

{

   NSLog (@”%i is not a Prime Number”, number);

}

  

}

return 0;

}

Output (as displayed in Xcode console window)

2012-02-10 15:53:52.072 PrimeNumbers[1164:707] Enter a number

23

2012-02-10 15:53:58.665 PrimeNumbers[1164:707] 23 is a Prime Number


2012-02-10 15:54:41.469 PrimeNumbers[1176:707] Enter a number

24

2012-02-10 15:54:44.589 PrimeNumbers[1176:707] 24 is not a Prime Number


This program checks whether the number is divisible by any of the n-1 number and if divisible (% operator returns reminder as 0) then the flag is set to false otherwise it is set to true. Then based on the flag value the message is printed to the console window.


Filed Under: Blogging, Develop, ios Tagged With: Objective C, prime number, Program

iPad App that prints Welcome Message

February 2, 2012 By Ravi Shankar Leave a Comment

Here is an example of writing an iPad app that prints a Welcome message.

Launch Xcode, click File -> New -> New Project.

New Project

Now select the template for the project as Single View Application.

Project Template

Provide the details for your project, like Product Name, Company Name then choose the device for which you are writing the App. Also mark the check box with label as Use Automatic Reference Counting and click the Next button.

Options for New IPad Project

And finally click the Create button to complete the project creation. Now Open the ViewController.xib file and using the Interface Builder designed add two label controls , TextField and Button as shown below.

UI Interface Builder

Label 1 is used for displaying the caption “Enter your Name”, TextField is used for accepting user input, Display button is used for printing the Welcome message in the second label.

Now edit ViewController.h file and add two IBOutlet elements for UITextField and UILabel and IBAction element for button as shown below.

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController

{

IBOutlet UITextField *txtName;

IBOutlet UILabel *lblMessage;

}

-(IBAction) btnDisplay;

@end

Then edit ViewController.m file and implement the IBAction function as shown below

– (IBAction)btnDisplay {

lblMessage.text = [@”Welcome “ stringByAppendingString: [txtName text]];

}

This function creates local string by adding “Welcome ” and suffixing it with the name entered in the Textfield.
After completing the coding part, it is time to connect the UI Interface elements with the Object References in code. Select ViewController.xib in the Project Navigator section then click the File’s Owner under Place Holder section. Drag and drop the respective object references to the UI element and connect map the btnDisplay with the Button Touch Down event. The completing mapping of UI Interface elements with Object References is shown below.
UI Elements and Object Reference Mapping

Now the Run the App in iPad simulator by clicking the Run option on the menu bar or by pressing Control + R on the Keyboard.
iPad Simulator
Now entering the name and clicking the Display button will print the Welcome message as shown below.
Welcome App in iPad Simulator

Filed Under: Apple, Develop, ios, iPad Tagged With: App, Apple, iPad, Welcome

Dissecting Objective-C program

January 14, 2012 By Ravi Shankar Leave a Comment

Fotolia_29238788_XS.jpg

We have already seen couple of examples in Objective-C like writing your first program in Objective-C and adding Objective-C classes. Now let us try and analyze the code to learn basic coding on Objective-C program.

Adding Comments

The very basic thing to learn is any programming language is “how to add comments”. In Objective-C you can add comments by adding two consecutive slashes “//” before the text entries. And if you want to add block of comments (more than one line) then you start with /* and end the comment with */

Including Header files

In Objective-C, users can include header files by adding “#import <header filename>”. This would include functions and classes from the header files in to the current program. The examples that has been discussed includes “Foundation” and “Calci” header files.

#import <Foundation/Foundation.h>

#import “Calci.h”

main keyword

int main (int argc, const char * argv[])

“main” is the keyword that marks the beginning line for execution of the program.

Reserving memory

  @autoreleasepool {

The execution code is provided within autoreleasepool and this allocates memory for the program. Now you do not have to explicitly drain the pool, the latest release will automatically drain the allocated memory.

Printing Message

 

  NSLog(@”My First Objective C Program using Xcode!”);

 

NSLog routine is used for printing text messages to the console. The text that needs to be printed is preceded with “@” character. And if you want to print value from a variable then you can use “%i” along with text.

 

NSLog(@”Addition of two numbers %i”, [calculator addition]);

 

Method definition

 

In Objective-C, you can define methods as shown below

 

-(void) setNumber1:(int) n1

-(int) addition

  • “-” refers to the access specifier.
  • void and int specifies the return type for the method.
  • setNumber1 and addition refers to the method name.
  • (int) n1 specifies the type of argument and argument that will be passed to the method.
  • return number1+number2; – “return” keyword is used returning the value from the method.

Filed Under: Apple, Develop, ios Tagged With: Apple, Objective C, Program

My fiirst program in Objective C using Xcode

January 11, 2012 By Ravi Shankar Leave a Comment

This tutorial is about writing a basic and simple program in Objective C using Xcode. Let us see an example program that prints “My First Program” in the Console Window.

Xcode new project window

Click the File menu on Xcode, then navigate to New -> New Project. This would display the following Choose a template window. Now select Application under Mac OS X then the Command Line Tool option.

Xcode Command Line Tool

In the Choose options for your new project, enter the name of Product Name and select the Type as Foundation and leave the rest of the option as it is. Click Next to continue with the project creation.

Xcode Choose Project Options

Now specify the location where you want to create the project folder and click Next to continue with the setup.

Xcode Project Creation Folder

This would create a new project window as shown below.

Xcode Project Window

Navigate to Project Navigator and edit main.m file. The Xcode would have already pre-populated code as shown below

//

// main.m

// Exercise1

//

// Created by Ravi Shankar on 11/01/12.

// Copyright (c) 2012 __MyCompanyName__. All rights reserved.

//

#import <Foundation/Foundation.h>

int main (int argc, const char * argv[])

{

@autoreleasepool {

// insert code here…

NSLog(@”Hello, World!”);

}

return 0;

}

Change the “Hello World” text to “My First Objective C Program using Xcode” and save the changes (Command + S).

@autoreleasepool {

// insert code here…

NSLog(@”My First Objective C Program using Xcode!”);

}

You can Run the project in Xcode by clicking Product menu and select Run from the menu list.

Xcode Project Build

You can Run Once the build is complete and successful, the output window in the bottom of Xcode will print the message as “My First Objective C Program using Xcode”.

Xcode Console Output Window

Filed Under: Apple, Develop, ios Tagged With: Apple, Objective C, Program, Xcode

  1. Pages:
  2. «
  3. 1
  4. 2
  5. 3
  6. 4
  7. 5
« Previous Page

Primary Sidebar

TwitterLinkedin

Recent Posts

  • How to block keywords in Jio broadband
  • How to disable opening an app automatically at login in Mac
  • How to set preferred Wifi network on Mac
  • Attribute Unavailable: Estimated section warning before iOS 11.0
  • How to recover Firefox password from Time Machine backup

Pages

  • About
  • Privacy Policy
  • Terms and Conditions

Copyright 2022 © rshankar.com

Terms and Conditions - Privacy Policy