Friday, November 25, 2011

Programmatically calling to a phone number from iPhone.

//  This method will call a phone number from string.
+ (void)callPhoneNumber:(NSString *)phoneNumberString
{
        if ([phoneNumberString length]>0)
        {
            phoneNumberString = [[phoneNumberString componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] componentsJoinedByString: @""];
            NSString *phoneDecender = @"tel:";
            NSString *totalPhoneNumberString = [phoneDecender stringByAppendingString:phoneNumberString];
            NSURL *aPhoneNumberURL = [NSURL URLWithString:totalPhoneNumberString];
       
            if ([[UIApplication sharedApplication] canOpenURL:aPhoneNumberURL])
            {
                    [[UIApplication sharedApplication] openURL:aPhoneNumberURL];
            }
       
        }
}

Applying Java script to UIWebView from Objective C - iPhone SDK

//------------------------------------------------------------------------------------------------------------ 
/*Java script code */
function switchFontsize(val)
{
    if(val)
    {
         document.getElementById("myDivId").style.fontSize = val + "px";
         document.getElementById("mySecondDivId").style.fontSize = val + "px";
    }
}
//-------------------------------------------------------------------------------------------------------------
/* Objective C function to apply font to the web view */
+ (void)applyFontToWebView:(UIWebView *)webView withFontSize:(int)fontSize
{
    NSString *jsString = [NSString stringWithFormat:@"switchFontsize(%d)",fontSize];
    [webView stringByEvaluatingJavaScriptFromString:jsString];
}
//-------------------------------------------------------------------------------------------------------------
/* Function to call when font plus button press event */
- (void)increaseFont
{
    // 24 is set as the maximum font size.
    if (currentFontSize < 24)
    {
        currentFontSize = currentFontSize + 2;
    }
    [FontClass applyFontToWebView:self.webView withFontSize:currentFontSize];
}

Note - Make sure that you are calling the java script file in the html string loaded in the webview.

Tuesday, May 31, 2011

Validating UITextView For Minimum Text Length

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{

if (([text length] == 0)&& ([textView.text length] ==1))
{
self.sendButton.enabled = NO;
}
else if ([text length] == 1)
{
self.sendButton.enabled = YES;
}

return YES;
}

Friday, January 14, 2011

NSXMLParserErrorDomain error 68

If you get this kind of parsing error,

NSXMLParserErrorDomain error 68 or called as 'NSXMLParserNAMERequiredError'

Reason :- Most likely, the error is caused because the xml data you are parsing. in the xml there will be some special characters that are not encoded.

Example :- character '&' did not show up as '&amp'

That is what the error means: Parser is expecting an input for that assuming '&' as a element.

Solution :- Change the xml.

For more details :- NSXMLParser Class Reference

Thanks

Sunday, January 2, 2011

Creating a POST request and downloading the data. - iOS SDK

NetCommunicator.m

@implementation NetCommunicator

@synthesize myFeedConnection,myReceivedData;

-(void)createPostRequestWith:(NSString *)username andPassword:(NSString *)password {

NSString *post = [NSString stringWithFormat:@"username=%@&password=%@",username,password];
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];

NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];

NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:@"www.yourwebpage.com"]];

[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];

self.myFeedConnection = [[[NSURLConnection alloc] initWithRequest:request delegate:self] autorelease];

// Test the validity of the connection object. The most likely reason for the connection object
// to be nil is a malformed URL, which is a programmatic error easily detected during development
// If the URL is more dynamic, then you should implement a more flexible validation technique, and
// be able to both recover from errors and communicate problems to the user in an unobtrusive manner.
//

NSAssert(self.myFeedConnection != nil, @"Failure to create URL connection.");

// show in the status bar that network activity is starting
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
}



