Skip to content
This repository has been archived by the owner on Feb 4, 2022. It is now read-only.

Sharing on iOS #233

Open
wants to merge 18 commits into
base: develop
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
24 changes: 24 additions & 0 deletions ios/OpenbookShareExtension/Base.lproj/MainInterface.storyboard
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="13122.16" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES" initialViewController="j1y-V4-xli">
<dependencies>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="13104.12"/>
<capability name="Safe area layout guides" minToolsVersion="9.0"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
</dependencies>
<scenes>
<!--Share View Controller-->
<scene sceneID="ceB-am-kn3">
<objects>
<viewController id="j1y-V4-xli" customClass="ShareViewController" customModuleProvider="" sceneMemberID="viewController">
<view key="view" opaque="NO" contentMode="scaleToFill" id="wbc-yd-nQP">
<rect key="frame" x="0.0" y="0.0" width="375" height="667"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<color key="backgroundColor" red="0.0" green="0.0" blue="0.0" alpha="0.0" colorSpace="custom" customColorSpace="sRGB"/>
<viewLayoutGuide key="safeArea" id="1Xd-am-t49"/>
</view>
</viewController>
<placeholder placeholderIdentifier="IBFirstResponder" id="CEy-Cv-SGf" userLabel="First Responder" sceneMemberID="firstResponder"/>
</objects>
</scene>
</scenes>
</document>
36 changes: 36 additions & 0 deletions ios/OpenbookShareExtension/Info.plist
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>CFBundleDevelopmentRegion</key>
<string>$(DEVELOPMENT_LANGUAGE)</string>
<key>CFBundleDisplayName</key>
<string>Openbook</string>
<key>CFBundleExecutable</key>
<string>$(EXECUTABLE_NAME)</string>
<key>CFBundleIdentifier</key>
<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundleName</key>
<string>$(PRODUCT_NAME)</string>
<key>CFBundlePackageType</key>
<string>XPC!</string>
<key>CFBundleShortVersionString</key>
<string>0.0.34</string>
<key>CFBundleVersion</key>
<string>34</string>
<key>NSExtension</key>
<dict>
<key>NSExtensionAttributes</key>
<dict>
<key>NSExtensionActivationRule</key>
<string>TRUEPREDICATE</string>
</dict>
<key>NSExtensionMainStoryboard</key>
<string>MainInterface</string>
<key>NSExtensionPointIdentifier</key>
<string>com.apple.share-services</string>
</dict>
</dict>
</plist>
11 changes: 11 additions & 0 deletions ios/OpenbookShareExtension/OpenbookShareExtension.entitlements
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>com.apple.security.application-groups</key>
<array>
<string>group.social.openbook.app</string>
<string>group.social.openbook.app.onesignal</string>
</array>
</dict>
</plist>
6 changes: 6 additions & 0 deletions ios/OpenbookShareExtension/ShareViewController.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#import <UIKit/UIKit.h>
#import <Social/Social.h>

@interface ShareViewController : SLComposeServiceViewController

@end
125 changes: 125 additions & 0 deletions ios/OpenbookShareExtension/ShareViewController.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
#import "ShareViewController.h"

const NSString* APP_GROUP_NAME = @"group.social.openbook.app";

@interface ShareViewController ()

@end

@implementation ShareViewController

- (BOOL)isContentValid {
// Do validation of contentText and/or NSExtensionContext attachments here
return YES;
}

- (NSString*)getTempDir {
NSFileManager* manager = [NSFileManager defaultManager];
NSString* tempDir = [[[manager containerURLForSecurityApplicationGroupIdentifier:APP_GROUP_NAME] path] stringByAppendingPathComponent:@"tmp"];

BOOL isDir;
NSError* error = nil;

if (![manager fileExistsAtPath:tempDir isDirectory:&isDir]) {
if (![manager createDirectoryAtPath:tempDir withIntermediateDirectories:YES attributes:nil error:&error]) {
// TODO: handle error
}
}

return tempDir;
}

