|
| 1 | +## PSPDFKit for React Native |
| 2 | + |
| 3 | +### CocoaPods integration |
| 4 | + |
| 5 | +#### Requirements |
| 6 | +- Xcode 8.3.3 |
| 7 | +- PSPDFKit >=6.8 |
| 8 | +- react-native >= 0.46.4 |
| 9 | +- CocoaPods >= 1.2.1 |
| 10 | + |
| 11 | +#### Getting Started |
| 12 | + |
| 13 | +Lets create a simple app that integrates `PSPDFKit.framework` using CocoaPods. |
| 14 | + |
| 15 | +1. Make sure `react-native-cli` is installed: `yarn global add react-native-cli` |
| 16 | +2. Create the app with `react-native init YourApp`. |
| 17 | +3. Step into your newly created app folder: `cd YourApp` |
| 18 | +4. Install `react-native-pspdfkit` from GitHub: `yarn add github:PSPDFKit/react-native` |
| 19 | +5. IMPORTANT: Do not link module react-native-pspdfkit: Do not use react-native link react-native-pspdfkit |
| 20 | +6. Create the folder `ios/PSPDFKit` and copy `PSPDFKit.framework` into it. |
| 21 | +7. Open ios/YourApp.xcodeproj in Xcode: open ios/YourApp.xcodeproj |
| 22 | +8. Make sure the deployment target is set to 9.0 or higher: |
| 23 | + |
| 24 | +9. Change "View controller-based status bar appearance" to YES in Info.plist: |
| 25 | + deployment-target.png |
| 26 | +10. Close the Xcode project |
| 27 | +11. Go back to the Terminal, `cd ios` then run `pod init` |
| 28 | +12. Replace the content of your newly created `Podfile` with this: |
| 29 | + |
| 30 | +```podfile |
| 31 | +target 'YourApp' do |
| 32 | + use_frameworks! |
| 33 | +
|
| 34 | + pod 'react-native-pspdfkit', :path => '../node_modules/react-native-pspdfkit' |
| 35 | +
|
| 36 | + # To use CocoaPods with React Native, you need to add this specific Yoga spec as well |
| 37 | + pod 'Yoga', :path => '../node_modules/react-native/ReactCommon/yoga/Yoga.podspec' |
| 38 | +
|
| 39 | + # You don't necessarily need all of these subspecs, but this would be a typical setup. |
| 40 | + pod 'React', :path => '../node_modules/react-native', :subspecs => [ |
| 41 | + 'Core', |
| 42 | + 'RCTText', |
| 43 | + 'RCTNetwork', |
| 44 | + 'RCTWebSocket', # needed for debugging |
| 45 | + 'RCTAnimation', |
| 46 | + 'RCTImage', |
| 47 | + 'RCTNetwork', |
| 48 | + 'BatchedBridge', # https://github.com/facebook/react-native/issues/14749 |
| 49 | + # Add any other subspecs you want to use in your project |
| 50 | + ] |
| 51 | +
|
| 52 | + # Add any other dependencies here, including any 3rd party native libraries that you depend on for |
| 53 | + # React Native. |
| 54 | +end |
| 55 | +``` |
| 56 | + |
| 57 | +13. Run `pod install` |
| 58 | +14. Open the newly created workspace: `YourApp.workspace` |
| 59 | +15. Copy `PSPDFKit.framework` into the Pods folder: `YourApp/ios/Pods` |
| 60 | +16. Drag and drop it from the Finder into the `RCTPSPDFKit` group: |
| 61 | + |
| 62 | +17. Add it to the `react-native-pspdfkit` framework: |
| 63 | + |
| 64 | +18. Embed `YourApp/ios/PSPDFKit/PSPDFKit.framework` (not the copy from `YourApp/ios/Pods/PSPDFKit.framework`) by drag and dropping it into the "Embedded Binaries" section of the "YourApp" target (Select "Create groups"). This will also add it to the "Linked Frameworks and Libraries" section: |
| 65 | + |
| 66 | +19. Add a PDF by drag and dropping it into your Xcode project (Select "Create groups" and add to target "YourApp"). This will add the document to the "Copy Bundle Resources" build phase: |
| 67 | + |
| 68 | +20. Replace the default component from `index.ios.js` with a simple touch area to present the bundled PDF: |
| 69 | + |
| 70 | +```javascript |
| 71 | +import React, { Component } from 'react'; |
| 72 | +import { |
| 73 | + AppRegistry, |
| 74 | + StyleSheet, |
| 75 | + NativeModules, |
| 76 | + Text, |
| 77 | + TouchableHighlight, |
| 78 | + View |
| 79 | +} from 'react-native'; |
| 80 | + |
| 81 | +var PSPDFKit = NativeModules.PSPDFKit; |
| 82 | + |
| 83 | +PSPDFKit.setLicenseKey('INSERT_YOUR_LICENSE_KEY_HERE'); |
| 84 | + |
| 85 | +// Change 'YourApp' to your app's name. |
| 86 | +class YourApp extends Component { |
| 87 | + _onPressButton() { |
| 88 | + PSPDFKit.present('document.pdf', {}) |
| 89 | + } |
| 90 | + |
| 91 | + render() { |
| 92 | + return ( |
| 93 | + <View style={styles.container}> |
| 94 | + <TouchableHighlight onPress={this._onPressButton}> |
| 95 | + <Text style={styles.text}>Tap to Open Document</Text> |
| 96 | + </TouchableHighlight> |
| 97 | + </View> |
| 98 | + ); |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +const styles = StyleSheet.create({ |
| 103 | + container: { |
| 104 | + flex: 1, |
| 105 | + justifyContent: 'center', |
| 106 | + alignItems: 'center', |
| 107 | + backgroundColor: '#F5FCFF', |
| 108 | + }, |
| 109 | + text: { |
| 110 | + fontSize: 20, |
| 111 | + textAlign: 'center', |
| 112 | + margin: 10, |
| 113 | + } |
| 114 | +}); |
| 115 | + |
| 116 | +// Change both 'YourApp's to your app's name. |
| 117 | +AppRegistry.registerComponent('YourApp', () => YourApp); |
| 118 | +``` |
| 119 | + |
| 120 | +Your app is now ready to launch. Run the app in Xcode or type `react-native run-ios` in the terminal. |
0 commit comments