Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better swift interop; better iOS 9+ support; and don't show activity if Chrome not installed. #24

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,26 @@
ChromeActivityDemo.xcodeproj/project.xcworkspace/xcuserdata/Alex.xcuserdatad/UserInterfaceState.xcuserstate

ChromeActivityDemo.xcodeproj/xcuserdata/Alex.xcuserdatad/xcdebugger/Breakpoints.xcbkptlist


# Xcode
#
build/
*.pbxuser
!default.pbxuser
*.mode1v3
!default.mode1v3
*.mode2v3
!default.mode2v3
*.perspectivev3
!default.perspectivev3
xcuserdata
*.xccheckout
*.moved-aside
DerivedData
*.hmap
*.ipa
*.xcuserstate


.DS_Store
10 changes: 5 additions & 5 deletions ARChromeActivity/ARChromeActivity.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@
@interface ARChromeActivity : UIActivity

/// Uses the "CFBundleName" from your Info.plist by default.
@property (strong, nonatomic) NSString *callbackSource;
@property (strong, nonatomic, nonnull) NSString *callbackSource;

/// The text beneath the icon. Defaults to "Chrome".
@property (strong, nonatomic) NSString *activityTitle;
/// The text beneath the icon. Defaults to "Open in Chrome".
@property (strong, nonatomic, nonnull) NSString *activityTitle;

/// Empty by default. Either set this in the initializer, or set this property. For iOS 9+, make sure you register the Chrome URL scheme in your Info.plist. See the demo project.
@property (strong, nonatomic) NSURL *callbackURL;
@property (strong, nonatomic, nullable) NSURL *callbackURL;

- (id)initWithCallbackURL:(NSURL *)callbackURL;
- (nonnull id)initWithCallbackURL:(nullable NSURL *)callbackURL;

@end
60 changes: 48 additions & 12 deletions ARChromeActivity/ARChromeActivity.m
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,7 @@
@implementation ARChromeActivity {
NSURL *_activityURL;
}

@synthesize callbackURL = _callbackURL;
@synthesize callbackSource = _callbackSource;
@synthesize activityTitle = _activityTitle;
@synthesize activityTitle = _title;

static NSString *encodeByAddingPercentEscapes(NSString *input) {
NSString *encodedValue = (NSString *)CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault, (CFStringRef)input, NULL, (CFStringRef)@"!*'();:@&=+$,/?%#[]", kCFStringEncodingUTF8));
Expand All @@ -28,7 +25,12 @@ @implementation ARChromeActivity {

- (void)commonInit {
_callbackSource = [[NSBundle mainBundle]objectForInfoDictionaryKey:@"CFBundleName"];
_activityTitle = @"Open in Chrome";
_title = @"Open in Chrome";
}

- (NSString *)activityTitle
{
return _title;
}

- (id)init {
Expand Down Expand Up @@ -61,6 +63,9 @@ - (NSString *)activityType {
}

- (BOOL)canPerformWithActivityItems:(NSArray *)activityItems {
if (![[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"googlechrome://"]]) {
return NO;
}
if (_callbackURL && ![[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"googlechrome-x-callback://"]]) {
return NO;
}
Expand Down Expand Up @@ -89,13 +94,44 @@ - (void)prepareWithActivityItems:(NSArray *)activityItems {
}

- (void)performActivity {
NSString *openingURL = encodeByAddingPercentEscapes(_activityURL.absoluteString);
NSString *callbackURL = encodeByAddingPercentEscapes(self.callbackURL.absoluteString);
NSString *sourceName = encodeByAddingPercentEscapes(self.callbackSource);

NSURL *activityURL = [NSURL URLWithString:[NSString stringWithFormat:@"googlechrome-x-callback://x-callback-url/open/?url=%@&x-success=%@&x-source=%@", openingURL, callbackURL, sourceName]];
[[UIApplication sharedApplication] openURL:activityURL];
[self activityDidFinish:YES];

BOOL success = NO;

if (_activityURL != nil) {

if (self.callbackURL && self.callbackSource) {
NSString *openingURL = encodeByAddingPercentEscapes(_activityURL.absoluteString);
NSString *callbackURL = encodeByAddingPercentEscapes(self.callbackURL.absoluteString);
NSString *sourceName = encodeByAddingPercentEscapes(self.callbackSource);

NSURL *activityURL = [NSURL URLWithString:[NSString stringWithFormat:@"googlechrome-x-callback://x-callback-url/open/?url=%@&x-success=%@&x-source=%@", openingURL, callbackURL, sourceName]];
[[UIApplication sharedApplication] openURL:activityURL];
success = YES;
} else {

NSString *scheme = _activityURL.scheme;

NSString *chromeScheme = nil;
if ([scheme isEqualToString:@"http"]) {
chromeScheme = @"googlechrome";
} else if ([scheme isEqualToString:@"https"]) {
chromeScheme = @"googlechromes";
}

if (chromeScheme) {
NSString *absoluteString = [_activityURL absoluteString];
NSRange rangeForScheme = [absoluteString rangeOfString:@":"];
NSString *urlNoScheme = [absoluteString substringFromIndex:rangeForScheme.location];
NSString *chromeURLString = [chromeScheme stringByAppendingString:urlNoScheme];
NSURL *chromeURL = [NSURL URLWithString:chromeURLString];

[[UIApplication sharedApplication] openURL:chromeURL];
success = YES;
}
}
}

[self activityDidFinish:success];
}

@end