#pragma mark -
#pragma mark NSURL Connection Delegate

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {

if (connection == self.myFeedConnection) {
self.myReceivedData = [NSMutableData data];
}

}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {

if (connection == self.myFeedConnection) {

[self.myReceivedData appendData:data];
}

}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {

[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;

if (connection == self.myFeedConnection) {
if ([error code] == kCFURLErrorNotConnectedToInternet)
{
// if we can identify the error, we can present a more precise message to the user.
NSDictionary *userInfo = [NSDictionary dictionaryWithObject:@"No Connection Error"
forKey:NSLocalizedDescriptionKey];
NSError *noConnectionError = [NSError errorWithDomain:NSCocoaErrorDomain
code:kCFURLErrorNotConnectedToInternet
userInfo:userInfo];
[self handleError:noConnectionError];
}
else
{
// otherwise handle the error generically
[self handleError:error];
}
// release our connection
self.myFeedConnection = nil;
}

}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {

if(connection == self.myFeedConnection) {

[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;

NSLog(@"my Data: %.*s", [self.myReceivedData length], [self.myReceivedData bytes]);

// Do the rest with the downloaded data.

self.myFeedConnection = nil;

}

}


// -------------------------------------------------------------------------------
// handleError:error // This method should be modified ..
// -------------------------------------------------------------------------------
- (void)handleError:(NSError *)error
{
NSString *errorMessage = [error localizedDescription];
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Cannot download"
message:errorMessage
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alertView show];
[alertView release];
}


@end

Tuesday, November 2, 2010

Core Data - Compile error

If You get this kind of compile error,

"_OBJC_CLASS_$_NSManagedObjectContext", referenced from:
objc-class-ref-to-NSManagedObjectContext in AppDelegate.o

"_OBJC_CLASS_$_NSEntityDescription", referenced from:
objc-class-ref-to-NSEntityDescription in AppDelegate.o

"_NSSQLiteStoreType", referenced from:
_NSSQLiteStoreType$non_lazy_ptr in AppDelegate.o
(maybe you meant: _NSSQLiteStoreType$non_lazy_ptr)


"_OBJC_CLASS_$_NSPersistentStoreCoordinator", referenced from:
objc-class-ref-to-NSPersistentStoreCoordinator in AppDelegate.o

"_OBJC_CLASS_$_NSFetchRequest", referenced from:
objc-class-ref-to-NSFetchRequest in AppDelegate.o

"_OBJC_CLASS_$_NSManagedObjectModel", referenced from:
objc-class-ref-to-NSManagedObjectModel in AppDelegate.o
ld: symbol(s) not found

collect2: ld returned 1 exit status


Solution:
Most likely, the error is caused because the Core Data Framework is not added to the project.

Right click Frameworks folder -> Add -> Existing Frameworks -> CoreData.framework.

Done. :)

Wednesday, June 16, 2010

Introduction To Android

I have just started looking into Android development. Here I am including some basic things I found about Android around the web.

Android is an open source mobile operating system that combines and builds upon parts of many different open source projects. You have access to the source code of the platform that is running on the phone. This can help you better understand how interface controls and the various other pieces work. If you happen to find a bug, you can also submit a patch for the issue, though this is a more advanced practice. Google has also pulled together a large group of companies (called the Open Handset Alliance) that both contribute to and use the Android OS in their hardware devices. This means that there is industry-wide support for Google’s OS, promising wide adoption across well-known vendors.

Advantages of developing for the Android platform:

* The development tools for the platform are free to download, and Google only charges a small fee to distribute applications on the Android Market.

* Freedom to innovate. The Android OS is an open-source platform based on the Linux kernel and multiple open-source libraries. In addition to building applications to run on Android devices, developers are free to contribute to or extend the platform as well.

* Freedom to collaborate. Android developers are not required to sign an NDA and are encouraged to collaborate and share source code with each other. According to a survey by Black Duck Software, the number of open source mobile apps and libraries grew at a rate of 168% from 2008 to 2009, faster on Android than any other platform. This means more code that you can reuse in your own projects to bring them to market much faster.

* Open distribution model. Very few restrictions are placed on the content or functionality allowed in Google’s Android Market, and developers are free to distribute their applications through other distribution channels as well.

