Development‎ > ‎iOS‎ > ‎

Code Snippets

Code Snippets

This page contains usual daily copy-paste codes - you may able to get rid of goto, but you cannot fully eliminate the copy-paste method. Trust me: see the code below and you will find that copy-paste is already the best method. And no, I DON'T using Xcode to write codes. I'm using VIM without code completion :) 

BTW the indent is not correct, beware.

Macros

// #define MQ_ENABLE_CATEGORY_NAVIGATIONBAR_BACKGROUND
// #define MQ_ENABLE_CATEGORY_NAVIGATIONCONTROLLER_ROTATE
// #define MQ_ENABLE_CATEGORY_NAVIGATIONCONTROLLER_ROTATEPHONE
// #define MQ_ENABLE_CATEGORY_VIEWCONTROLLER_ROTATE // XXX: study needed
// #define MQ_ENABLE_SDK_FACEBOOK // XXX: replaced by SHK
// #define MQ_ENABLE_APP_STORE
// #define MQ_ENABLE_APP_ADS
// #define MQ_ENABLE_MUSICKIT
// #define MQ_ENABLE_MAPKIT

Initialization

This part covers common init routines.

Settings


// init settings
if ([@"setting_place" setting_string] == nil)
[@"setting_place" setting_string:@"Beijing"];

Loading & Cache


// init foundation
[MQLoading set_theme:@"national motto"];
[[MQLoading shared] setNav:nav];
[MQCache load_all];

MiniApps


// init miniapps
miniapps = [[MQMiniAppsController alloc] init];
miniapps.nav = nav;

TableProvider


provider_app = [[MQTableViewProvider alloc] initWithTableView:table_app];
provider_app.delegate = self;
[provider_app apply_theme_color:[UIColor whiteColor] on:[UIColor blackColor]];
[provider_app.texts add_array:
@"Flashlight",
@"Mirror",
nil];
[provider_app.images add_array:
@"color001.png",
@"color002.png",
nil];
[table_app reloadData];

LocationManager


// init location
CLLocationManager* locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
[locationManager startUpdatingLocation];

GameCenter


// init game center
gc = [[MQGCController alloc] initWithDelegate:self];
gc.nav = nav;

ShareKit


// init sharekit
[SHK flushOfflineQueue];

Delegates

Header

<UINavigationControllerDelegate,
    UITabBarControllerDelegate,
    UIWebViewDelegate,
    CLLocationManagerDelegate>

Implementation

#pragma mark delegates

ImagePicker


- (void)imagePickerController:(UIImagePickerController *)a_picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
}

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)a_picker
{
}

LocationManager


- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)new fromLocation:(CLLocation *)old
{
    //  NSLog(@"location updated: %@", new);
    [MQCache set:new key:@"current_location"];
}

NavigationController


- (void)navigationController:(UINavigationController *)nav willShowViewController:(UIViewController *)controller animated:(BOOL)animated
{
    //  disable the "edit" button in the "more" tab
    if (tab.moreNavigationController == nav)
        nav.navigationBar.topItem.rightBarButtonItem = nil;
}

TabBarController


- (void)tabBarController:(UITabBarController *)tab didSelectViewController:(UIViewController *)controller
{
}

TableView


- (void)tableView:(UITableView *)table didSelectRowAtIndexPath:(NSIndexPath *)path
{
}

WebView


- (void)webViewDidFinishLoad:(UIWebView *)webView
{
}

- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
    NSLog(@"DELEGATE web error: %@", error);
}

AppKit

Import: MQAppKit/MQAppController, MQAppKit/MQAppDelegate

Header


AppDelegate


@interface directoryAppDelegate: MQAppDelegate <UIApplicationDelegate> {
UIWindow* window;
AppLocalDirectory* app;
}
...

AppController


@interface AppLocalDirectory: MQAppController
{
}
- (void)load;
@end

Implementation


AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    
[self app_init];
app = [[AppLocalDirectory alloc] initWithApp:self nib:@"AppLocalDirectory"];
[self app_load:app with:@"load"];

[window addSubview:self.nav.view];
[self.window makeKeyAndVisible];

return YES;
}

- (void)applicationWillResignActive:(UIApplication *)application {
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
}

- (void)applicationWillTerminate:(UIApplication *)application {
}

#pragma mark -
#pragma mark Memory management

- (void)applicationDidReceiveMemoryWarning:(UIApplication *)application {
NSLog(@"memory warning - but app should not be released, force retained");
[app retain];
}

- (void)dealloc {
[app release];
[window release];
[super dealloc];
}

@end

AppController


@implementation AppLocalDirectory

- (void)load
{
}

@end

Game Center

Import: MQAppKit/MQGCController

Initialization


// init
gc = [[MQGCController alloc] initWithDelegate:self];
gc.nav = nav;
// ...

Delegate


#pragma mark game center delegate

- (void)gamecenter_authentication_changed
{
NSLog(@"gc status changed: %i", gc.is_authenticated);
if (gc.is_authenticated)
{
NSLog(@"player: %@", [GKLocalPlayer localPlayer]);
[self change_profile:[GKLocalPlayer localPlayer].alias];
}
}

Methods


[gc report_score:score leaderboard:@"org.superarts.myapp.gc.lb.total_points"];
[gc report_percent:100 achievement:@"org.superarts.myapp.gc.ac.defeat_leo"];

[gc show_leaderboard];
[gc show_achievement];

SHK

ShareKit is a great library for general text / URL / image sharing.


- (IBAction)action_share_text
{
        SHKItem*        item = [SHKItem text:text_share.text];
        SHKActionSheet *actionSheet = [SHKActionSheet actionSheetForItem:item];
        [actionSheet showFromToolbar:toolbar_share];
}

- (IBAction)action_share_url
{
        NSURL*          url = [NSURL URLWithString:@"http://soft.superarts.org"];
        SHKItem*        item = [SHKItem URL:url title:text_share.text];
        SHKActionSheet *actionSheet = [SHKActionSheet actionSheetForItem:item];
        [actionSheet showFromToolbar:toolbar_share];
}

- (IBAction)action_share_image
{
        SHKItem*        item = [SHKItem image:image_share.image title:text_share.text];
        SHKActionSheet *actionSheet = [SHKActionSheet actionSheetForItem:item];
        [actionSheet showFromToolbar:toolbar_share];
}

Subpages (1): Table
Comments