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);
}



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