Pages

Subscribe:

Thursday, March 24, 2011

iphone SDK - Tracking the field being edited


How to track the current field being edited in iphone SDK? Here is a sample code.




//
//  RegistrationFormViewController.h
//  RegistrationForm
//
//  Created by Raja on 1/10/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface RegistrationFormViewController : UIViewController {
IBOutlet UITextField *name;
IBOutlet UITextField *age;
IBOutlet UITextField *email;
IBOutlet UITextField *contactno;
IBOutlet UILabel *lblPreviousEdit;
IBOutlet UILabel *lblCurrentEdit;
}

@property (nonatomicretain) UITextField *name;
@property (nonatomicretain) UITextField *age;
@property (nonatomicretain) UITextField *email;
@property (nonatomicretain) UITextField *contactno;
@property (nonatomicretain) UILabel *lblPreviousEdit;
@property (nonatomicretain) UILabel *lblCurrentEdit;

@end

iPhone SDK - How to get UDID?


How to get the UDID of the iPhone via code?


//
//  SerialViewController.h
//  Serial
//
//  Created by Raja on 10/11/10.
//  Copyright __MyCompanyName__ 2010. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface SerialViewController : UIViewController {
IBOutlet UILabel *t1,*t2,*t3,*t4,*t5,*t6;
}

@end


//
//  SerialViewController.m
//  Serial
//
//  Created by Raja on 10/11/10.
//  Copyright __MyCompanyName__ 2010. All rights reserved.
//

#import "SerialViewController.h"
#import <dlfcn.h>
#import <mach/port.h>
#import <mach/kern_return.h>

@implementation SerialViewController



/*
// The designated initializer. Override to perform setup that is required before the view is loaded.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) {
        // Custom initialization
    }
    return self;
}
*/

/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
}
*/



// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
/*CFTypeRef serialNumberAsCFString;
io_service_t platformExpert = IOServiceGetMatchingService(kIOMasterPortDefault, IOServiceMatching("IOPlatformExpertDevice"));
if (platformExpert)
{
serialNumberAsCFString = IORegistryEntryCreateCFProperty(platformExpert, CFSTR(kIOPlatformUUIDKey), kCFAllocatorDefault, 0);
}
IOObjectRelease(platformExpert);
NSString *serial = [[NSString alloc] initWithFormat:@"%@",serialNumberAsCFString];
    NSLog(@"serail no==>%@",serialNumberAsCFString);
    NSLog(@"serail no==>%@",serial);*/
