Archive for the ‘Objective C’ Category

11
May

If it is your root view controller, and you have access to your app delegate, you should be able to access your controller like this:

appDelegate.window.rootViewController

,

10
May

Include the SystemConfiguration.framework in your project.

Make some imports

#import <sys/socket.h>
#import <netinet/in.h>
#import <SystemConfiguration/SystemConfiguration.h>

Now just call this function

/*
Connectivity testing code pulled from Apple's Reachability Example: http://developer.apple.com/library/ios/#samplecode/Reachability
 */
+(BOOL)hasConnectivity {
    struct sockaddr_in zeroAddress;
    bzero(&zeroAddress, sizeof(zeroAddress));
    zeroAddress.sin_len = sizeof(zeroAddress);
    zeroAddress.sin_family = AF_INET;

    SCNetworkReachabilityRef reachability = SCNetworkReachabilityCreateWithAddress(kCFAllocatorDefault, (const struct sockaddr*)&zeroAddress);
    if(reachability != NULL) {
        //NetworkStatus retVal = NotReachable;
        SCNetworkReachabilityFlags flags;
        if (SCNetworkReachabilityGetFlags(reachability, &flags)) {
            if ((flags & kSCNetworkReachabilityFlagsReachable) == 0)
            {
                // if target host is not reachable
                return NO;
            }

            if ((flags & kSCNetworkReachabilityFlagsConnectionRequired) == 0)
            {
                // if target host is reachable and no connection is required
                //  then we'll assume (for now) that your on Wi-Fi
                return YES;
            }

            if ((((flags & kSCNetworkReachabilityFlagsConnectionOnDemand ) != 0) ||
                 (flags & kSCNetworkReachabilityFlagsConnectionOnTraffic) != 0))
            {
                // ... and the connection is on-demand (or on-traffic) if the
                //     calling application is using the CFSocketStream or higher APIs

                if ((flags & kSCNetworkReachabilityFlagsInterventionRequired) == 0)
                {
                    // ... and no [user] intervention is needed
                    return YES;
                }
            }

            if ((flags & kSCNetworkReachabilityFlagsIsWWAN) == kSCNetworkReachabilityFlagsIsWWAN)
            {
                // ... but WWAN connections are OK if the calling application
                //     is using the CFNetwork (CFSocketStream?) APIs.
                return YES;
            }
        }
    }

    return NO;
}

, ,

07
May

We will need to implement the touch/drag events directly.

Check out the touchesBegan, touchesMoved etc. delegate methods in UIResponder.

One approach would be to subclass imageview and implement touchesBegan, touchesMoved in it, and use this imageview subclass to display our images in the scroll view.

On the touchesBegan create a new image view and add it to the outer view and set its image to be the same as the one in the scroll view. We need to overlay it directly over our source image in the scroll view so adjust its frame origin to be relative to the outer view we will need to use the scrollview origin and also the content view size and offset of the source image view inside the content view in order to recalculate the new origin in the outer view.

Then on the touches moved, simply readjust the frame of this image in accordance with the coordinates from the touches moved so that the image follows the touch.

Do a boundary check against the frame of our target imageview – once the user drags it into this boundary, make that target imageviews image the same as the image in the view being dragged and remove the dragged image from the containing view and release it.

04
May

In Cocos2D Android, positioning elements is a very tedious process. In most cases, the absolute position that we give will not be used as it is. You can fix this issue by setting the following properties of that element :

 

Consider a CCSprite object, mySprite. Now you have to set the following properties of that object before positioning it absolutely

 

mySprite.setAnchorPoint(0, 0);

mySprite.setRelativeAnchorPoint(true);

 

Now if you give the absolute position for the object, it will work fine.

, , , , ,

04
May

The following method will return the array with items in reverse order. The input given to this method is the NSMutableArray, the contents of which you want to reverse and the output will the NSMutableArray with the values reversed.

 

- (NSMutableArray *)reverseArray:(NSMutableArray *)arraytoReverse {

if ([arraytoReverse count] == 0)

return arraytoReverse; // Return from the method if the array is empty.

NSUInteger start = 0; // Setting the initial counter.

NSUInteger end = [arraytoReverse count] - 1; // Setting the final counter.

while (start < end) { // Iterating through the contents of the array.

[arraytoReverse exchangeObjectAtIndex: start withObjectAtIndex: end];

start ++;

end--;

}

return arraytoReverse; // Returning the array with values reversed.

}

,

30
Apr

In cocos2d x for windows ccmenu is almost same as that in normal cocos2d.

Here is a sample code. We can add two items in the menu:menuItem1 and menuItem2.

CCMenuItemSprite menuItem1 = CCMenuItemSprite.itemFromNormalSprite(in1, in2, this, new SEL_MenuHandler(informationAction));

CCMenuItemSprite menuItem2 = CCMenuItemSprite.itemFromNormalSprite(in3, in4, this, new SEL_MenuHandler(buttonAction));

In the above code, in1 and in3 are normal sprites and in2 and in4 are selected sprite.Next parameter this is the target. and buttonAction and the informationAction are touch functions.

CCMenu menu1 = CCMenu.menuWithItems(menuItem1, menuItem2);

Now we create the ccmenu menu1 with the above menuItems.

 

 

 

28
Apr

The icon on the home screen can show a number, or badge, on the icon.

Set the icon badge number:

[UIApplication sharedApplication].applicationIconBadgeNumber = 10;

Remove Icon Badge Number:

[UIApplication sharedApplication].applicationIconBadgeNumber = 0

28
Apr

Sometimes you wish to transfer control from your application to another application.

Dial the phone (your app ends and the phone dialer begins):

// Dial the phone
NSString *phoneToCall = @"tel: 123-456-7890";
NSString *phoneToCallEncoded = [phoneToCall stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding];
NSURL *url = [[NSURL alloc] initWithString:phoneToCallEncoded];
[[UIApplication sharedApplication] openURL:url];

Transfer to a web page (your app ends and the web app begins):

// Access web page
NSURL *url = [[NSURL alloc] initWithString:@"http://servin.com"];
[[UIApplication sharedApplication] openURL:url];

28
Apr

The status bar takes up 20 pixels at the very top of the screen, showing cell phone signal strength, network activity, time, and other info.

Get status:

BOOL result;
result = [UIApplication sharedApplication].statusBarHidden;

Hide status bar:

[UIApplication sharedApplication].statusBarHidden = YES;

Show status bar:

[UIApplication sharedApplication].statusBarHidden = NO;

,

28
Apr

When enabled, the idle timer puts your iPhone or iPod Touch into sleep mode after a given amount of non-use.

When disabled, the idle timer will not put your device to sleep. You sometimes want this, based on the type of iPhone application you are running.

Get current value:

BOOL result;
result = [UIApplication sharedApplication].idleTimerDisabled;

Timer Disabled:

[UIApplication sharedApplication].idleTimerDisabled = YES;

Timer Enabled:

[UIApplication sharedApplication].idleTimerDisabled = NO;

 

,