Ios and http requests

You can either use NSURLRequest or NSMutableURLRequest
the difference is that with NSMutableURLRequest you can change the url the method etc.

I will use the NSMutableURLRequest

Supposing you have a class implementation


#import "UrlFetcher.h"

@implementation UrlFetcher

-(void)setUrl:(NSString *)url
{
    twitterUrl = [NSURL URLWithString:url];
}

-(void)fetchandParse
{
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:twitterUrl];
    [request setHTTPMethod:@"GET"];
    [[NSURLConnection alloc] initWithRequest:request delegate:self];    
}

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
    NSLog(@"didReceiveResponse");
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    NSLog(@"got data");
    NSString *result = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    NSLog(result);
}

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

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
}
@end

Since you set as the delegate the class where the method is called you need to implement the
methods

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

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

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

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

Ios + JSON = NSJSONSerialization

Ios 5 provides you with a cool library for json parsing without having to search for
third party libraries.

Here is an example of how to use the NSJSONSerialization class, in order to parse or do some json serialization

//
//  JsonTest.h
//  JsonTest
//
//  Created by gkatzioura on 1/14/13.
//  Copyright (c) 2013 Gkatziouras Emmanouil. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface JsonTest : NSObject
-(NSString*)getjSon;
-(void)parseJson:(NSString*)jsonString;
@end
//
//  JsonTest.m
//  JsonTest
//
//  Created by gkatzioura on 1/14/13.
//  Copyright (c) 2013 Gkatziouras Emmanouil. All rights reserved.
//

#import "JsonTest.h"

@implementation JsonTest
-(NSString*)getjSon {

    NSMutableDictionary* mineDictionary = [[NSMutableDictionary alloc] init];

    [mineDictionary setObject:@"gkatzioura" forKey:@"username"];
    [mineDictionary setObject:@"Gkatziouras" forKey:@"surname"];
    [mineDictionary setObject:@"Emmanouil" forKey:@"name"];

    NSMutableDictionary* yourDictionary = [[NSMutableDictionary alloc] init];

    [yourDictionary setObject:@"dunno" forKey:@"username"];
    [yourDictionary setObject:@"dunno" forKey:@"surname"];
    [yourDictionary setObject:@"dunno" forKey:@"name"];

    NSMutableArray* jsonArray = [[NSMutableArray alloc] init];

    [jsonArray addObject:mineDictionary];
    [jsonArray addObject:yourDictionary];

    NSData* nsdata = [NSJSONSerialization dataWithJSONObject:jsonArray options:NSJSONReadingMutableContainers error:nil];

    NSString* jsonString =[[NSString alloc] initWithData:nsdata encoding:NSUTF8StringEncoding];

    return jsonString;
}

-(void)parseJson:(NSString *)jsonString {

    NSData *jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
    NSArray *jsonArray = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:nil];

    for(NSDictionary* account in jsonArray) {

        NSLog([account objectForKey:@"username"]);
        NSLog([account objectForKey:@"surname"]);
        NSLog([account objectForKey:@"name"]);
    }

}
@end

And you just do

#import "JsonTest.h"

.
.
.
.

JsonTest* jsonTest = [[JsonTest alloc] init];

NSString* wut = [jsonTest getjSon];

//NSLog(wut);

[jsonTest parseJson:wut];

NSJSONSerialization reference

Ios Phonegap Simple Plugin

You have a Phonegap project, up and running and you want to create a simple plugin.

On your Plugins directory create two files

SimplePlugin.h which contains these lines

#import <Cordova/CDVPlugin.h>
@interface SimplePlugin : CDVPlugin
-(void)sayHello:(CDVInvokedUrlCommand*)command;
@end

and SimplePlugin.c which contains these lines

#import "SimplePlugin.h"

/**
* Gkatziouras Emmanouil 
* Simple phonegap Plugin
*/

@implementation SimplePlugin

-(void)sayHello:(CDVInvokedUrlCommand *)command {
    
    CDVPluginResult* pluginResult = nil;
    NSString* javascript = nil;
   
    //You can choose other statuses for the result such as CDVCommandStatus_ERROR CDVCommandStatus_NO_RESULT and more
    pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:@"Hello From Phonegap"];
    
    javascript = [pluginResult toSuccessCallbackString:command.callbackId];
    [self writeJavascript:javascript];
}
@end

Now on your javascript file you can call the native code through the Cordova.exec function

Cordova.exec(
                 function(success_parameters) {alert(success_parameters);},
                 function(error_parameters){alert(error_parameters)},
                 "SimplePlugin",
                 "sayHello",
                 ["noparameters"]
                 );

The bad news is that you only have the choice to make async calls to the native code you created.
The first parameter of the function is when the plugin is succesfully called, this means that you used the CDVCommandStatus_OK to send a result. The second parameter is a function when it comes to use the CDVCommandStatus_ERROR status on your native code. The third parameter is the plugin’s name, the forth is the function you want to call. The last parameter is the json data you want to pass to your native function.

Also don’t forget to register your plugin at the Cordova.plist file.

   <dict>
   <key>SimplePlugin</key>
   <string>SimplePlugin</string>
   </dict>