Thursday, February 16, 2012

Load a PDF File in the iPhone App Smoothly

In todays session we will learn how to launch a PDF file present in the server into our iPhone application.

For this you require the QuickLook Framework and its class called as the QLPreviewController. With the help of this class you can view documents like

a) iWork Documents
b) Microsoft Office Documents
c) RTF documents
d) PDF Files
e) Images
f) Text Files
g) CSV (Comma separated files)

For this tutorial i have just displayed PDF files, the file could be present any where in the server or in the app bundle. But since in maximum of the application the PDF files are loaded from the server i shall do the same in this demo.

Step 1: Open Xcode and create a view which looks like the one given below

Step 2: Add the Quick Look framework into your project and import it into your view controller.h file, create the object of the QLPreviewController in the table views did select row at index method and set its dataSource and delegate methods

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    //initializing the fileURL object with the URL links to be loaded
    switch (indexPath.row) {
        case 0
            fileURL = [NSURL URLWithString:kStringURLViewControllerPDF];

      case 1:
            fileURL = [NSURL URLWithString:kStringURLQLPreviewControllerPDF];
            case 2:
            fileURL = [NSURL URLWithString:kStringURLUIDocumentInteractionControllerPDF];
    //creating the object of the QLPreviewController
    QLPreviewController *previewController = [[QLPreviewController alloc] init];
    //settnig the datasource property to self
    previewController.dataSource = self;
    //pusing the QLPreviewController to the navigation stack
    [[self navigationController] pushViewController:previewController animated:YES];
    [previewController release];

Code Explanation: The maximum coding is very simple and is given in the comments i am initializing the fileURL object which is the object of NSURL class which will be returned in the QLPreviewController datasource later to load the pdf files/

Step 3: Add the QLPreviewController datasource method which will do all the hardwork for you and launch the PDF for you.

#pragma mark QLPreviewControllerDataSource

// Returns the number of items that the preview controller should preview
- (NSInteger)numberOfPreviewItemsInPreviewController:(QLPreviewController *)previewController
    return 30;

// returns the item that the preview controller should preview
- (id)previewController:(QLPreviewController *)previewController previewItemAtIndex:(NSInteger)idx
    return fileURL;

Code Explanation:

numberOfPreviewItemsInPreviewControllerInvoked when the Quick Look preview controller needs to know the number of preview items to include in the preview navigation list. (required)

previewItemAtIndex: Invoked when the Quick Look preview controller needs the preview item for a specified index position. (required)

Step 4: Add the view into the iPhone window with the help of the navigation controller, and then add the navigation controller into the iPhone window to add navigation and in case if you dont want navigation you may skip the adding the navigation controller.

Step 5: Launch the application and view the PDF's 

you may also zoom in and out and even print the PDF.

But it is advised to use the UIDocumentInteractionController but its a bit complex so i came up with this easy solution.

Download the source code from here.

Let me know in case if you have any queries regarding the same until then Happy iCoding and have a great Day.

Friday, February 10, 2012

Launch the app store app from your application

Sometimes it may happen that you would like to launch the app store containing your app details from your application. So in today’s tutorial we will be having a look at that.

For this tutorial I have just prepared a simple view with a button on it on the hit of which I will be launching the app store containing my applications details.
Note: This demo cannot be tested in the iPhone simulator for the testing of this demo you require this application to be deployed into your iOS device. (Deploy an app to the iOS device)

Step 1: Create an empty iPhone application with and add the UIViewController subclass to it.

Step 2: Create a view just like the one given below and the add the view of this view controller into the iPhone window.

Step 3: Create a function for the button and this function will contain the code for launching the app store application of the iOS device from your application. So declare a function with proper name into the your ViewController.h file and implement the same in the yourViewController.m file given below is the code for the same.

MyViewController.h file
MyViewController.m file
Code Explanation: In the above code since we want to launch the app store we have used the itms-apps: in place of http followed by your application link which is present on the app store.

Step 4: Build your project and deploy the project into the iOS device.

Step 5: After you touch the button you will see the app store app is launched with the application details who’s link you have provided.

You can download the sample project from here.

This kind of requirement is mostly used when a client requests that he wants a review section in his application. You may also use the link maker from apple to get your application url link.

In case of any queries or errors you may drop me a mail or ask your queries via comments, until then

Happy iCoding and have a great Day.

Friday, February 3, 2012

Using The Twitter Framework in iOS 5

The twitter framework apple has added is a Christmas gift to all the iOS developers as twitter integration has now become a child's play. By using the twitter framework you may now post your stuff to twitter but a user only have to connect to Twitter with his account once and he/she can do this in the setting section of the iPhone or iPod and look for the twitter app settings. After that, apps have the possibility to post a tweet with the users account.

TWTweetComposeViewController class  in the twitter framework makes every thing possible if you want to tweet in the iOS 5 but if you want the twitter integration in the other devices then in that case you have to use the old methods of tweeting and in case if your application has support from iOS 4 to iOS 5 then in that case you need to perform a check and then tweet happily ever after . 

In this tutorial I’m going to show you how to use this class and start using the Twitter integration. You will be surprised how easy this class is to implement. Check the end of this article for a sample project you can download.

Step 1: Open Xcode and create an empty project add the ViewController subclass file in the project with Xib and create a view which looks something like the one given below.

Step 2: Select the .h file of your viewcontroller and create a functions to connect to the button, an IBOutlet to connect to the text field and one alert view object which will be used to notify the user regarding his tweets. Also add the twitter framework into your project, select the xcode project file and go to build phases and from their expand the link binary with library option.

Link Binary with library

Twitter Framework

 Given below is the code view for the .h file of the view controller

Step3: Select the .m file of your view controller and add the below code, i have divided the code blocks with the explanation for the same so that it will be easy for you to understand.

Code explanation: a) First we are checking whether the device supports the Twitter framework or not.

or you can also check for twitter account with the help of below code

b) Secondly we set the text for the content that we want to tweet with the help of the method setInitialText and then display the twitter composer modal view controller.

c) A user has to know whether his tweet has been posted or not so we will also check this with the help of TWTweetComposerViewControllerResult type and with the help of completionHandler property.
completionHandler: This is the handler that gets called when the user is done composing the tweet.

TWTweetComposerViewControllerResult: This is a enumerator with various values that determines whether a tweet is done successfully or has failed due to some problems like no internet connection or 3G connections etc.

Step 4: Once all the above code is understood by you and everything is working fine so far then its time you connected your IBOutlets and IBAction and then move to the AppDelegate.m file where you will add the twitter view into the iPhone window in the application did finish launching method.

Step 5: Run the application and start tweeting.

In case if their are no twitter account configured in your simulator you will get the below screen

You can download the code snipped here.

In case if you get some error which says

"_OBJC_CLASS_$ACAccountStore" referenced from objc-class-ref from TwitterViewController.o 

Then kindly add the accounts framework to fix it

You may also post images and URL Links with the help of the methods in the TWTweetComposeViewController

Image:  [twitterComposer addImage:SomeImagePresent_InBundleor_Camera];

URL: [twitterComposer addURL:someURL];

If you have any queries or if you think that i have missed out any important point then let me know via comments or mails, until then Happy iCoding and have a great Day.