- (NSString*) getTempFileWithExtension:(NSString*)extension {
NSString* fileName = [[NSUUID UUID] UUIDString];
NSString* tempFile = [fileName stringByAppendingPathExtension:extension];

return tempFile;
}

- (UIApplication*)getUIApplication {
UIResponder* responder = (UIResponder*)self;
while ((responder = [responder nextResponder]) != nil) {
if ([responder isKindOfClass:[UIApplication class]]) {
return (UIApplication*)responder;
}
}
return nil;
}

- (void)callOpenbookAppWithSharedFileName:(NSString*)fileName {
NSURL* url = [[NSURL URLWithString:@"openbook://share"] URLByAppendingPathComponent:fileName];
NSLog(@"Opening URL: %@", url);
UIApplication* application = [self getUIApplication];
if (application == nil) {
NSLog(@"Failed to get UIApplication, can't open URL!");
return;
}

[application performSelector:@selector(openURL:) withObject:url];
}

- (void)callOpenbookAppWithData:(NSDictionary*)data {
NSFileManager* manager = [NSFileManager defaultManager];
NSError* error;
NSData* jsonData = [NSJSONSerialization dataWithJSONObject:data options:0 error:&error];
// TODO: handle error
NSString* tempPath = [self getTempDir];
NSString* jsonFile = [self getTempFileWithExtension:@"json"];
if (![manager createFileAtPath:[tempPath stringByAppendingPathComponent:jsonFile] contents:jsonData attributes:nil]) {
// TODO: handle error
}
// call main app
[self callOpenbookAppWithSharedFileName:jsonFile];
// Inform the host that we're done, so it un-blocks its UI. Note: Alternatively you could call super's -didSelectPost, which will similarly complete the extension context.
[self.extensionContext completeRequestReturningItems:@[] completionHandler:nil];
}

- (void)didSelectPost {
// This is called after the user selects Post. Do the upload of contentText and/or NSExtensionContext attachments.
NSFileManager* manager = [NSFileManager defaultManager];
NSExtensionContext* context = self.extensionContext;
// As we have to communicate text and an image to the app, write everything to a file instead of putting everything in an URL.
NSMutableDictionary* args = [[NSMutableDictionary alloc] init];
args[@"text"] = self.contentText;
// Handle an image
BOOL hasCallback = NO;
NSArray* items = context.inputItems;
if ([items count] >= 1) {
NSExtensionItem* item = items[0];
if ([item.attachments count] >= 1) {
NSItemProvider* itemProvider = item.attachments[0];
if ([itemProvider hasItemConformingToTypeIdentifier:@"public.image"]) {
hasCallback = YES;
[itemProvider loadItemForTypeIdentifier:@"public.image" options:nil completionHandler:^void(UIImage* image, NSError* error) {
// TODO: handle error
NSData* imageData = UIImageJPEGRepresentation(image, 1.0);
if (imageData == nil){
// TODO: handle error
}
NSString* tempPath = [self getTempDir];
NSString* fileName = [self getTempFileWithExtension:@"jpg"];
if (![manager createFileAtPath:[tempPath stringByAppendingPathComponent:fileName] contents:imageData attributes:nil]) {
// TODO: handle error
}
args[@"path"] = [tempPath stringByAppendingPathComponent:fileName];
NSLog(@"image written to %@, opening app", args[@"path"]);
[self callOpenbookAppWithData:args];
}];
} else {
NSLog(@"No UTI public.image found, ignoring attachment");
}
}
}

// If we have a callback (when loading an image), do not immediately open the app, that will happen in the callback
if (!hasCallback) {
[self callOpenbookAppWithData:args];
}
}

- (NSArray *)configurationItems {
// To add configuration options via table cells at the bottom of the sheet, return an array of SLComposeSheetConfigurationItem here.
return @[];
}

@end
Loading