Sunday, 5 May 2013

iOS Programming Basic: How Does the Hello World App Work?

Before  start you may come across these questions:
  • What are those .xib, .h and .m file?
  • What are those “ugly” code inside “showMessage”? What do they mean?
  • What actually happens after you taps the “Hello World” button? How does the button trigger the “showMessage” action?
  • How does the “Run” button in Xcode work?

 

Interface Builder, Header and Implementation Files

.xib – For files with .xib extension, they’re Interface Builder files that store the application’s user interface (UI). As you click on the .xib file, Xcode automatically switches to the Interface Builder for you to edit the UI of the app via drag-and-drop.

.h and .m – Files with .h extension refers to the header files while those with .m extension are the implementation files. Like most of the programming languages, the source code of Objective-C is divided into two parts: interface and implementation.  

In Objective-C, interfaces of a class are organized in “.h” file. We use the syntax “@interface” to declare the interface of a class. Take a look at the HelloWorldViewController.h, which is the header file:

@interface HelloWorldViewController : UIViewController

-(IBAction)showMessage;

@end
The actual implementation is put in the HelloWorldViewController.m, the implementation file:
@implementation HelloWorldViewController

// I've removed other methods for better reading. Focus on the showMessage method first.

- (IBAction)showMessage
{
    UIAlertView *helloWorldAlert = [[UIAlertView alloc]
                                    initWithTitle:@"My First App" message:@"Hello, World!" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
   
    // Display the Hello World Message
    [helloWorldAlert show];
}

@end

 

Monday, 22 April 2013

Dropbox Integration in IOS Device :

Requirements:


1. You need the 4.0 version of the iPhone SDK. The version of your XCode should
be at least 3.2.3.
2. You need to have registered as a Dropbox application with mobile access at
http://dropbox.com/developers. You should have a consumer key and secret.
3. You need to download the dropbox sdk from https://www.dropbox.com/developers/releases

A. Adding DropboxSDK to your project


1. Open your project in XCode

2. Right-click on your project in the group tree in the left pane

3. Select Add -> Existing Files...

4. Navigate to where you uncompressed the Dropbox SDK and select the DropboxSDK

subfolder

5. Select "Copy items into destination group's folder"

6. Make sure "Recursively create groups for any added folders" is selected

7. Press Add button

8. Find the Frameworks folder in your app's group tree in the left pane

9. Make sure the framework Security.framework is added to your project

10. If not, right-click on Frameworks and select Add -> Existing Frameworks...

11. Select Security.framework from the list and select Add

12. Build your application. At this point you should have no build failures or

warning


B. Login successfully in your app


1. In your application delegate's application:didFinishLaunchingWithOptions:
method, add the following code:

 
 DBSession* dbSession =[[DBSession alloc]
                          initWithAppKey:@"URAppKey"
                          appSecret:@"URappSecret"
                          root:kDBRootAppFolder] // either kDBRootAppFolder or kDBRootDropbox
     ;
    [DBSession setSharedSession:dbSession]; 

Note: you will need to #import "DropboxSDK.h" at the top of this file

2. Somewhere in your app, add an event to launch the login controller, which
should look something like this:

- (void)didPressLink {
DBLoginController* controller = [[DBLoginController new] autorelease];
[controller presentFromController:self];
}

Note: you will need to #import "DropboxSDK.h" at the top of this file


C. Creating folder in your dropbox using your App

Create your won ViewController .m and .h files
1. In your .m file add the below code,



@interface DropBoxViewController () < DBLoginControllerDelegate, DBRestClientDelegate>

@property (nonatomic, readonly) DBRestClient* restClient;
@end

#pragma mark DBLoginControllerDelegate methods
- (void)loginControllerDidLogin:(DBLoginController*)controller
{
restClient = [self restClient];
[restClient setDelegate:self];
[def setBool:YES forKey:@"userLoggedToDropboxAccnt"];
[NSUserDefaults resetStandardUserDefaults];
[restClient loadMetadata:@"" withHash:photosHash];
}
- (void)loginControllerDidCancel:(DBLoginController*)controller {
}
- (DBRestClient*)restClient {
if (restClient == nil) {
restClient = [[DBRestClient alloc] initWithSession:[DBSession sharedSession]];
restClient.delegate = self;
}
return restClient;
}

#pragma mark DBRestClientDelegate methods
 
- (void)restClient:(DBRestClient*)client loadedMetadata:(DBMetadata*)metadata {
[photosHash release];
photosHash = [metadata.hash retain];
NSMutableArray* newPhotoPaths = [NSMutableArray new];
for (DBMetadata* child in metadata.contents) {
[newPhotoPaths addObject:child.path];
}
[photoPaths release];
photoPaths = newPhotoPaths;
self.contentArray = photoPaths;
if([photoPaths containsObject:folderNmTxtField.text]){
}
else{
[restClient createFolder:folderNmTxtField.text];
}
}

photosHash is of type NSString defined in .h file

photoPaths is an NSArray defined in .h file


D.Uploading file in Your DropBox using your app


if restClient not initialized earlier, add the below code

restClient=[self restClient];
[restClient setDelegate:self];
[restClient loadMetadata:@"" withHash:photosHash];

for uploading,

[restClient uploadFile: filename toPath: (folder in which file is to be uploaded) fromPath: (path of the file to be uploaded);