adsense

2011年6月28日星期二

check connection

之前有文章講用 NSURLCONNECTION 既 TIMEOUT 用來 CHECK 部 IPHONE 係未 CONNECTED

其實本身 APPLE 都出左個 CLASS,用來 CHECK
1. connected
2. wifi
3. mobile network connection

上網或係 XCODE 搵 reachability ,可以係 APPLE DOWNLOAD

個 PACKAGE 有 reachability.m , 同埋 reachability.h

要用個陣 抄入去係個 PROJECT 入边

另外要加個 framework 叫 SystemConfiguration.framework


以下係 EXAMPLE

我用黎係未 CONNECTED 可以參考 。。或者可以參考 reachability.m

TestConnection.h

#import

@class Reachability;

@interface TestConnectionViewController : UIViewController {


Reachability* internetReach;


}

@end


TestConnection.m


- (BOOL)checkInternetConnection{

BOOL connected;

internetReach = [[Reachability reachabilityForInternetConnection] retain];
[internetReach startNotifier];
NetworkStatus netStatus = [internetReach currentReachabilityStatus];

if (netStatus == NotReachable) {
NSLog(@"Internet Connection Fail");
connected = NO;
}else {
NSLog(@"Internet Connection Success");
connected = YES;
}
return connected;
}


//Called by Reachability whenever status changes.
- (void) reachabilityChanged: (NSNotification* )note
{
Reachability* curReach = [note object];
NSParameterAssert([curReach isKindOfClass: [Reachability class]]);
BOOL internetConnection = [self checkInternetConnection];

if (internetConnection){
NSLog(@"connected");
}else {
NSLog(@"not connected");
}
}



// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];


[[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(reachabilityChanged:) name: kReachabilityChangedNotification object: nil];



BOOL internetConnection = [self checkInternetConnection];

if (internetConnection){
NSLog(@"connected");
}else {
NSLog(@"not connected");
}


}

2011年6月8日星期三

method ? function?

我係 IPHONE 新手, 寫開 PHP , 開始見到 IPHONE D METHOD 真係睇到睇花 , 下边有個例子。。希望幫下好似我咁新手既人


-(int)add : (int)first addto :(int) second{
int ans;
ans = first + second;
return ans;
}


- (void)viewDidLoad {
    [super viewDidLoad];
int ans1;
int a = 10;
int b = 11
ans1 = [ self add:a addto:b];
NSLog(@"%d",ans1);
}



好簡單, 就係將兩個數加埋, 

2011年5月31日星期二

uiwebview 用法

此摘要無法顯示。請 按一下這裡以查看文章。

udid

上次講到 UDID , 

下边就係獲取機中 UDID , 呢個 ID 唔同, 所以當有 ACTION 行, 你可以經 WEB PASS 個 UDID 去你自已個 SERVER , 咁就可以數住個 USER 用左幾多次, 就算係洗左個 PROGRAM 再裝番都無用


NSString *udid = [[UIDevice currentDevice] uniqueIdentifier];
NSLog(@"%@",udid);

2011年5月29日星期日





















如果想 USER 用特定次數便提示用家比 “錢” 買 完整版 , 可以用呢個方法

本身 APPS 有個叫nsuserdefault 可以儲存資料, 例如APPS 登入資料, 用過幾次等

今次呢個 APPS 如果用家用過一次便會收 nsuserdefault 既 counter +1 , 除左儲 integer , 可以儲 string , 什至 array 都得



係你個 ACTION 個度加入。


int calCounter = [[NSUserDefaults standardUserDefaults] integerForKey:@"counter"];
calCounter +=1;
[[NSUserDefaults standardUserDefaults] setInteger:calCounter forKey:@"counter"];
[[NSUserDefaults standardUserDefaults] synchronize];
calCounter = [[NSUserDefaults standardUserDefaults] integerForKey:@"counter"];
NSLog(@"new counter %d",calCounter);




最後個兩句係比 CONSOLE 睇結果, 加唔加無乜所謂


係每次行 ACTION 前后

加上



calcounter%10 == 0 , 即係每 10 次問一問 , 然后加入一個 FUNCTION




calCounter = [[NSUserDefaults standardUserDefaults] integerForKey:@"counter"];
if ( calCounter%10 == 0 && calCounter >1){
NSString *alertMessage = [NSString stringWithFormat:@"%d times calculation has been made",calCounter];
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:@"Go Get Full Version"
message:alertMessage
delegate:self cancelButtonTitle:@"Ignore"
otherButtonTitles:@"Buy Full Version",nil];
[alert show];
[alert release];
}