Created
March 13, 2010 03:28
-
-
Save martinbowling/331083 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
public class HtmlElement : Element { | |
public string Url; | |
static NSString hkey = new NSString ("HtmlElement"); | |
UIWebView web; | |
UIToolbar toolbar; | |
UIBarButtonItem forward; | |
UIBarButtonItem back; | |
UIBarButtonItem refresh; | |
UIBarButtonItem actionbutton; | |
public HtmlElement (string caption, string url) : base (caption) | |
{ | |
Url = url; | |
} | |
public override UITableViewCell GetCell (UITableView tv) | |
{ | |
var cell = tv.DequeueReusableCell (hkey); | |
if (cell == null){ | |
cell = new UITableViewCell (UITableViewCellStyle.Default, hkey); | |
cell.SelectionStyle = UITableViewCellSelectionStyle.Blue; | |
} | |
cell.Accessory = UITableViewCellAccessory.DisclosureIndicator; | |
cell.TextLabel.Text = Caption; | |
return cell; | |
} | |
static bool NetworkActivity { | |
set { | |
UIApplication.SharedApplication.NetworkActivityIndicatorVisible = value; | |
} | |
} | |
// We use this class to dispose the web control when it is not | |
// in use, as it could be a bit of a pig, and we do not want to | |
// wait for the GC to kick-in. | |
class WebViewController : UIViewController { | |
HtmlElement container; | |
public WebViewController (HtmlElement container) : base () | |
{ | |
this.container = container; | |
} | |
public override void ViewWillDisappear (bool animated) | |
{ | |
base.ViewWillDisappear (animated); | |
NetworkActivity = false; | |
container.web.StopLoading (); | |
container.web.Dispose (); | |
container.web = null; | |
} | |
} | |
public override void Selected (DialogViewController dvc, UITableView tableView, NSIndexPath path) | |
{ | |
var vc = new WebViewController (this); | |
toolbar = new UIToolbar(new RectangleF(0,0,UIScreen.MainScreen.ApplicationFrame.Width,40)); | |
toolbar.BarStyle = UIBarStyle.Black; | |
forward = new UIBarButtonItem(); | |
forward.Image = UIImage.FromFile("arrow-right.png"); | |
back = new UIBarButtonItem(); | |
back.Image = UIImage.FromFile("arrow-left.png"); | |
refresh = new UIBarButtonItem(UIBarButtonSystemItem.Refresh); | |
actionbutton = new UIBarButtonItem(UIBarButtonSystemItem.Action); | |
var spacer10 = new UIBarButtonItem(UIBarButtonSystemItem.FixedSpace); | |
spacer10.Width = 10; | |
var spacer20 = new UIBarButtonItem(UIBarButtonSystemItem.FixedSpace); | |
spacer20.Width = 20; | |
var spacer160 = new UIBarButtonItem(UIBarButtonSystemItem.FixedSpace); | |
spacer160.Width = 160; | |
back.Enabled = false; | |
forward.Enabled = false; | |
var buttons = new UIBarButtonItem []{ spacer10, back, spacer20, forward, spacer160, refresh, spacer20, actionbutton }; | |
toolbar.SetItems(buttons, false); | |
web = new UIWebView(new RectangleF(0,40,UIScreen.MainScreen.ApplicationFrame.Width, (UIScreen.MainScreen.ApplicationFrame.Height-40))){ | |
BackgroundColor = UIColor.White, | |
ScalesPageToFit = true, | |
AutoresizingMask = UIViewAutoresizing.FlexibleWidth | UIViewAutoresizing.FlexibleHeight | |
}; | |
forward.Clicked += delegate { | |
web.GoForward(); | |
}; | |
back.Clicked += delegate { | |
web.GoBack(); | |
}; | |
refresh.Clicked += delegate { | |
web.Reload(); | |
}; | |
actionbutton.Clicked += delegate { | |
UIActionSheet actionSheet = new UIActionSheet("", null, null, null, "Open In Safari", "Email to a Friend", "Cancel"); | |
actionSheet.CancelButtonIndex = 2; | |
actionSheet.Clicked += delegate(object sender, UIButtonEventArgs e) { | |
if (e.ButtonIndex == 0) | |
{ | |
UIApplication.SharedApplication.OpenUrl (web.Request.Url); | |
} | |
else if (e.ButtonIndex == 1) | |
{ | |
MFMailComposeViewController mailvc = new MFMailComposeViewController(); | |
mailvc.Finished += delegate { | |
vc.DismissModalViewControllerAnimated(true); | |
}; | |
mailvc.SetMessageBody("<br />\r\n<br />\r\nShared from The Connectors iPhone App <br />\r\n<a href=\"http://TheConnectorsApp.com\">http://TheConnectorsApp.com</a>", true); | |
vc.PresentModalViewController(mailvc,true); | |
} | |
}; | |
actionSheet.ShowInView(vc.View); | |
// | |
}; | |
web.LoadStarted += delegate { | |
NetworkActivity = true; | |
}; | |
web.LoadFinished += delegate { | |
back.Enabled = web.CanGoBack; | |
forward.Enabled = web.CanGoForward; | |
NetworkActivity = false; | |
}; | |
web.LoadError += (webview, args) => { | |
NetworkActivity = false; | |
if (web != null) | |
web.LoadHtmlString (String.Format ("<html><center><font size=+5 color='red'>An error occurred:<br>{0}</font></center></html>", args.Error.LocalizedDescription), null); | |
}; | |
vc.NavigationItem.Title = Caption; | |
vc.View.AddSubview(toolbar); | |
vc.View.AddSubview (web); | |
dvc.ActivateController (vc); | |
web.LoadRequest (NSUrlRequest.FromUrl (new NSUrl (Url))); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment