Monday 1 October 2012

The Social Framework - Makes the iPhone/iPad App Simple in Facebook and Twitter Posting


Hi All,

For easy sharing of content including images and url's with Facebook, Twitter and Sina Weibo(I am not familiar with this). iOS 6 provides a new framework named Social. 

Now a days in most of the iOS app had a required functionality to share some content in Facebook / Twitter to increase the app acceptability and as advertisement. For achieving this by a formal method the developer has to code separate set of classes to achieve oAuth for each one for Facebook and other for Twitter, as each had their own framework and the developer need to use those classes to communicate with. but for a developer perspective it is very much extravaganza for a simple sharing.

Now everything ends, apple provide a framework for both, effort is very much less and QA effort is also less. cheers Apple!!!!

To Integrate the Facebook / Twitter / SinaWeibo

First add social framework to project.



And import the framework as follows in the class needed to invoke the share action.

#import <Social/Social.h>

Create a SLComposeViewController object with the service type required,

SLComposeViewController *controller = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];

Service type available are,

SLServiceTypeTwitter
SLServiceTypeFacebook
SLServiceTypeSinaWeibo

Then we can set the content to share,
- (BOOL)setInitialText:(NSString *)text;
- (BOOL)addImage:(UIImage *)image;
- (BOOL)addURL:(NSURL *)url;

Setting of parameters can be done before the view is presented.

Present the view when needed,
[self presentViewController:controller animated:YES completion:nil];





In iOS6 there is an option to login the Facebook and Twitter if not logged then when a SLComposeView is present it show an alert to tell the user to login.


cheers!!!!

if you need more details feel free to contact me 

thanks,
Naveen Shan

Thursday 24 May 2012

The iTunesArtwork in iOS Apps


The Artwork need to do by every iOS Developer

Hi All,

Please make sure that we add "iTunesArtwork" image in every iOs App that we did.
It is used to show in iTunes  when an IPA is synced with device.




What we need to do.
1. Create a 512 * 512 PNG image .
2. Named it as "iTunesArtwork" . Remember iOS device is case-sensitive and note there is no extension for the image.
3. Add this image to application bundle.

NB : We can reuse this image as large icon required while submitting the app to app-store.

Also as a developer it is good enough to ensure the application contains all the icons with specified size.

Icon Required For iPhone/iPod and its Naming Convention and the place where it shows

1. Normal Icon  : 57 * 57  - (icon.png) - inside spring board.
2. Normal Icon (Retina display)  : 114 * 114  - (icon@2x.png) - inside spring board when retina display enabled.
3. App icon for the App Store (iTunesArtwork) :  512 * 512 - (iTunesArtwork) - inside iTunes and App-store.
4. App icon for the App Store (iTunesArtwork) (Retina display) :  1024 * 1024 - (iTunesArtwork@2x) - inside iTunes and App-store when retina display enabled.
5. Small icon for Spotlight search results and Settings : 29 * 29 - (icon-Small.png) - inside iPhone Search and Settings.
6. Small icon for Spotlight search results and Settings (Retina display) : 58 * 58 - (icon-Small@2x.png) - inside iPhone Search and Settings when retina display enabled.
7. Launch Image : 320 * 480 - (Default.png) - show while app launching.
8. Launch Image (Retina display) : 640 * 960 - (Default@2x.png) - show while app launching when retina display enabled.

Icon Required For iPad and its Naming Convention and the place where it shows

1. Normal Icon  : 72 * 72  - (icon.png) - inside spring board.
       if it is a universal app name the icon as (icon-Large.png).
2. Normal Icon (Retina display)  : 114 * 114  - (icon@2x.png) - inside spring board when retina display enabled.
       it is same as in case for iPhone/iPod.
3. App icon for the App Store (iTunesArtwork) :  512 * 512 - (iTunesArtwork) - inside iTunes and App-store.
       it is same as in case for iPhone/iPod.
