Skip to content

Commit

Permalink
Merge pull request #16 from davisjp1822/master
Browse files Browse the repository at this point in the history
Added iPhone and iPad compatible authentication view that is contained in a nice Navigation Controller.
  • Loading branch information
jeyben committed Nov 27, 2013
2 parents e8fa492 + aab7481 commit 7f2bb6c
Show file tree
Hide file tree
Showing 6 changed files with 134 additions and 19 deletions.
72 changes: 72 additions & 0 deletions IOSLinkedInAPI/LIALinkedInAuthorization.xib
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.XIB" version="3.0" toolsVersion="4514" systemVersion="13A603" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES">
<dependencies>
<deployment defaultVersion="1552" identifier="iOS"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="3747"/>
</dependencies>
<objects>
<placeholder placeholderIdentifier="IBFilesOwner" id="-1" userLabel="File's Owner" customClass="LIALinkedInAuthorizationViewController">
<connections>
<outlet property="authenticationWebView" destination="vlf-ov-cZS" id="vwv-u3-XX5"/>
<outlet property="loadingLabel" destination="axu-LC-bXI" id="k51-Zv-QvE"/>
</connections>
</placeholder>
<placeholder placeholderIdentifier="IBFirstResponder" id="-2" customClass="UIResponder"/>
<view contentMode="scaleToFill" id="1">
<rect key="frame" x="0.0" y="0.0" width="320" height="568"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
<webView clipsSubviews="YES" contentMode="scaleToFill" scalesPageToFit="YES" allowsInlineMediaPlayback="NO" mediaPlaybackRequiresUserAction="NO" mediaPlaybackAllowsAirPlay="NO" translatesAutoresizingMaskIntoConstraints="NO" id="vlf-ov-cZS">
<rect key="frame" x="0.0" y="0.0" width="320" height="524"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<color key="backgroundColor" red="1" green="1" blue="1" alpha="1" colorSpace="calibratedRGB"/>
<dataDetectorType key="dataDetectorTypes"/>
<connections>
<outlet property="delegate" destination="-1" id="4q7-Lh-dht"/>
</connections>
</webView>
<toolbar opaque="NO" clearsContextBeforeDrawing="NO" contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="WjC-WP-w4C">
<rect key="frame" x="0.0" y="524" width="320" height="44"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" flexibleMinY="YES"/>
<items>
<barButtonItem image="01-refresh.png" id="7T0-Tr-wGB">
<connections>
<action selector="tappedRefreshButton:" destination="-1" id="kgn-fR-RcX"/>
</connections>
</barButtonItem>
</items>
</toolbar>
<label opaque="NO" clipsSubviews="YES" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Loading..." lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="axu-LC-bXI">
<rect key="frame" x="122" y="535" width="77" height="21"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
<constraints>
<constraint firstAttribute="height" constant="21" id="EE0-LC-wpo"/>
<constraint firstAttribute="width" constant="77" id="iSa-53-4zz"/>
</constraints>
<fontDescription key="fontDescription" type="system" pointSize="17"/>
<color key="textColor" cocoaTouchSystemColor="darkTextColor"/>
<nil key="highlightedColor"/>
</label>
</subviews>
<color key="backgroundColor" white="1" alpha="1" colorSpace="custom" customColorSpace="calibratedWhite"/>
<constraints>
<constraint firstAttribute="trailing" secondItem="WjC-WP-w4C" secondAttribute="trailing" id="2jd-V9-C5r"/>
<constraint firstAttribute="centerX" secondItem="axu-LC-bXI" secondAttribute="centerX" id="DE2-u2-U4e"/>
<constraint firstItem="WjC-WP-w4C" firstAttribute="top" secondItem="vlf-ov-cZS" secondAttribute="bottom" id="DNv-T1-N6u"/>
<constraint firstItem="vlf-ov-cZS" firstAttribute="leading" secondItem="1" secondAttribute="leading" id="GlM-fL-rJe"/>
<constraint firstAttribute="trailing" secondItem="vlf-ov-cZS" secondAttribute="trailing" id="IcQ-uW-iQ4"/>
<constraint firstItem="vlf-ov-cZS" firstAttribute="top" secondItem="1" secondAttribute="top" id="JP7-cO-j9f"/>
<constraint firstItem="WjC-WP-w4C" firstAttribute="leading" secondItem="1" secondAttribute="leading" id="JmH-9c-ryp"/>
<constraint firstItem="axu-LC-bXI" firstAttribute="top" secondItem="vlf-ov-cZS" secondAttribute="bottom" constant="11" id="SJ4-wO-d1q"/>
<constraint firstAttribute="bottom" secondItem="WjC-WP-w4C" secondAttribute="bottom" id="sEp-ek-fuS"/>
<constraint firstAttribute="bottom" secondItem="axu-LC-bXI" secondAttribute="bottom" constant="12" id="sRq-eY-WYB"/>
<constraint firstItem="WjC-WP-w4C" firstAttribute="top" secondItem="vlf-ov-cZS" secondAttribute="bottom" id="yTR-bw-vPj"/>
</constraints>
<simulatedStatusBarMetrics key="simulatedStatusBarMetrics"/>
<simulatedScreenMetrics key="simulatedDestinationMetrics" type="retina4"/>
</view>
</objects>
<resources>
<image name="01-refresh.png" width="23" height="26"/>
</resources>
</document>
3 changes: 3 additions & 0 deletions IOSLinkedInAPI/LIALinkedInAuthorizationViewController.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,7 @@ typedef void(^LIAAuthorizationCodeFailureCallback)(NSError *errorReason);
@interface LIALinkedInAuthorizationViewController : UIViewController

