Skip to content
This repository has been archived by the owner on May 19, 2018. It is now read-only.

Commit

Permalink
Web view: Copy URL, Open in Safari
Browse files Browse the repository at this point in the history
  • Loading branch information
Max Bäumle authored and Max Bäumle committed Mar 4, 2013
1 parent d2b5f26 commit a78f69a
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 10 deletions.
18 changes: 17 additions & 1 deletion Classes/WebController.m
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
#import "WebController.h"
#import "IOCApplication.h"


@interface WebController () <UIWebViewDelegate>
@interface WebController () <UIWebViewDelegate, UIActionSheetDelegate>
@property(nonatomic,strong)NSURL *url;
@property(nonatomic,strong)NSString *html;
@property(nonatomic,weak)IBOutlet UIWebView *webView;
@property(nonatomic,weak)IBOutlet UIBarButtonItem *leftButton;
@property(nonatomic,weak)IBOutlet UIBarButtonItem *rightButton;
@property(nonatomic,weak)IBOutlet UIBarButtonItem *actionButton;
@property(nonatomic,weak)IBOutlet UIToolbar *toolbar;
@property(nonatomic,strong)IBOutlet UIActivityIndicatorView *activityView;
- (IBAction)leftButtonTapped:(id)sender;
- (IBAction)rightButtonTapped:(id)sender;
- (IBAction)actionButtonTapped:(id)sender;
@end


Expand Down Expand Up @@ -92,11 +95,22 @@ - (IBAction)rightButtonTapped:(id)sender {
[self.webView goForward];
}

- (IBAction)actionButtonTapped:(id)sender {
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:[[self.webView.request URL] absoluteString] delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil otherButtonTitles:@"Open in Safari", @"Copy URL", nil];
[actionSheet showFromToolbar:self.toolbar];
}