4. App icon for the App Store (iTunesArtwork) (Retina display) :  1024 * 1024 - (iTunesArtwork@2x) - inside iTunes and App-store when retina display enabled.
        it is same as in case for iPhone/iPod.
5. Small icon for Settings : 29 * 29 - (icon-Small.png) - inside iPad Settings.
6. Small icon for Settings (Retina display) : 58 * 58 - (icon-Small@2x.png) - inside iPad Settings when retina display enabled.
7. Small icon for Spotlight search results : 50 * 50 - (icon-Small~ipad.png)  - inside iPad Search.
8. Small icon for Spotlight search results (Retina display) : 100 * 100 - (icon-Small~ipad@2x.png)  - inside iPad Search when retina display enabled.
9. Launch Image Portrait : 768 * 1004 - (Default-Portrait~ipad.png) - show while app launching in portrait orientation.
10. Launch Image Portrait (Retina display) : 1536 * 2008 - (Default-Portrait~ipad@2x.png) - show while app launching in portrait orientation when retina display enabled.
11. Launch Image Landscape : 1024 * 748 - (Default-Landscape~ipad.png) - show while app launching in landscape orientation.
12. Launch Image (Retina display) : 2048 * 1496 - (Default-Landscape~ipad@2x.png) - show while app launching in landscape orientation when retina display enabled.

NB : if we follow the naming convention I specified above then there is no need to specify the icons in application info-plist and also it helps to support the older iOS versions.

So In short,
iPhone app requires : 8 images
iPad app requires     : 12 images
And Universal iOS app requires : 15 images


if we ensure this much of images is added in application then a normal user thinks your application uses nice graphics till he/she launch your app.

if you need more details feel free to contact me

thanks,
Naveen Shan

Sunday 1 April 2012

Inter-App Communication - Document Support in iOS Application


Hi All,

Now, I would like to discuss about how an iOS application can be communicate to other iOS application available in the device (simply says inter-app communication). May be we all know how we are able to open an url in safari from our native app. It like,

 [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://knowledgetransfer-naveenshan.blogspot.com/"]];  

so here safari is an application in the device we invoke it by openURL. The iOS detect the application need to open is by using the given url scheme. i.e. here the given url scheme is "http". usually if the openURL scheme is 'http' or 'https' then iOS launch safari and safari load the given url in it.

Safari detect the url need to load is through the UIApplication delegate which will invoke when an application is launch by some other application the delegate is as follows,

 -(BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {  
   //here we can get the url.  
   return YES;  
 }  

In this delegate method safari will get the url need to load and it load that url inside it.

Now you have in doubt that how can we define a url scheme for our own application,

Its very simple just add a key named "URL types" to your application info plist and define a "URL identifier" and "URL Schemes" inside it.





To load a file in iBook we can use write a scheme like this,

 //to load iBook  
 NSString *stringURL = [NSString stringWithFormat:@"itms-books://?%@",filePath];  
 NSURL *fileURL = [NSURL URLWithString:stringURL];  
 [[UIApplication sharedApplication] openURL:url];  

here we need to know the scheme defined in iBook application. Similarly to open our application from other application that application need to know scheme defined in our application. :) :) :)

So only by defining URL types in plist the iOS only allows other application to open your application only by knowing your application scheme (Great! Security) and not listed in any UIDocumentInteractionController OpenIn Menu . It is because how did iOS know whether that document is supported by your application unless and until you tell them explicitly.

hence we need to define the supported document inside the application info plist then whenever any UIDocumentInteractionController of any application checks for a document OpenIn that specified in your application, then your application is also listed in OpenIn Menu .













Sample Plist Attached here.

cheers!!!!

if you need more details feel free to contact me 

thanks,
Naveen Shan

Tuesday 13 March 2012

Monitor Network Status and Network Type in iOS


Hi All,

