Skip to content

Commit

Permalink
use PDFKit to show pdf if support
Browse files Browse the repository at this point in the history
  • Loading branch information
Wonday committed Jul 31, 2018
1 parent 9336651 commit c007fa5
Show file tree
Hide file tree
Showing 6 changed files with 550 additions and 7 deletions.
38 changes: 31 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ export default class Pdf extends Component {
path: '',
isDownloaded: false,
progress: 0,
isSupportPDFKit: false
};

this.lastRNBFTask = null;
Expand All @@ -119,6 +120,12 @@ export default class Pdf extends Component {
}

componentDidMount() {
if (Platform.OS === "ios") {
const PdfViewManagerNative = require('react-native').NativeModules.PdfViewManager;
PdfViewManagerNative.supportPDFKit((isSupportPDFKit)=>{
this.setState({isSupportPDFKit:isSupportPDFKit});
});
}
this._loadFromSource(this.props.source);
}

Expand Down Expand Up @@ -374,18 +381,30 @@ export default class Pdf extends Component {
/>
);
} else if (Platform.OS === "ios") {
if (this.state.isSupportPDFKit) {
return (
<PdfView
<PdfCustom
ref={component => (this._root = component)}
{...this.props}
style={[{backgroundColor: '#EEE'}, this.props.style]}
path={this.state.path}
onLoadComplete={this.props.onLoadComplete}
onPageChanged={this.props.onPageChanged}
onError={this._onError}
onPageSingleTap={this.props.onPageSingleTap}
onScaleChanged={this.props.onScaleChanged}
onChange={this._onChange}
/>
);
);
} else {
return (
<PdfView
{...this.props}
style={[{backgroundColor: '#EEE'}, this.props.style]}
path={this.state.path}
onLoadComplete={this.props.onLoadComplete}
onPageChanged={this.props.onPageChanged}
onError={this._onError}
onPageSingleTap={this.props.onPageSingleTap}
onScaleChanged={this.props.onScaleChanged}
/>
);
}
} else {
return (null);
}
Expand All @@ -399,8 +418,13 @@ if (Platform.OS === "android") {
var PdfCustom = requireNativeComponent('RCTPdf', Pdf, {
nativeOnly: {path: true, onChange: true},
})
} else if (Platform.OS === "ios") {
var PdfCustom = requireNativeComponent('RCTPdfView', Pdf, {
nativeOnly: {path: true, onChange: true},
})
}