- (id)initWithApplication:(LIALinkedInApplication *)application success:(LIAAuthorizationCodeSuccessCallback)success cancel:(LIAAuthorizationCodeCancelCallback)cancel failure:(LIAAuthorizationCodeFailureCallback)failure;

- (IBAction)tappedRefreshButton:(id)sender;

@end
69 changes: 51 additions & 18 deletions IOSLinkedInAPI/LIALinkedInAuthorizationViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,18 @@
#import "LIALinkedInAuthorizationViewController.h"
#import "NSString+LIAEncode.h"

#define SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending)

NSString *kLinkedInErrorDomain = @"LIALinkedInERROR";
NSString *kLinkedInDeniedByUser = @"the+user+denied+your+request";

@interface LIALinkedInAuthorizationViewController ()
@property(nonatomic, strong) UIWebView *authenticationWebView;
@property(nonatomic, weak) IBOutlet UIWebView *authenticationWebView;
@property(nonatomic, weak) IBOutlet UILabel *loadingLabel;
@property(nonatomic, copy) LIAAuthorizationCodeFailureCallback failureCallback;
@property(nonatomic, copy) LIAAuthorizationCodeSuccessCallback successCallback;
@property(nonatomic, copy) LIAAuthorizationCodeCancelCallback cancelCallback;
@property(nonatomic, strong) LIALinkedInApplication *application;
@property(nonatomic, strong) UIActivityIndicatorView *activityIndicatorView;
@end

@interface LIALinkedInAuthorizationViewController (UIWebViewDelegate) <UIWebViewDelegate>
Expand Down Expand Up @@ -64,27 +66,39 @@ - (id)initWithApplication:(LIALinkedInApplication *)application success:(LIAAuth

- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];

self.activityIndicatorView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
self.activityIndicatorView.center = self.view.center;
self.activityIndicatorView.hidesWhenStopped = YES;
[self.activityIndicatorView startAnimating];
[self.view addSubview:self.activityIndicatorView];

self.authenticationWebView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
self.authenticationWebView.delegate = self;
self.authenticationWebView.hidden = YES;
[self.view addSubview:self.authenticationWebView];

self.navigationController.navigationBarHidden = YES;

if(SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"7")) {

self.edgesForExtendedLayout = UIRectEdgeNone;
}

self.view = [[[NSBundle mainBundle] loadNibNamed:@"LIALinkedInAuthorization" owner:self options:nil] objectAtIndex:0];

UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(tappedCancelButton:)];
self.navigationItem.leftBarButtonItem = cancelButton;

self.authenticationWebView.scalesPageToFit = YES;

}

- (void)viewDidAppear:(BOOL)animated {
NSString *linkedIn = [NSString stringWithFormat:@"https://www.linkedin.com/uas/oauth2/authorization?response_type=code&client_id=%@&scope=%@&state=%@&redirect_uri=%@", self.application.clientId, self.application.grantedAccessString, self.application.state, [self.application.redirectURL LIAEncode]];
[self.authenticationWebView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:linkedIn]]];
}

#pragma mark UI Action Methods

- (void)tappedCancelButton:(id)sender {

[self dismissViewControllerAnimated:YES completion:nil];
}

- (IBAction)tappedRefreshButton:(id)sender {

[self.loadingLabel setAlpha:1.0];
[self.authenticationWebView reload];
}

@end

@implementation LIALinkedInAuthorizationViewController (UIWebViewDelegate)
Expand Down Expand Up @@ -138,8 +152,27 @@ - (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error {
}

- (void)webViewDidFinishLoad:(UIWebView *)webView {
[self.activityIndicatorView stopAnimating];
self.authenticationWebView.hidden = NO;


/*fix for the LinkedIn Auth window - it doesn't scale right when placed into
a webview inside of a form sheet modal. If we transform the HTML of the page
a bit, and fix the viewport to 540px (the width of the form sheet), the problem
is solved.
*/
if([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
NSString* js =
@"var meta = document.createElement('meta'); "
@"meta.setAttribute( 'name', 'viewport' ); "
@"meta.setAttribute( 'content', 'width = 540px, initial-scale = 1.0, user-scalable = yes' ); "
@"document.getElementsByTagName('head')[0].appendChild(meta)";

[webView stringByEvaluatingJavaScriptFromString: js];
}

[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:1.0];
[self.loadingLabel setAlpha:0];
[UIView commitAnimations];
}

@end
9 changes: 8 additions & 1 deletion IOSLinkedInAPI/LIALinkedInHttpClient.m
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,14 @@ - (void)getAuthorizationCode:(void (^)(NSString *))success cancel:(void (^)(void
- (void)showAuthorizationView:(LIALinkedInAuthorizationViewController *)authorizationViewController {
if (self.presentingViewController == nil)
self.presentingViewController = [[UIApplication sharedApplication] keyWindow].rootViewController;
[self.presentingViewController presentViewController:authorizationViewController animated:YES completion:nil];

UINavigationController *nc = [[UINavigationController alloc] initWithRootViewController:authorizationViewController];

if([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
nc.modalPresentationStyle = UIModalPresentationFormSheet;
}

[self.presentingViewController presentViewController:nc animated:YES completion:nil];
}

- (void)hideAuthenticateView {
Expand Down
Binary file added IOSLinkedInAPI/Resources/01-refresh.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added IOSLinkedInAPI/Resources/01-refresh@2x.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 7f2bb6c

Please sign in to comment.