- (void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex {
if (buttonIndex == 0) [(IOCApplication *)[UIApplication sharedApplication] forceOpenURL:[self.webView.request URL]];
else if (buttonIndex == 1) [UIPasteboard generalPasteboard].URL = [self.webView.request URL];
}

#pragma mark WebView

- (void)webViewDidStartLoad:(UIWebView *)webview {
self.leftButton.enabled = [webview canGoBack];
self.rightButton.enabled = [webview canGoForward];
self.actionButton.enabled = [[self.webView.request URL] host] != nil;
[self.activityView startAnimating];
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
}
Expand All @@ -105,13 +119,15 @@ - (void)webViewDidFinishLoad:(UIWebView *)webview {
self.navigationItem.title = [webview stringByEvaluatingJavaScriptFromString:@"document.title"];
self.leftButton.enabled = [webview canGoBack];
self.rightButton.enabled = [webview canGoForward];
self.actionButton.enabled = [[self.webView.request URL] host] != nil;
[self.activityView stopAnimating];
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
}

- (void)webView:(UIWebView *)webview didFailLoadWithError:(NSError *)error {
self.leftButton.enabled = [webview canGoBack];
self.rightButton.enabled = [webview canGoForward];
self.actionButton.enabled = [[self.webView.request URL] host] != nil;
[self.activityView stopAnimating];
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
}
Expand Down
75 changes: 66 additions & 9 deletions IBFiles/WebView.xib
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,17 @@
<int key="IBUISystemItemIdentifier">5</int>
</object>
<object class="IBUIBarButtonItem" id="407294535">
<bool key="IBUIEnabled">NO</bool>
<string key="targetRuntimeIdentifier">IBCocoaTouchFramework</string>
<reference key="IBUIToolbar" ref="630246777"/>
<int key="IBUISystemItemIdentifier">9</int>
</object>
<object class="IBUIBarButtonItem" id="313549996">
<string key="targetRuntimeIdentifier">IBCocoaTouchFramework</string>
<reference key="IBUIToolbar" ref="630246777"/>
<int key="IBUISystemItemIdentifier">5</int>
</object>
<object class="IBUIBarButtonItem" id="332990842">
<bool key="IBUIEnabled">NO</bool>
<object class="NSCustomResource" key="IBUIImage">
<string key="NSClassName">NSImage</string>
Expand All @@ -99,7 +110,7 @@
<string key="targetRuntimeIdentifier">IBCocoaTouchFramework</string>
<reference key="IBUIToolbar" ref="630246777"/>
</object>
<object class="IBUIBarButtonItem" id="313549996">
<object class="IBUIBarButtonItem" id="416762741">
<string key="targetRuntimeIdentifier">IBCocoaTouchFramework</string>
<reference key="IBUIToolbar" ref="630246777"/>
<int key="IBUISystemItemIdentifier">5</int>
Expand Down Expand Up @@ -173,19 +184,27 @@
</object>
<object class="IBConnectionRecord">
<object class="IBCocoaTouchOutletConnection" key="connection">
<string key="label">rightButton</string>
<string key="label">toolbar</string>
<reference key="source" ref="372490531"/>
<reference key="destination" ref="630246777"/>
</object>
<int key="connectionID">17</int>
</object>
<object class="IBConnectionRecord">
<object class="IBCocoaTouchOutletConnection" key="connection">
<string key="label">actionButton</string>
<reference key="source" ref="372490531"/>
<reference key="destination" ref="407294535"/>
</object>
<int key="connectionID">16</int>
<int key="connectionID">22</int>
</object>
<object class="IBConnectionRecord">
<object class="IBCocoaTouchOutletConnection" key="connection">
<string key="label">toolbar</string>
<string key="label">rightButton</string>
<reference key="source" ref="372490531"/>
<reference key="destination" ref="630246777"/>
<reference key="destination" ref="332990842"/>
</object>
<int key="connectionID">17</int>
<int key="connectionID">23</int>
</object>
<object class="IBConnectionRecord">
<object class="IBCocoaTouchOutletConnection" key="connection">
Expand All @@ -205,11 +224,19 @@
</object>
<object class="IBConnectionRecord">
<object class="IBCocoaTouchEventConnection" key="connection">
<string key="label">rightButtonTapped:</string>
<string key="label">actionButtonTapped:</string>
<reference key="source" ref="407294535"/>
<reference key="destination" ref="372490531"/>
</object>
<int key="connectionID">19</int>
<int key="connectionID">24</int>
</object>
<object class="IBConnectionRecord">
<object class="IBCocoaTouchEventConnection" key="connection">
<string key="label">rightButtonTapped:</string>
<reference key="source" ref="332990842"/>
<reference key="destination" ref="372490531"/>
</object>
<int key="connectionID">25</int>
</object>
</object>
<object class="IBMutableOrderedSet" key="objectRecords">
Expand Down Expand Up @@ -264,6 +291,8 @@
<reference ref="975151111"/>
<reference ref="407294535"/>
<reference ref="313549996"/>
<reference ref="332990842"/>
<reference ref="416762741"/>
</object>
<reference key="parent" ref="191373211"/>
</object>
Expand Down Expand Up @@ -292,6 +321,16 @@
<reference key="object" ref="313549996"/>
<reference key="parent" ref="630246777"/>
</object>
<object class="IBObjectRecord">
<int key="objectID">20</int>
<reference key="object" ref="332990842"/>
<reference key="parent" ref="630246777"/>
</object>
<object class="IBObjectRecord">
<int key="objectID">21</int>
<reference key="object" ref="416762741"/>
<reference key="parent" ref="630246777"/>
</object>
</object>
</object>
<object class="NSMutableDictionary" key="flattenedProperties">
Expand All @@ -308,6 +347,8 @@
<string>12.IBPluginDependency</string>
<string>13.IBPluginDependency</string>
<string>14.IBPluginDependency</string>
<string>20.IBPluginDependency</string>
<string>21.IBPluginDependency</string>
<string>3.IBPluginDependency</string>
<string>4.IBPluginDependency</string>
<string>9.IBPluginDependency</string>
Expand All @@ -327,6 +368,8 @@
<string>com.apple.InterfaceBuilder.IBCocoaTouchPlugin</string>
<string>com.apple.InterfaceBuilder.IBCocoaTouchPlugin</string>
<string>com.apple.InterfaceBuilder.IBCocoaTouchPlugin</string>
<string>com.apple.InterfaceBuilder.IBCocoaTouchPlugin</string>
<string>com.apple.InterfaceBuilder.IBCocoaTouchPlugin</string>
</object>
</object>
<object class="NSMutableDictionary" key="unlocalizedProperties">
Expand All @@ -341,7 +384,7 @@
<reference key="dict.values" ref="0"/>
</object>
<nil key="sourceID"/>
<int key="maxID">19</int>
<int key="maxID">25</int>
</object>
<object class="IBClassDescriber" key="IBDocument.Classes">
<object class="NSMutableArray" key="referencedPartialClassDescriptions">
Expand All @@ -353,24 +396,31 @@
<bool key="EncodedWithXMLCoder">YES</bool>
<object class="NSArray" key="dict.sortedKeys">
<bool key="EncodedWithXMLCoder">YES</bool>
<string>actionButtonTapped:</string>
<string>leftButtonTapped:</string>
<string>rightButtonTapped:</string>
</object>
<object class="NSArray" key="dict.values">
<bool key="EncodedWithXMLCoder">YES</bool>
<string>id</string>
<string>id</string>
<string>id</string>
</object>
</object>
<object class="NSMutableDictionary" key="actionInfosByName">
<bool key="EncodedWithXMLCoder">YES</bool>
<object class="NSArray" key="dict.sortedKeys">
<bool key="EncodedWithXMLCoder">YES</bool>
<string>actionButtonTapped:</string>
<string>leftButtonTapped:</string>
<string>rightButtonTapped:</string>
</object>
<object class="NSArray" key="dict.values">
<bool key="EncodedWithXMLCoder">YES</bool>
<object class="IBActionInfo">
<string key="name">actionButtonTapped:</string>
<string key="candidateClassName">id</string>
</object>
<object class="IBActionInfo">
<string key="name">leftButtonTapped:</string>
<string key="candidateClassName">id</string>
Expand All @@ -385,6 +435,7 @@
<bool key="EncodedWithXMLCoder">YES</bool>
<object class="NSArray" key="dict.sortedKeys">
<bool key="EncodedWithXMLCoder">YES</bool>
<string>actionButton</string>
<string>activityView</string>
<string>leftButton</string>
<string>rightButton</string>
Expand All @@ -393,6 +444,7 @@
</object>
<object class="NSArray" key="dict.values">
<bool key="EncodedWithXMLCoder">YES</bool>
<string>UIBarButtonItem</string>
<string>UIActivityIndicatorView</string>
<string>UIBarButtonItem</string>
<string>UIBarButtonItem</string>
Expand All @@ -404,6 +456,7 @@
<bool key="EncodedWithXMLCoder">YES</bool>
<object class="NSArray" key="dict.sortedKeys">
<bool key="EncodedWithXMLCoder">YES</bool>
<string>actionButton</string>
<string>activityView</string>
<string>leftButton</string>
<string>rightButton</string>
Expand All @@ -412,6 +465,10 @@
</object>
<object class="NSArray" key="dict.values">
<bool key="EncodedWithXMLCoder">YES</bool>
<object class="IBToOneOutletInfo">
<string key="name">actionButton</string>
<string key="candidateClassName">UIBarButtonItem</string>
</object>
<object class="IBToOneOutletInfo">
<string key="name">activityView</string>
<string key="candidateClassName">UIActivityIndicatorView</string>
Expand Down
1 change: 1 addition & 0 deletions IOCApplication.h
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
@interface IOCApplication : UIApplication
- (void)forceOpenURL:(NSURL *)url;
@end
4 changes: 4 additions & 0 deletions IOCApplication.m
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,8 @@ - (BOOL)openURL:(NSURL *)url {
return [(iOctocat *)self.delegate openURL:url] ? YES : [super openURL:url];
}

- (void)forceOpenURL:(NSURL *)url {
[super openURL:url];
}

@end

0 comments on commit a78f69a

Please sign in to comment.