const styles = StyleSheet.create({
progressContainer: {
flex: 1,
Expand Down
24 changes: 24 additions & 0 deletions ios/RCTPdf.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
objects = {

/* Begin PBXBuildFile section */
5267DA06210F0EDE00D20C59 /* RCTPdfView.m in Sources */ = {isa = PBXBuildFile; fileRef = 5267DA03210F0EDD00D20C59 /* RCTPdfView.m */; };
5267DA07210F0EDE00D20C59 /* RCTPdfViewManager.m in Sources */ = {isa = PBXBuildFile; fileRef = 5267DA05210F0EDE00D20C59 /* RCTPdfViewManager.m */; };
5267DA0A210F0F0800D20C59 /* PDFKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 5267DA09210F0F0800D20C59 /* PDFKit.framework */; };
526B80401EABD92200259970 /* RCTPdfPageViewManager.m in Sources */ = {isa = PBXBuildFile; fileRef = 526B803F1EABD92200259970 /* RCTPdfPageViewManager.m */; };
52A324EA1FD3C3FB000B0B18 /* PdfManager.m in Sources */ = {isa = PBXBuildFile; fileRef = 52A324E91FD3C3FB000B0B18 /* PdfManager.m */; };
52F79DB71EB2DA4F00CAD00C /* RCTPdfPageView.m in Sources */ = {isa = PBXBuildFile; fileRef = 52F79DB41EB2DA4F00CAD00C /* RCTPdfPageView.m */; };
Expand All @@ -26,6 +29,11 @@

/* Begin PBXFileReference section */
20D8E03F1C8E946C00039823 /* libRCTPdf.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = libRCTPdf.a; sourceTree = BUILT_PRODUCTS_DIR; };
5267DA02210F0EDD00D20C59 /* RCTPdfView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = RCTPdfView.h; sourceTree = "<group>"; };
5267DA03210F0EDD00D20C59 /* RCTPdfView.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = RCTPdfView.m; sourceTree = "<group>"; };
5267DA04210F0EDD00D20C59 /* RCTPdfViewManager.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = RCTPdfViewManager.h; sourceTree = "<group>"; };
5267DA05210F0EDE00D20C59 /* RCTPdfViewManager.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = RCTPdfViewManager.m; sourceTree = "<group>"; };
5267DA09210F0F0800D20C59 /* PDFKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = PDFKit.framework; path = System/Library/Frameworks/PDFKit.framework; sourceTree = SDKROOT; };
526B803E1EABD92200259970 /* RCTPdfPageViewManager.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = RCTPdfPageViewManager.h; sourceTree = "<group>"; };
526B803F1EABD92200259970 /* RCTPdfPageViewManager.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = RCTPdfPageViewManager.m; sourceTree = "<group>"; };
52A324E81FD3C3FB000B0B18 /* PdfManager.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = PdfManager.h; sourceTree = "<group>"; };
Expand All @@ -39,6 +47,7 @@
isa = PBXFrameworksBuildPhase;
buildActionMask = 2147483647;
files = (
5267DA0A210F0F0800D20C59 /* PDFKit.framework in Frameworks */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand All @@ -50,6 +59,7 @@
children = (
20D8E0411C8E946C00039823 /* RCTPdf */,
20D8E0401C8E946C00039823 /* Products */,
5267DA08210F0F0800D20C59 /* Frameworks */,
);
sourceTree = "<group>";
};
Expand All @@ -64,6 +74,10 @@
20D8E0411C8E946C00039823 /* RCTPdf */ = {
isa = PBXGroup;
children = (
5267DA02210F0EDD00D20C59 /* RCTPdfView.h */,
5267DA03210F0EDD00D20C59 /* RCTPdfView.m */,
5267DA04210F0EDD00D20C59 /* RCTPdfViewManager.h */,
5267DA05210F0EDE00D20C59 /* RCTPdfViewManager.m */,
52A324E81FD3C3FB000B0B18 /* PdfManager.h */,
52A324E91FD3C3FB000B0B18 /* PdfManager.m */,
52F79DB31EB2DA4F00CAD00C /* RCTPdfPageView.h */,
Expand All @@ -74,6 +88,14 @@
path = RCTPdf;
sourceTree = "<group>";
};
5267DA08210F0F0800D20C59 /* Frameworks */ = {
isa = PBXGroup;
children = (
5267DA09210F0F0800D20C59 /* PDFKit.framework */,
);
name = Frameworks;
sourceTree = "<group>";
};
/* End PBXGroup section */

/* Begin PBXNativeTarget section */
Expand Down Expand Up @@ -132,6 +154,8 @@
buildActionMask = 2147483647;
files = (
52A324EA1FD3C3FB000B0B18 /* PdfManager.m in Sources */,
5267DA07210F0EDE00D20C59 /* RCTPdfViewManager.m in Sources */,
5267DA06210F0EDE00D20C59 /* RCTPdfView.m in Sources */,
526B80401EABD92200259970 /* RCTPdfPageViewManager.m in Sources */,
52F79DB71EB2DA4F00CAD00C /* RCTPdfPageView.m in Sources */,
);
Expand Down
39 changes: 39 additions & 0 deletions ios/RCTPdf/RCTPdfView.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/**
* Copyright (c) 2017-present, Wonday (@wonday.org)
* All rights reserved.
*
* This source code is licensed under the MIT-style license found in the
* LICENSE file in the root directory of this source tree.
*/

#ifndef RCTPdfView_h
#define RCTPdfView_h

#if __has_include(<React/RCTAssert.h>)
#import <React/RCTEventDispatcher.h>
#import <React/UIView+React.h>
#else
#import "RCTEventDispatcher.h"
#import "UIView+React.h"
#endif


@class RCTEventDispatcher;

@interface RCTPdfView : UIView <UIGestureRecognizerDelegate>

@property(nonatomic, strong) NSString *path;
@property(nonatomic) int page;
@property(nonatomic) float scale;
@property(nonatomic) BOOL horizontal;
@property(nonatomic) int fitPolicy;
@property(nonatomic) int spacing;
@property(nonatomic, strong) NSString *password;


@property(nonatomic, copy) RCTBubblingEventBlock onChange;


@end

#endif /* RCTPdfView_h */
Loading

0 comments on commit c007fa5

Please sign in to comment.