* Multi-platform support. There are a wide variety of hardware devices powered by the Android OS, including many different phones and tablet computers. Development for the platform can occur on Windows, Mac OS or Linux.

* Multi-carrier support. A large number of telecom carriers currently offer Android powered phones.

to be continued .... :)

Friday, April 16, 2010

iPhone OS 4.0

Things to know about the iPhone OS 4.0

1. Multi-tasking

Any iPhone user will have been enraged for years that they can only perform one task at a time on their device (and this now includes iPad users as well).

Thankfully, that has changed, and in true Apple form it's been done with a slick overlay.

Basically, rather than an ugly task manager, double-tapping the home button will bring up a dock of (admittedly, only four) running applications you can switch to while staying in another app.

While this is cool in itself, Apple is taking it up a notch by using the multi-tasking element to add in some new functionality.

Location-based services and VoIP are all now included and can stay running even when you're on the move and not using them - great news for the likes of Skype.

And push just got that much bigger for Apple - while push notifications have hit 10 billion since launch last year, new 'local' notifications won't need to parse the Apple servers, thus saving batteries.

2. Apple's new Game Center

Apple has been banging on about how great its iPhone platform is for gaming, and its launched the Game Center to back it up.

Basically, it's a way of taking gaming for the iPhone from the device and into the cloud, with the launch of social networking for gaming.

This means that you can invite friends to join you in a game, or if you're up too late/a complete Billy-No-Mates then you can use the MatchMaking option to find you a gaming partner.

Additional functionality comes in the form of integrated leader boards and achievements - ways to boast about how good you are at games is kind of the reason we all play them in the first place.

Apple hinted that points might come into the achievements soon, jumping on the bandwagon Microsoft put in motion with Windows Phone 7.

3. Revolutionising the world of mobile advertising

We all wondered what Apple was doing when it quietly bought Quattro last year, a mobile advertising firm.

Well, it's become clear - it was launching iAd, to help it change the way we think about mobile advertising.

Say what you like about Jobs' lot, its made a huge success of the mobile apps market, and its taken that know-how and used it to actually make in-app advertising compelling.

Imagine you're in a general movie showtimes app, and then suddenly you see Woody from Toy Story poking his head out the bottom. 'Peel' the bottom layer up and you get an app within an app, offering you all kinds of cool things.

App developers can add in things like maps, video and e-commerce stores from these iAds, meaning a rich source of revenue that is a lot more entertaining than simply seeing boring words served against your App.

Apple has realised the potential here too - it's taking a massive 40% of the revenue for 'serving the iAds', which could be a very tidy extra income indeed.

4. Folders for your icons

Ever been left frustrated by the fact that you can 'only' have 180 icons on your home screen? Well, Folders is here to save you from that fate, giving you the ability to hoard up to 2,000 apps on your device.

It's more about organisation than anything else - making it easier to group your applications into elements like gaming, sports apps, utilities etc.

The folders themselves will auto-name, or you can call them what you like. If you don't want your partner to find your more dodgy apps, we don't recommend calling the folder 'System32' - not only is it not very Apple-like, apparently they know that it's not a real OS subfolder...

And for Jobs' final folder trick, it was shown that you can now change the background wallpaper on the iPhone. You know, like you've been able to do on phones since the dark ages.

5. The unified inbox

The iPhone has evolved slowly - too slowly, some might say, with Apple seemingly dragging its heels to make sure it has stuff to update.

But one of the problems to come out of these slow updates is the existence of so many email inboxes on people's iPhones, meaning you have to jump in and out of them to see all your mail - tricky if you're a freelancer or just an @ enthusiast.

So Apple has unified them all into one place - thankfully coinciding with the announcement you can have more than one Exchange account on its devices.

Not only will the messages appear in one central place, the conversations will now also be threaded, meaning you won't have to hop in and out of emails to see what's been said.

Attachments via Apps are also included as well - meaning you can open and manipulate elements included on email the way you want to.