Personal Project

Showing posts with label iOS App. Show all posts
Showing posts with label iOS App. Show all posts

Monday, April 11, 2016

How to cache UIWebView webpage for offline viewing ?

There is an issue when invoking UIWebView API on iOS that webpage won`t be displayed 
immediately.  It will take around 2 seconds to load the html page from the server and display
it properly. 
In order to optimize the UI, i suggest that  preloading the webpage in cache and it could be displayed for offline viewing at anytime.  

This solution has integrated UnityAds SDK for displaying video ad and the source codes can be seen at github.


Sunday, October 13, 2013

PROTOCOLS


In Objective C, an object cannot inherit from multiple superclasses, yet it can conform to multiple protocols.  If you need a GameCenterManageDelegate to be inherited by many Views, you can define as shown below.

GameCenterManagerDelegate.h
@protocol GameCenterManagerDelegate <NSObject>
@optional
- (void) processGameCenterAuth: (NSError*) error;
@end

GameCenterManagerDelegate.m
@implementation GameCenterManager
// ... 
@end


GameViewController.h
@interface GameViewController : ViewController <GameCenterManagerDelegate> 


@end

How do CATEGORIES EXTEND CLASSES ?



Categories are an elegant way to extend any Objective C class without changing original codes. Take a peek at StoreKit framework for example, you can extend SKProduct.h in StoreKit framework as shown below.


SKProduct+ priceWithString.h

#import <Foundation/Foundation.h>
#import <StoreKit/StoreKit.h>

@interface SKProduct (priceWithString)                                  // priceAsString : category name
@property (nonatomic, readonly) NSString *priceAsString;      // Extended property    
-(void) printPrice;                                                                         // Extended function


@end


SKProduct+ priceWithString.m

#import "SKProduct+priceAsString.h"

@implementation SKProduct (priceAsString)

- (NSString *) priceAsString
{
    NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];

    [formatter setLocale:[self priceLocale]];
    
    NSString *str = [formatter stringFromNumber:[self price]];
    [formatter release];
    return str;
}

-(void) printPrice  {    // Extended function

}

@end

Rules to avoid retain cycles


1. Child view cannot retain parent”s view property.

2. Child view uses weak property pointing to parents.

3. Remove subview → release or close all relationship related to parents.


Good Answer 
ref: 
http://www.cocoawithlove.com/2009/07/rules-to-avoid-retain-cycles.html


Why do you write "assign" in a property declaration on an Objective-C class ?


1. assign / unsafe_unretained (no memory management shall be done with this property, it is handled manually by the person assigning the value) (default if not ARC or object type).

2. Assign is used for C primitive property to replace a variable with new values.




Property :
  • nonatomic (does not enforce thread safety on the property, mainly for use when only one thread shall be used throughout a program)
  • atomic (enforces thread safety on the property, mainly for use when multiple threads shall be used throughout a program) (default)
  • retain / strong (automatically retains / releases values on set, makes sure values do not deallocate unexpectedly) (default if ARC and object type)
  • readonly (cannot set property)
  • readwrite (can both set and get property) (default)
  • assign / unsafe_unretained (no memory management shall be done with this property, it is handled manually by the person assigning the value) (default if not ARC or object type)
  • copy (copies the object before setting it, in cases where the value set must not change due to external factors (strings, arrays, etc).
  • weak (automatically zeroes the reference should the object be deallocated, and does not retain the value passed in)


Good ref: 
http://rdcworld-iphone.blogspot.in/2012/12/variable-property-attributes-or.html


Friday, October 4, 2013

How to solve iOS 7 status bar overlapping problem ?


Although there are lots of solutions to this problem on the internet,  this is the most easiest way to make it work.

Solution:  Add the following codes in your View Controller.m

-(BOOL) prefersStatusBarHidden
{
   return YES;


}

Before: 
iOS 7 status bar overlaps background image as shown below.


















After : iOS 7 status bar is hidden.