Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Reachability] Fix startup crash + update sample
- Loading branch information
Oleg Demchenko
authored and
Oleg Demchenko
committed
Sep 10, 2015
1 parent
1b316ce
commit d75b9c3
Showing
12 changed files
with
535 additions
and
673 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,170 +1,10 @@ | ||
using System; | ||
using CoreGraphics; | ||
using Foundation; | ||
using Foundation; | ||
using UIKit; | ||
|
||
namespace reachability | ||
{ | ||
public partial class ReachabilityAppDelegate : UIApplicationDelegate | ||
{ | ||
UITableView tableView; | ||
NetworkStatus remoteHostStatus, internetStatus, localWifiStatus; | ||
|
||
static void Main (string[] args) | ||
{ | ||
UIApplication.Main (args, null, (string)null); | ||
} | ||
|
||
public override bool FinishedLaunching (UIApplication application, NSDictionary launchOptions) | ||
{ | ||
AddTable (); | ||
UpdateStatus (); | ||
Reachability.ReachabilityChanged += (object sender, EventArgs e) => { | ||
UpdateStatus (); | ||
}; | ||
|
||
window.MakeKeyAndVisible (); | ||
|
||
return true; | ||
} | ||
|
||
void UpdateStatus () | ||
{ | ||
remoteHostStatus = Reachability.RemoteHostStatus (); | ||
internetStatus = Reachability.InternetConnectionStatus (); | ||
localWifiStatus = Reachability.LocalWifiConnectionStatus (); | ||
tableView.ReloadData (); | ||
} | ||
|
||
void AddTable () | ||
{ | ||
CGRect tableFrame = UIScreen.MainScreen.ApplicationFrame; | ||
|
||
tableView = new UITableView (tableFrame, UITableViewStyle.Grouped) { | ||
AutoresizingMask = UIViewAutoresizing.FlexibleWidth | UIViewAutoresizing.FlexibleHeight, | ||
RowHeight = 44.0f, | ||
SeparatorStyle = UITableViewCellSeparatorStyle.None, | ||
SectionHeaderHeight = 28.0f, | ||
ScrollEnabled = false, | ||
|
||
Source = new DataSource (this), | ||
}; | ||
|
||
contentView.InsertSubviewBelow (tableView, summaryLabel); | ||
contentView.BringSubviewToFront (summaryLabel); | ||
tableView.ReloadData (); | ||
} | ||
|
||
class DataSource : UITableViewSource | ||
{ | ||
ReachabilityAppDelegate parent; | ||
UIImage imageCarrier, imageWiFi, imageStop; | ||
|
||
public DataSource (ReachabilityAppDelegate parent) | ||
{ | ||
imageCarrier = UIImage.FromFile ("WWAN5.png"); | ||
imageWiFi = UIImage.FromFile ("Airport.png"); | ||
imageStop = UIImage.FromFile ("stop-32.png"); | ||
|
||
this.parent = parent; | ||
} | ||
|
||
public override NSIndexPath WillSelectRow (UITableView view, NSIndexPath index) | ||
{ | ||
return null; | ||
} | ||
|
||
public override nint RowsInSection (UITableView view, nint section) | ||
{ | ||
return 1; | ||
} | ||
|
||
public override nint NumberOfSections (UITableView view) | ||
{ | ||
return 3; | ||
} | ||
|
||
public override string TitleForHeader (UITableView view, nint section) | ||
{ | ||
switch (section) { | ||
case 0: | ||
return Reachability.HostName; | ||
case 1: | ||
return "Access to internet hosts"; | ||
case 2: | ||
return "Access to Local Bonjour Hosts"; | ||
default: | ||
return "Unknown"; | ||
} | ||
} | ||
|
||
static NSString ReachabilityTableCellIdentifier = new NSString ("ReachabilityTableCell"); | ||
|
||
public override UITableViewCell GetCell (UITableView tableView, NSIndexPath indexPath) | ||
{ | ||
var cell = tableView.DequeueReusableCell (ReachabilityTableCellIdentifier); | ||
if (cell == null) { | ||
cell = new UITableViewCell (UITableViewCellStyle.Default, ReachabilityTableCellIdentifier); | ||
var label = cell.TextLabel; | ||
label.Font = UIFont.SystemFontOfSize (12f); | ||
label.TextColor = UIColor.DarkGray; | ||
label.TextAlignment = UITextAlignment.Left; | ||
} | ||
|
||
var row = indexPath.Row; | ||
string text = ""; | ||
UIImage image = null; | ||
switch (indexPath.Section) { | ||
case 0: | ||
switch (parent.remoteHostStatus) { | ||
case NetworkStatus.NotReachable: | ||
text = "Cannot connect to remote host"; | ||
image = imageStop; | ||
break; | ||
case NetworkStatus.ReachableViaCarrierDataNetwork: | ||
text = "Reachable via data carrier network"; | ||
image = imageCarrier; | ||
break; | ||
case NetworkStatus.ReachableViaWiFiNetwork: | ||
text = "Reachable via WiFi network"; | ||
image = imageWiFi; | ||
break; | ||
} | ||
break; | ||
case 1: | ||
switch (parent.internetStatus) { | ||
case NetworkStatus.NotReachable: | ||
text = "Access not available"; | ||
image = imageStop; | ||
break; | ||
case NetworkStatus.ReachableViaCarrierDataNetwork: | ||
text = "Available via data carrier network"; | ||
image = imageCarrier; | ||
break; | ||
case NetworkStatus.ReachableViaWiFiNetwork: | ||
text = "Available via WiFi network"; | ||
image = imageWiFi; | ||
break; | ||
} | ||
break; | ||
case 2: | ||
switch (parent.localWifiStatus) { | ||
case NetworkStatus.NotReachable: | ||
text = "Access not available"; | ||
image = imageStop; | ||
break; | ||
case NetworkStatus.ReachableViaWiFiNetwork: | ||
text = "Available via WiFi network"; | ||
image = imageWiFi; | ||
break; | ||
} | ||
break; | ||
} | ||
cell.TextLabel.Text = text; | ||
cell.ImageView.Image = image; | ||
return cell; | ||
} | ||
} | ||
namespace Reachability { | ||
[Register ("AppDelegate")] | ||
public class AppDelegate : UIApplicationDelegate { | ||
public override UIWindow Window { get; set; } | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
using UIKit; | ||
|
||
namespace Reachability { | ||
public class Application { | ||
// This is the main entry point of the application. | ||
static void Main (string[] args) | ||
{ | ||
// if you want to use a different Application Delegate class from "AppDelegate" | ||
// you can specify it here. | ||
UIApplication.Main (args, null, "AppDelegate"); | ||
} | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
<?xml version="1.0" encoding="UTF-8" standalone="no"?> | ||
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="8187.4" systemVersion="15A178w" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" initialViewController="G6t-Sy-zaY"> | ||
<dependencies> | ||
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="8151.3"/> | ||
</dependencies> | ||
<scenes> | ||
<!--Root View Controller--> | ||
<scene sceneID="AaP-65-3tI"> | ||
<objects> | ||
<tableViewController id="uYg-Wb-NE8" customClass="MainViewController" sceneMemberID="viewController"> | ||
<tableView key="view" clipsSubviews="YES" contentMode="scaleToFill" alwaysBounceVertical="YES" dataMode="prototypes" style="plain" separatorStyle="default" rowHeight="44" sectionHeaderHeight="28" sectionFooterHeight="28" id="bpy-jb-hsL"> | ||
<rect key="frame" x="0.0" y="0.0" width="320" height="568"/> | ||
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/> | ||
<animations/> | ||
<color key="backgroundColor" white="1" alpha="1" colorSpace="calibratedWhite"/> | ||
<prototypes> | ||
<tableViewCell clipsSubviews="YES" contentMode="scaleToFill" selectionStyle="default" indentationWidth="10" id="V1L-rk-bW7"> | ||
<rect key="frame" x="0.0" y="92" width="320" height="44"/> | ||
<autoresizingMask key="autoresizingMask"/> | ||
<tableViewCellContentView key="contentView" opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center" tableViewCell="V1L-rk-bW7" id="mEc-0D-cmu"> | ||
<rect key="frame" x="0.0" y="0.0" width="320" height="43.5"/> | ||
<autoresizingMask key="autoresizingMask"/> | ||
<animations/> | ||
</tableViewCellContentView> | ||
<animations/> | ||
</tableViewCell> | ||
</prototypes> | ||
<connections> | ||
<outlet property="dataSource" destination="uYg-Wb-NE8" id="UW0-nO-B63"/> | ||
<outlet property="delegate" destination="uYg-Wb-NE8" id="SFR-fz-wuW"/> | ||
</connections> | ||
</tableView> | ||
<navigationItem key="navigationItem" title="Root View Controller" id="WD6-LY-Wq6"/> | ||
</tableViewController> | ||
<placeholder placeholderIdentifier="IBFirstResponder" id="WLN-0l-dYf" userLabel="First Responder" sceneMemberID="firstResponder"/> | ||
</objects> | ||
<point key="canvasLocation" x="748" y="134"/> | ||
</scene> | ||
<!--Navigation Controller--> | ||
<scene sceneID="rk5-AZ-VT1"> | ||
<objects> | ||
<navigationController id="G6t-Sy-zaY" sceneMemberID="viewController"> | ||
<navigationBar key="navigationBar" contentMode="scaleToFill" id="AZi-ef-rH6"> | ||
<rect key="frame" x="0.0" y="0.0" width="320" height="44"/> | ||
<autoresizingMask key="autoresizingMask"/> | ||
<animations/> | ||
</navigationBar> | ||
<connections> | ||
<segue destination="uYg-Wb-NE8" kind="relationship" relationship="rootViewController" id="FKX-Rz-4BI"/> | ||
</connections> | ||
</navigationController> | ||
<placeholder placeholderIdentifier="IBFirstResponder" id="R6s-Lf-xV5" userLabel="First Responder" sceneMemberID="firstResponder"/> | ||
</objects> | ||
<point key="canvasLocation" x="208" y="134"/> | ||
</scene> | ||
</scenes> | ||
</document> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
using System; | ||
|
||
using UIKit; | ||
using Foundation; | ||
|
||
namespace Reachability { | ||
public partial class MainViewController : UITableViewController { | ||
const string ReachabilityTableCellIdentifier = "ReachabilityTableCell"; | ||
|
||
UIImage imageCarrier, imageWiFi, imageStop; | ||
NetworkStatus remoteHostStatus, internetStatus, localWifiStatus; | ||
|
||
public MainViewController (IntPtr handle) : base (handle) | ||
{ | ||
} | ||
|
||
public override void ViewDidLoad () | ||
{ | ||
base.ViewDidLoad (); | ||
UpdateStatus (null, null); | ||
Reachability.ReachabilityChanged += UpdateStatus; | ||
|
||
imageCarrier = UIImage.FromFile ("WWAN5.png"); | ||
imageWiFi = UIImage.FromFile ("Airport.png"); | ||
imageStop = UIImage.FromFile ("stop-32.png"); | ||
|
||
TableView.AutoresizingMask = UIViewAutoresizing.FlexibleWidth | UIViewAutoresizing.FlexibleHeight; | ||
TableView.RowHeight = 44.0f; | ||
TableView.SeparatorStyle = UITableViewCellSeparatorStyle.None; | ||
TableView.SectionHeaderHeight = 28.0f; | ||
TableView.ScrollEnabled = false; | ||
} | ||
|
||
public override NSIndexPath WillSelectRow (UITableView view, NSIndexPath index) | ||
{ | ||
return null; | ||
} | ||
|
||
public override nint RowsInSection (UITableView view, nint section) | ||
{ | ||
return 1; | ||
} | ||
|
||
public override nint NumberOfSections (UITableView view) | ||
{ | ||
return 3; | ||
} | ||
|
||
public override string TitleForHeader (UITableView view, nint section) | ||
{ | ||
switch (section) { | ||
case 0: | ||
return Reachability.HostName; | ||
case 1: | ||
return "Access to internet hosts"; | ||
case 2: | ||
return "Access to Local Bonjour Hosts"; | ||
default: | ||
return "Unknown"; | ||
} | ||
} | ||
|
||
public override UITableViewCell GetCell (UITableView tableView, Foundation.NSIndexPath indexPath) | ||
{ | ||
var cell = tableView.DequeueReusableCell (ReachabilityTableCellIdentifier); | ||
if (cell == null) { | ||
cell = new UITableViewCell (UITableViewCellStyle.Default, ReachabilityTableCellIdentifier); | ||
var label = cell.TextLabel; | ||
label.Font = UIFont.SystemFontOfSize (12f); | ||
label.TextColor = UIColor.DarkGray; | ||
label.TextAlignment = UITextAlignment.Left; | ||
} | ||
|
||
string text = ""; | ||
UIImage image = null; | ||
switch (indexPath.Section) { | ||
case 0: | ||
switch (remoteHostStatus) { | ||
case NetworkStatus.NotReachable: | ||
text = "Cannot connect to remote host"; | ||
image = imageStop; | ||
break; | ||
case NetworkStatus.ReachableViaCarrierDataNetwork: | ||
text = "Reachable via data carrier network"; | ||
image = imageCarrier; | ||
break; | ||
case NetworkStatus.ReachableViaWiFiNetwork: | ||
text = "Reachable via WiFi network"; | ||
image = imageWiFi; | ||
break; | ||
} | ||
break; | ||
case 1: | ||
switch (internetStatus) { | ||
case NetworkStatus.NotReachable: | ||
text = "Access not available"; | ||
image = imageStop; | ||
break; | ||
case NetworkStatus.ReachableViaCarrierDataNetwork: | ||
text = "Available via data carrier network"; | ||
image = imageCarrier; | ||
break; | ||
case NetworkStatus.ReachableViaWiFiNetwork: | ||
text = "Available via WiFi network"; | ||
image = imageWiFi; | ||
break; | ||
} | ||
break; | ||
case 2: | ||
switch (localWifiStatus) { | ||
case NetworkStatus.NotReachable: | ||
text = "Access not available"; | ||
image = imageStop; | ||
break; | ||
case NetworkStatus.ReachableViaWiFiNetwork: | ||
text = "Available via WiFi network"; | ||
image = imageWiFi; | ||
break; | ||
} | ||
break; | ||
} | ||
cell.TextLabel.Text = text; | ||
cell.ImageView.Image = image; | ||
return cell; | ||
} | ||
|
||
void UpdateStatus (object sender, EventArgs e) | ||
{ | ||
remoteHostStatus = Reachability.RemoteHostStatus (); | ||
internetStatus = Reachability.InternetConnectionStatus (); | ||
localWifiStatus = Reachability.LocalWifiConnectionStatus (); | ||
TableView.ReloadData (); | ||
} | ||
} | ||
} |
Oops, something went wrong.