NSString *serialNumber = nil;
void *IOKit = dlopen("/System/Library/Frameworks/IOKit.framework/IOKit"RTLD_NOW);
if (IOKit)
{
mach_port_t *kIOMasterPortDefault = dlsym(IOKit, "kIOMasterPortDefault");
CFMutableDictionaryRef (*IOServiceMatching)(const char *name) = dlsym(IOKit, "IOServiceMatching");
mach_port_t (*IOServiceGetMatchingService)(mach_port_t masterPort, CFDictionaryRef matching) = dlsym(IOKit, "IOServiceGetMatchingService");
CFTypeRef (*IORegistryEntryCreateCFProperty)(mach_port_t entry, CFStringRef key, CFAllocatorRef allocator, uint32_t options) = dlsym(IOKit, "IORegistryEntryCreateCFProperty");
kern_return_t (*IOObjectRelease)(mach_port_t object) = dlsym(IOKit, "IOObjectRelease");
if (kIOMasterPortDefault && IOServiceGetMatchingService && IORegistryEntryCreateCFProperty && IOObjectRelease)
{
mach_port_t platformExpertDevice = IOServiceGetMatchingService(*kIOMasterPortDefault, IOServiceMatching("IOPlatformExpertDevice"));
if (platformExpertDevice)
{
CFTypeRef platformSerialNumber = IORegistryEntryCreateCFProperty(platformExpertDevice, CFSTR("IOPlatformSerialNumber"), kCFAllocatorDefault0);
if (CFGetTypeID(platformSerialNumber) == CFStringGetTypeID())
{
serialNumber = [NSString stringWithString:(NSString*)platformSerialNumber];
CFRelease(platformSerialNumber);
}
IOObjectRelease(platformExpertDevice);
}
}
dlclose(IOKit);
}
NSLog(@"Serial NO ==>%@",serialNumber);
t1.text=serialNumber;
NSString *id = [[UIDevice currentDeviceuniqueIdentifier];
t2.text=id;
NSLog(@"UDID==>%@",id);
NSString *name = [[UIDevice currentDevicename];
t3.text=name;
NSLog(@"Name==>%@",name);
NSString *systemName = [[UIDevice currentDevicesystemName];
t4.text=systemName;
NSLog(@"systemName==>%@",systemName);
NSString *systemVersion = [[UIDevice currentDevicesystemVersion];
t5.text=systemVersion;
NSLog(@"systemVersion==>%@",systemVersion);
NSString *model = [[UIDevice currentDevicemodel];
t6.text=model;
NSLog(@"model==>%@",model);
NSString *localizedModel = [[UIDevice currentDevicelocalizedModel];
NSLog(@"localizedModel==>%@",localizedModel);
//name
//systemName
//systemVersion
//model
//localizedModel
    [super viewDidLoad];
}



/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}


- (void)dealloc {
    [super dealloc];
}

@end
/*
 * Adds the serialNumber property to the UIDevice class
 *
 * The implementation uses undocumented (for iOS) IOKit functions,
 * so handle with caution and be prepared for nil.
 */

#import <UIKit/UIDevice.h>

@interface UIDevice (serialNumber)

@property (nonatomicreadonly) NSString *serialNumber;

@end

/*
Licensed under the MIT License

Copyright (c) 2010 Cédric Luthi

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/

iPhone SDK - Simulate the copy, paste text with pasteboard

How to simulate the cut copy and paste functions in code with pasteboard? Here is a simple example for that.

1. Start a new xcode project.
2. Add two textviews (textpad and notepad in my example!) and three buttons as below.


Here is the interface code.


//
//  PasteBoardSampleViewController.h
//  PasteBoardSample
//
//  Created by Raja T S Sekhar on 3/24/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface PasteBoardSampleViewController : UIViewController {

IBOutlet UITextView *textPad;
IBOutlet UITextView *notePad;
}

@property (nonatomic, retain) UITextView *textPad;
@property (nonatomic, retain) UITextView *notePad;

-(IBAction)copy;
-(IBAction)cut; 
-(IBAction)paste;

@end

Here is the implementation

//
//  PasteBoardSampleViewController.m
//  PasteBoardSample
//
//  Created by Raja T S Sekhar on 3/24/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "PasteBoardSampleViewController.h"

@implementation PasteBoardSampleViewController

@synthesize textPad;
@synthesize notePad;

/*
// The designated initializer. Override to perform setup that is required before the view is loaded.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}
*/

/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
}
*/



// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
}

-(IBAction)cut {
[self copy];
textPad.text = @"";

-(IBAction)copy {
NSString *copyString = [[NSString alloc] initWithFormat:@"%@",[textPad text]];
UIPasteboard *pb = [UIPasteboard generalPasteboard];
[pb setString:copyString];
}

-(IBAction)paste {
UIPasteboard *pb = [UIPasteboard generalPasteboard];
notePad.text = [pb string];
}

Run the application.

Type something first text view. Now use the buttons to copy/cut and paste the text to other. The code uses pasteboard for the process.



iPhone SDK - Post messages in Facebook

How to post messages or comments in Facebook wall with iPhone SDK? Here is the sample code!

1. Download the Facebook API from Git
2. Start a new project in xcode.
3. Drag and drop the src folder from Facebook API to our new project (in classes)

Here is your app delegate interface.

App Delegate .h


#import <UIKit/UIKit.h>
#import "FBConnect.h"


@class FacebookSampleViewController;

@interface FacebookSampleAppDelegate : NSObject <UIApplicationDelegate, FBSessionDelegate> {
    UIWindow *window;
    FacebookSampleViewController *viewController;

Facebook *facebook;
}

@property (nonatomic, retain) Facebook *facebook;
@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet FacebookSampleViewController *viewController;

@end

Here is the app delegate implementation.
You need the APPID from Facebook here. publish_stream permission is to post messages on your wall.

App Delegate .m



#import "FacebookSampleAppDelegate.h"
#import "FacebookSampleViewController.h"

@implementation FacebookSampleAppDelegate

@synthesize window;
@synthesize viewController;
@synthesize facebook;


#pragma mark -
#pragma mark Application lifecycle

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    
    
    // Override point for customization after application launch.


facebook = [[Facebook alloc] initWithAppId:@"xxxxxxxxxxxxx"]; // Use your app id here
NSArray* permissions =  [[NSArray arrayWithObjects:@"publish_stream", @"read_stream", nil] retain];
[facebook authorize:permissions delegate:self];
    // Add the view controller's view to the window and display.
    [self.window addSubview:viewController.view];
    [self.window makeKeyAndVisible];

    return YES;
}

- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url {
    return [facebook handleOpenURL:url]; 
}


Now, find the view controller interface below. Here we have a button which call the post messages action/dialog.

#import <UIKit/UIKit.h>
#import "FBConnect.h"
#import "FBDialog.h"
#import "FacebookSampleAppDelegate.h"

@class FacebookSampleAppDelegate;

@interface FacebookSampleViewController : UIViewController <FBRequestDelegate>{

FacebookSampleAppDelegate *facebookSampleAppDelegate;
}
@property (nonatomic, retain) FacebookSampleAppDelegate *facebookSampleAppDelegate;

- (IBAction) showPost;

@end


And here is the implementation for view controller.

#import "FacebookSampleViewController.h"

@implementation FacebookSampleViewController

@synthesize facebookSampleAppDelegate;

/*
// The designated initializer. Override to perform setup that is required before the view is loaded.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}
*/

/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
}
*/



// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {

facebookSampleAppDelegate = (FacebookSampleAppDelegate *)[[UIApplication sharedApplication] delegate];

//[facebookSampleAppDelegate.facebook authorize:nil delegate:self];
    [super viewDidLoad];
}



- (IBAction) showPost {
[facebookSampleAppDelegate.facebook dialog:@"feed" andDelegate:self];
}



Run the above application.
It will ask you to authorize Facebook connect.
Then click the post button. You will be presented the post messages dialog through which you can publish messages on your wall.