In most of the iOS application which uses server and server api's wants to a check whether network is available at any moment when application runs.
we all know how to check the network status (Using Apple's Reachability code we can check the network status by pinging to a hosted server).
But some cases instead of checking for network is available we need to get/alert about network status change and the network type change.

So here I got up with an implementation of a custom class that will alert the network status change.


//To get the Network Status/ Change in Network Type with NSNetworkMonitor,
(Here prefix NS means not 'Next Step' it just 'Naveen Shan' my name, if you don't like change it.)

1. Initialize the Network Monitor on App Launch.

 //for monitoring network connectivity  
 [NSNetworkMonitor initializeNetworkMonitor];  

2. To Catch the Network Status implement the following Notification.

 //to catch network status.  
 [[NSNotificationCenter defaultCenter] addObserver:self  
                      selector:@selector(changeInNetworkConnection:)  
                        name:KNETWORKNOTIFIER  
                       object:nil];  

3. The Response Method is implemented as follows,

 -(void)changeInNetworkConnection:(NSNotification *)notification {  
   NSDictionary *status = [notification object];  
   if ([[status objectForKey:@"NetworkAvailable"] boolValue]) {  
     NSLog(@"The NetworkConnection StatusDidChanged : NO Connection");  
   }  
   else  {  
     if ([[status objectForKey:@"NetworkType"] isEqualToString:@"WiFi"]) {  
       NSLog(@"The NetworkConnection StatusDidChanged : wifi Connection");  
     }  
     else  {  
       NSLog(@"The NetworkConnection StatusDidChanged : wann Connection");  
     }  
   }  
 }  

Some Helper Methods

 +(BOOL)isNetWorkConnectionAvailable;  
 +(NetworkAvailable)currentNetwork;  

One Important Tip

- we get a network change notification on app launch when the network status is changed while app is in minimize stage.

Dependencies

- Need to add Apple's Reachability Classes.
- Need to add SystemConfiguration.framework.

I attached Complete code here

if you need more details feel free to contact me

thanks,
Naveen Shan

Monday 5 March 2012

Label to display NSAttributedString (Label through a Path)


Hi All,

I don't know how many of you are familiar with NSAttributedString, if we simply write a definition "Attributed String is a string that itself contains its properties to display (like color, thickness, font, style all that are need to display the string)".

So the Attributed String help the programmer to display a long string, as different paragraph (with line breaks, justification etc) with different fonts and color for different part of string.
To achieve this iOS provide a framework called "CoreText" which is made available from Xcode 3.2 onwards. But in iOS as of now we have only limited access for this framework resource because Mac framework had lot of method to read and write Attributed String from files(like doc, docx etc).

While discussing the limitation of Attributed String processing in iOS the important think we can find is there is no UI (User Interface) elements to display Attributed String. The CoreText framework provide a method to draw the string in a path, which is not as simple like a string displayed in UILabel's.

So I got up with an implementation of a custom class that will draw the given Attributed String with a specified path.

See the screen Shots,




//To Display the NSAttributedString with NSAttributedLabel,
(Here prefix NS means not 'Next Step' it just 'Naveen Shan' my name, if you don't like change it.)

1. Create a object of NSAttributedLabel set Frame and Attributed String and addSubview the label where you need to show (here is the simplicity now this class acts like UILabel to display String).

Things to be noted is,
* We need to set enough attribute to the string before we addSubview the label. It is because, as we all know the drawRect method of a view will call at the time of setting superview for that view, if we set the attributes later we need to call a setNeedsDisplay method for proper functioning.
* We need to set Background color for that view. It is because I override the drawRect method of the view so if we not set the default background color s black.

 -(void)showAttributedLabel {  
   NSString *string = @"Lorem ipsum \n dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.";  
   //normal font attributes  
   CTFontRef normalFontRef = CTFontCreateWithName((CFStringRef)@"Helvetica", 18, NULL);  
   NSDictionary* normalFontAttribute = [[NSDictionary alloc] initWithObjectsAndKeys:(__bridge id)normalFontRef,(NSString*)kCTFontAttributeName, nil];  
   //bold font attributes  
   CTFontRef boldFontRef = CTFontCreateWithName((CFStringRef)@"Helvetica-Bold", 20, NULL);  
   NSDictionary* boldFontAttribute = [[NSDictionary alloc] initWithObjectsAndKeys:(__bridge id)boldFontRef,(NSString*)kCTFontAttributeName, nil];  
   NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:string];  
   //setting attributes.  
   [attributedString addAttributes:normalFontAttribute range:NSMakeRange(0, [attributedString length])];  
   [attributedString addAttributes:boldFontAttribute range:NSMakeRange(0, 11)];  
   NSAttributedLabel *objAttributedLabel = [[NSAttributedLabel alloc] init];  
   [objAttributedLabel setFrame:CGRectMake(50, 50, (self.view.frame.size.width/2), 500)];  
   [objAttributedLabel setBackgroundColor:[UIColor clearColor]];  
   [objAttributedLabel setAttributedString:attributedString];  
   [self.view addSubview:objAttributedLabel];  
   [objAttributedLabel release];  
   objAttributedLabel = nil;  
   string = nil;  
   [boldFontAttribute release];  
   boldFontAttribute = nil;  
   [normalFontAttribute release];  
   normalFontAttribute = nil;  
 }  

Some More Interesting Abilities of NSAttributedLabel,
Here I provide a flexibility of displaying string in columns(in vertical span).

First I like to tell how the NSAttributedLabel can be used just as a Label to display Attributed String
Set the KCOLUMNCOUNT in NSAttributedLabel.h as 0 .

if you need more than one column you need to set the required column count in KCOLUMNCOUNT at NSAttributedLabel.h
and specify the rect for each column in '-(CGRect)rectForColumnAtIndex:(int)index' method at NSAttributedLabel.m

One Important Tip
The Attributed String is drawn from its end point and from end of the path to start of the path. so if the specified path rect is not enough to show the string then the top (start) of the string is missing to draw (not the end). Similarly if you try to put some extra space at the bottom of path to hold lengthy string, I think its not possible.
To Sole this problem I can only be able to provide a function that will return the length of string that the given frame can accumulate, here it is,

 - (int)endIndexOfAttributtedString:(NSMutableAttributedString *)attributtedString InFrame:(CGRect)rect {  
   int currentIndex = 0; //now I map it from string beginning.  
   if (attributtedString != nil)  {  
     CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString((__bridge CFAttributedStringRef)attributtedString);  
     CGMutablePathRef path = CGPathCreateMutable();  
     CGPathAddRect(path, NULL, rect);  
     CTFrameRef frame = CTFramesetterCreateFrame(framesetter, CFRangeMake(currentIndex, 0), path, NULL);  
     CFRange frameRange = CTFrameGetVisibleStringRange(frame);  
     currentIndex += frameRange.length;  
     CFRelease(frame);  
     CFRelease(path);  
     CFRelease(framesetter);  
   }  
   return currentIndex;  
 }  

I attached Complete code here

if you need more details feel free to contact me

thanks,
Naveen Shan

Friday 3 February 2012

Arrowed Title View


Hi All,

I Found that in lot of application specifically in case of iPad with complex view design sometimes we use arrow to a specific location to pointing out the user what to do, most developers use image(arrowed image) for this purpose with a label over it. But I think if we use a subclass of UIView is less weight, so I go through the drawing tutorials and create my on view. I use it in my two different projects.

In this example code I attached herewith shows the arrow at bottom left corner, but any one can change the location very easily.

Now (while posting this blog), I think it is better to had the view in such a way that the arrow position is changeable from external interface, But now I don't have time so I will post another blog or update this blog later for this. (I mean I try to implement Arrowed title view with arrow can be at any location around the view like the UIPopover arrow setting).

See the screen Shots,




//To Display the Arrowed Title View named as NSArrowedTitleView
(Here prefix NS means not 'Next Step' it just 'Naveen Shan' my name, if you don't like change it.)

 -(void)showTitleView {  
   NSArrowedTitleView *objTitleView = [[NSArrowedTitleView alloc] initWithFrame:CGRectMake(0, 20, (self.view.frame.size.width), 40)];  
   objTitleView.viewColor = [UIColor grayColor];  
   objTitleView.viewTitle = @"NSArrowed Title View";  
   [objTitleView setNeedsDisplay];  
   [self.view addSubview:objTitleView];  
   [objTitleView release];  
   objTitleView = nil;  
 }  

2. call the above method like as follows,

 [self showTitleView];  

I attached complete code here

if you need more details feel free to contact me

thanks,
Naveen Shan

Tuesday 31 January 2012

Customized UIAlertView to Input Credential


Hi All,

I Found lot of UIAlertView customization but I cannot able to find a nice one to take credential input, I need such alert view for an NTLM authentication for a web service I called in my recent project. It is mainly due to in browser itself NTLM credential is input using a alert popup and my customer also requires it.
so I search over google and I got lot of samples but no one matches some of my requirement so I write my own customized UIAlertView with following specialities,

1. It support multiple line for title and message and it will 'word' wrapped the text.
2. As in case of ordinary UIAlertView if message is too big it will add to a UITextView instead of a label, I overcome it.
3. Set the frame of AlertView as per the text length.
4. Label size is align with Text (Word wrapping of text with correct alignment to top left).
5. Support for iOS3.2 and above including iOS5.0.
6. Handle proper display and dismiss of keyboard with Next action.
7. Support for iPhone and iPad with orientation.

See the following Screen Shots,






//To Display the Customized UIAlertview named as NSAuthAlertView
(Here prefix NS means not 'Next Step' it just 'Naveen Shan' my name, if you don't like change it.)

1. Paste this function in the class you wish to call,

 + (void)showCredentialInputAlert:(NSString *)alertTitle message:(NSString *)strAlertMessage delegateObject:(id)delegate viewTag:(int)iTag  {  
   NSAuthAlertView *alertView;  
   @try {  
     alertView=[[NSAuthAlertView alloc] initWithTitle:alertTitle message:strAlertMessage delegate:delegate viewTag:iTag];  
     [alertView show];  
     [alertView release];  
     alertView  =  nil;  
   }  
   @catch (NSException * exception) {  
     NSLog(@"Exception on showCredentialInputAlert : %@",[exception description]);  
   }  
   @finally {  
     [alertView release];  
     alertView  =  nil;  
   }  
 }  

2. call the above method like as follows,

 [[self class] showCredentialInputAlert:@"NSAuthAlertView" message:[NSString stringWithFormat:@"Enter Credentials for Login"] delegateObject:self   
 viewTag:KAUTHENTICATEALERTTAG];  

3. As a best practice you need to define tag value,
 #define KAUTHENTICATEALERTTAG 12131415  

//To catch the response (Username and Password) form NSAuthAlertView

1. Paste the following alert view delegate,

 #pragma mark - UIAlertView delegate  
 - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {  
   if (alertView.tag == KAUTHENTICATEALERTTAG) {  
     if (buttonIndex == 1)  {  
       NSString *username = ((UITextField *)[alertView viewWithTag:(KAUTHENTICATEALERTTAG + 1)]).text;  
       NSString *password = ((UITextField *)[alertView viewWithTag:(KAUTHENTICATEALERTTAG + 2)]).text;  
       if (username && password)  {  
         //validation of credential is done here.  
         //[self validCredential];  
       }  
       else     {  
         //empty credential.  
       }  
     }  
     else if (buttonIndex == 0) {  
     }  
   }  
 }  

I attached complete code here

if you need more details feel free to contact me

thanks,
Naveen Shan