Skip to content

Commit

Permalink
Added wkwebview compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
tien271 authored and Tien Nguyen committed Jun 15, 2020
1 parent afff77e commit b764f46
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 75 deletions.
20 changes: 2 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ For example, you can use [PhotoSwipe](https://github.com/dimsemenov/photoswipe)

**Co-maintainer needed**

Currently Android code is pretty stable, iOS has few stability [issues](https://github.com/terikon/cordova-plugin-photo-library/issues).
Currently Android code is pretty stable, iOS has few stability [issues](https://github.com/tien271/cordova-plugin-photo-library-wkwebview/issues).
**Co-maintainer with iOS/Swift knowlege is needed, please [contact](https://github.com/viskin)**.

Contributions are welcome.
Expand All @@ -33,7 +33,7 @@ Please write tests for your contribution.

# Install

cordova plugin add cordova-plugin-photo-library --variable PHOTO_LIBRARY_USAGE_DESCRIPTION="To choose photos" --save
cordova plugin add cordova-plugin-photo-library-wkwebview --variable PHOTO_LIBRARY_USAGE_DESCRIPTION="To choose photos" --save

# Usage

Expand Down Expand Up @@ -312,22 +312,6 @@ var app = angular

TypeScript definitions are provided in [PhotoLibrary.d.ts](https://github.com/terikon/cordova-plugin-photo-library/blob/master/PhotoLibrary.d.ts)

# Tests

The library includes tests in [tests](https://github.com/terikon/cordova-plugin-photo-library/tree/master/tests) folder. All tests are in
[tests.js](https://github.com/terikon/cordova-plugin-photo-library/blob/master/tests/tests.js) file.

# Running tests

## Travis

tcc.db file is located at $HOME/Library/Developer/CoreSimulator/Devices/$DEVICEID/data/Library/TCC/TCC.db

## Helper app

To run tests, use [special photo-library-tester](https://github.com/terikon/photo-library-tester).
It's always useful to run these tests before submitting changes, for each platform (android, ios, browser).

# TODO

- [#38](https://github.com/terikon/cordova-plugin-photo-library/issues/38) browser platform: saveImage and saveVideo should download file.
Expand Down
76 changes: 36 additions & 40 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,40 +1,36 @@
{
"name": "cordova-plugin-photo-library",
"version": "2.3.1",
"description": "Plugin that just gets photos from the gallery",
"main": "index.js",
"scripts": {
"test:android": "cordova-paramedic --platform android --plugin . --verbose",
"test:ios": "cordova-paramedic --platform ios --plugin . --verbose",
"test": "echo \"Run tests with https://github.com/terikon/cordova-plugin-photo-library-tester, or run test:android or test:ios.\" && exit 1",
"copy-test-images:android:emulator": "adb -e push tests/test-images /mnt/sdcard/DCIM && adb -e shell am broadcast -a android.intent.action.MEDIA_MOUNTED -d file:///sdcard",
"copy-test-images:ios:simulator": "xcrun simctl addmedia booted tests/test-images/*"
},
"repository": {
"type": "git",
"url": "git+https://github.com/terikon/cordova-plugin-photo-library.git"
},
"author": {
"name": "Roman Viskin",
"email": "[email protected]",
"url": "http://il.linkedin.com/in/romanviskin"
},
"license": "MIT",
"bugs": {
"url": "https://github.com/terikon/cordova-plugin-photo-library/issues"
},
"homepage": "https://github.com/terikon/cordova-plugin-photo-library#readme",
"devDependencies": {
"eslint": "^6.6.0",
"@types/cordova": "^0.0.34",
"@types/jasmine": "^2.5.41",
"es5-shim": "4.5.9",
"es6-shim": "0.35.3",
"es7-shim": "6.0.0",
"blueimp-canvastoblob": "2.1.0",
"cordova-paramedic": "https://github.com/apache/cordova-paramedic.git"
},
"dependencies": {
"async": "^2.1.4"
}
}
{
"name": "cordova-plugin-photo-library-wkwebview",
"version": "3.0.1",
"description": "Plugin that just gets photos from the gallery",
"main": "index.js",
"scripts": {
"test:android": "cordova-paramedic --platform android --plugin . --verbose",
"test:ios": "cordova-paramedic --platform ios --plugin . --verbose",
"test": "echo \"Run test:android or test:ios.\" && exit 1",
"copy-test-images:android:emulator": "adb -e push tests/test-images /mnt/sdcard/DCIM && adb -e shell am broadcast -a android.intent.action.MEDIA_MOUNTED -d file:///sdcard",
"copy-test-images:ios:simulator": "xcrun simctl addmedia booted tests/test-images/*"
},
"repository": {
"type": "git",
"url": "git+https://github.com/tien271/cordova-plugin-photo-library-wkwebview.git"
},
"author": "Roman Viskin <[email protected]> (http://il.linkedin.com/in/romanviskin)",
"license": "MIT",
"bugs": {
"url": "https://github.com/tien271/cordova-plugin-photo-library-wkwebview/issues"
},
"homepage": "https://github.com/tien271/cordova-plugin-photo-library-wkwebview#readme",
"devDependencies": {
"eslint": "^6.6.0",
"@types/cordova": "^0.0.34",
"@types/jasmine": "^2.5.41",
"es5-shim": "4.5.9",
"es6-shim": "0.35.3",
"es7-shim": "6.0.0",
"blueimp-canvastoblob": "2.1.0",
"cordova-paramedic": "git+https://github.com/apache/cordova-paramedic.git"
},
"dependencies": {
"async": "^2.1.4"
}
}
3 changes: 2 additions & 1 deletion plugin.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?xml version='1.0' encoding='utf-8'?>
<plugin id="cordova-plugin-photo-library" version="2.2.1" xmlns="http://apache.org/cordova/ns/plugins/1.0" xmlns:android="http://schemas.android.com/apk/res/android">
<plugin id="cordova-plugin-photo-library-wkwebview"
version="3.0.1" xmlns="http://apache.org/cordova/ns/plugins/1.0" xmlns:android="http://schemas.android.com/apk/res/android">
<name>Photo Library</name>
<js-module src="www/PhotoLibrary.js" name="PhotoLibrary">
<clobbers target="cordova.plugins.photoLibrary" />
Expand Down
31 changes: 20 additions & 11 deletions src/ios/PhotoLibrary.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import Foundation
import Photos

@objc(PhotoLibrary) class PhotoLibrary : CDVPlugin {

Expand All @@ -9,6 +10,14 @@ import Foundation
// Do not call PhotoLibraryService here, as it will cause permission prompt to appear on app start.

URLProtocol.registerClass(PhotoLibraryProtocol.self)

let status = PHPhotoLibrary.authorizationStatus()

if (status == PHAuthorizationStatus.notDetermined) {
// Access has not been determined.
PHPhotoLibrary.requestAuthorization({ (newStatus) in
})
}

}

Expand All @@ -22,7 +31,7 @@ import Foundation
@objc func getLibrary(_ command: CDVInvokedUrlCommand) {
concurrentQueue.async {

if !PhotoLibraryService.hasPermission() {
if PHPhotoLibrary.authorizationStatus() != .authorized {
let pluginResult = CDVPluginResult(status: CDVCommandStatus_ERROR, messageAs: PhotoLibraryService.PERMISSION_ERROR)
self.commandDelegate!.send(pluginResult, callbackId: command.callbackId)
return
Expand Down Expand Up @@ -79,7 +88,7 @@ import Foundation
@objc func getAlbums(_ command: CDVInvokedUrlCommand) {
concurrentQueue.async {

if !PhotoLibraryService.hasPermission() {
if PHPhotoLibrary.authorizationStatus() != .authorized {
let pluginResult = CDVPluginResult(status: CDVCommandStatus_ERROR, messageAs: PhotoLibraryService.PERMISSION_ERROR)
self.commandDelegate!.send(pluginResult, callbackId: command.callbackId)
return
Expand All @@ -98,7 +107,7 @@ import Foundation

@objc func isAuthorized(_ command: CDVInvokedUrlCommand) {
concurrentQueue.async {
let pluginResult = CDVPluginResult(status: CDVCommandStatus_OK, messageAs: PhotoLibraryService.hasPermission())
let pluginResult = CDVPluginResult(status: CDVCommandStatus_OK, messageAs: PHPhotoLibrary.authorizationStatus() != .authorized)
self.commandDelegate!.send(pluginResult, callbackId: command.callbackId)
}
}
Expand All @@ -107,7 +116,7 @@ import Foundation
@objc func getThumbnail(_ command: CDVInvokedUrlCommand) {
concurrentQueue.async {

if !PhotoLibraryService.hasPermission() {
if PHPhotoLibrary.authorizationStatus() != .authorized {
let pluginResult = CDVPluginResult(status: CDVCommandStatus_ERROR, messageAs: PhotoLibraryService.PERMISSION_ERROR)
self.commandDelegate!.send(pluginResult, callbackId: command.callbackId)
return
Expand Down Expand Up @@ -142,7 +151,7 @@ import Foundation
@objc func getPhoto(_ command: CDVInvokedUrlCommand) {
concurrentQueue.async {

if !PhotoLibraryService.hasPermission() {
if PHPhotoLibrary.authorizationStatus() != .authorized {
let pluginResult = CDVPluginResult(status: CDVCommandStatus_ERROR, messageAs: PhotoLibraryService.PERMISSION_ERROR)
self.commandDelegate!.send(pluginResult, callbackId: command.callbackId)
return
Expand Down Expand Up @@ -172,7 +181,7 @@ import Foundation
@objc func getLibraryItem(_ command: CDVInvokedUrlCommand) {
concurrentQueue.async {

if !PhotoLibraryService.hasPermission() {
if PHPhotoLibrary.authorizationStatus() != .authorized {
let pluginResult = CDVPluginResult(status: CDVCommandStatus_ERROR, messageAs: PhotoLibraryService.PERMISSION_ERROR)
self.commandDelegate!.send(pluginResult, callbackId: command.callbackId)
return
Expand Down Expand Up @@ -230,8 +239,7 @@ import Foundation

@objc func saveImage(_ command: CDVInvokedUrlCommand) {
concurrentQueue.async {

if !PhotoLibraryService.hasPermission() {
if PHPhotoLibrary.authorizationStatus() != .authorized {
let pluginResult = CDVPluginResult(status: CDVCommandStatus_ERROR, messageAs: PhotoLibraryService.PERMISSION_ERROR)
self.commandDelegate!.send(pluginResult, callbackId: command.callbackId)
return
Expand All @@ -241,24 +249,25 @@ import Foundation

let url = command.arguments[0] as! String
let album = command.arguments[1] as! String

NSLog("album: %@, %@", album, url);

service.saveImage(url, album: album) { (libraryItem: NSDictionary?, error: String?) in
if (error != nil) {
let pluginResult = CDVPluginResult(status: CDVCommandStatus_ERROR, messageAs: error)
self.commandDelegate!.send(pluginResult, callbackId: command.callbackId)
} else {
let pluginResult = CDVPluginResult(status: CDVCommandStatus_OK, messageAs: libraryItem as! [String: AnyObject]?)
self.commandDelegate!.send(pluginResult, callbackId: command.callbackId )
self.commandDelegate!.send(pluginResult, callbackId: command.callbackId )
}
}

}
}

@objc func saveVideo(_ command: CDVInvokedUrlCommand) {
concurrentQueue.async {

if !PhotoLibraryService.hasPermission() {
if PHPhotoLibrary.authorizationStatus() != .authorized {
let pluginResult = CDVPluginResult(status: CDVCommandStatus_ERROR, messageAs: PhotoLibraryService.PERMISSION_ERROR)
self.commandDelegate!.send(pluginResult, callbackId: command.callbackId)
return
Expand Down
6 changes: 1 addition & 5 deletions tests/README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
Automatic and manual tests for cordova-plugin-photo-library.
Automatic and manual tests for cordova-plugin-photo-library-wkwebview.
Tests are written with [cordova-plugin-test-framework](https://github.com/apache/cordova-plugin-test-framework). Automatic tests run with [jasmine](https://jasmine.github.io/).

# Running tests

Please use [cordova-plugin-photo-library-tester](https://github.com/terikon/cordova-plugin-photo-library-tester) to run the tests.

# Shims

Tests plugin adds es5, es6 and es7 shims. If needed, update esX-shim.min.js files from node_modules/esX-shim folder.

0 comments on commit b764f46

Please sign in to comment.