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

Consider App Extension environment #23

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
2 changes: 2 additions & 0 deletions Example/Podfile
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,6 @@ target 'RxKeyboardExample' do
pod 'SwiftyColor'
pod 'SwiftyImage'
pod 'CGFloatLiteral'

target 'RxKeyboardShareExtension'
end
8 changes: 4 additions & 4 deletions Example/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ PODS:
- ReusableKit (1.1.0)
- RxCocoa (3.1.0):
- RxSwift (~> 3.1)
- RxKeyboard (0.4.0):
- RxKeyboard (0.4.1):
- RxCocoa (>= 3.0)
- RxSwift (>= 3.0)
- RxSwift (3.1.0)
Expand Down Expand Up @@ -34,14 +34,14 @@ SPEC CHECKSUMS:
ManualLayout: 68ac8cfa6b5f656f7a9fadec3730208b95986880
ReusableKit: 89e743a120bab0d95848bff27e481833c2768905
RxCocoa: 50d7564866da9299161e86a263ce12a0873904d8
RxKeyboard: cf7f4463f27e580a3542127877ef9e3a71183744
RxKeyboard: 6bd1a1e1e199d869699ed8da2307b474137ec28f
RxSwift: 83ff553e7593fdfdcb2562933a64c0284dffdadc
SnapKit: 12b24f569cb7c143acc9c22b9d91b23e7b1c84a2
SwiftyColor: 7fa09db14051bc5d7f539e1c4576665975225992
SwiftyImage: 79090f8b96057b3e91064de172936b97855086e6
Then: 8496793c8fcac95482b943909b440724c7d8d82c
UITextView+Placeholder: 77680995fcdd07c3f52ec92fe1150874a2ac89ff

PODFILE CHECKSUM: 49cf5def62bcfa2dfbe0c5118a9372a4162ec992
PODFILE CHECKSUM: 586f03c5a1d10260c3fad008f55a493a57b525a5

COCOAPODS: 1.1.1
COCOAPODS: 1.2.1
199 changes: 197 additions & 2 deletions Example/RxKeyboardExample.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="11134" systemVersion="15F34" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" colorMatched="YES" initialViewController="j1y-V4-xli">
<dependencies>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="11106"/>
<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="target" sceneMemberID="viewController">
<layoutGuides>
<viewControllerLayoutGuide type="top" id="8bI-gs-bmD"/>
<viewControllerLayoutGuide type="bottom" id="d5i-Ba-RvD"/>
</layoutGuides>
<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"/>
</view>
</viewController>
<placeholder placeholderIdentifier="IBFirstResponder" id="CEy-Cv-SGf" userLabel="First Responder" sceneMemberID="firstResponder"/>
</objects>
</scene>
</scenes>
</document>
36 changes: 36 additions & 0 deletions Example/RxKeyboardShareExtension/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>en</string>
<key>CFBundleDisplayName</key>
<string>RxKeyboardShareExtension</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>1.0</string>
<key>CFBundleVersion</key>
<string>1</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>
53 changes: 53 additions & 0 deletions Example/RxKeyboardShareExtension/ShareViewController.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
//
// ShareViewController.swift
// RxKeyboardShareExtension
//
// Created by Suyeol Jeon on 04/05/2017.
// Copyright © 2017 Suyeol Jeon. All rights reserved.
//

import UIKit
import Social
import RxKeyboard
import RxSwift
import SnapKit

class ShareViewController: UIViewController {
let disposeBag = DisposeBag()
let textView = UITextView()
let toolbar = UIToolbar()

override func viewDidLoad() {
super.viewDidLoad()
self.textView.font = UIFont.systemFont(ofSize: 50)
self.textView.text = (0...100).map { "\($0)" }.joined(separator: "\n")
self.textView.keyboardDismissMode = .interactive
self.view.addSubview(self.textView)
self.view.addSubview(self.toolbar)

self.textView.snp.makeConstraints { make in
make.edges.equalToSuperview()
}
self.toolbar.snp.makeConstraints { make in
make.left.right.equalToSuperview()
make.bottom.equalToSuperview()
}

// Set `gestureValue` for using interactive keyboard tracking
RxKeyboard.instance.gestureView = self.view
RxKeyboard.instance.visibleHeight
.drive(onNext: { [weak self] visibleHeight in
guard let `self` = self else { return }
self.toolbar.snp.updateConstraints { make in
make.bottom.equalTo(-visibleHeight)
}
self.view.setNeedsLayout()
UIView.animate(withDuration: 0) {
self.textView.contentInset.bottom = visibleHeight + self.toolbar.frame.height
self.textView.scrollIndicatorInsets.bottom = self.textView.contentInset.bottom
self.view.layoutIfNeeded()
}
})
.addDisposableTo(self.disposeBag)
}
}
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,16 @@ RxKeyboard.instance.frame

> **Note**: In real world, you should use `setNeedsLayout()` and `layoutIfNeeded()` with animation block. See the [example project](https://github.com/RxSwiftCommunity/RxKeyboard/blob/master/Example/Sources/ViewControllers/MessageListViewController.swift#L92-L105) for example.

- <a name="tip-app-extension" href="#tip-app-extension">🔗</a> **I want to use RxKeyboard in App Extension.**

App Extesion doesn't allow the `UIApplication.shared` API so the pan gesture recognizer cannot automatically track the interactive keybord dismissing. You have to do something manually in the App Extension target: setting `gestureView`.

```swift
// ShareViewController.swift
RxKeyboard.instance.gestureView = self.view // add this line on `viewDidLoad()`
RxKeyboard.instance.visibleHeight.drive(...)
```

- Anything else? Please open an issue or make a Pull Request.

## Dependencies
Expand Down
2 changes: 2 additions & 0 deletions RxKeyboard.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,7 @@
03A258D41DA94EC50035A85B /* Debug */ = {
isa = XCBuildConfiguration;
buildSettings = {
APPLICATION_EXTENSION_API_ONLY = NO;
CLANG_ENABLE_MODULES = YES;
CODE_SIGN_IDENTITY = "";
DEFINES_MODULE = YES;
Expand All @@ -302,6 +303,7 @@
03A258D51DA94EC50035A85B /* Release */ = {
isa = XCBuildConfiguration;
buildSettings = {
APPLICATION_EXTENSION_API_ONLY = NO;
CLANG_ENABLE_MODULES = YES;
CODE_SIGN_IDENTITY = "";
DEFINES_MODULE = YES;
Expand Down
26 changes: 22 additions & 4 deletions Sources/RxKeyboard.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,17 @@ public class RxKeyboard: NSObject {
/// Get a singleton instance.
public static let instance = RxKeyboard()

/// A view that receives pan gesture. The default value is the AppDelegate's window. Set this
/// value with the root view controller's view if the target is an App Extension.
public var gestureView: UIView? {
get { return self._gestureView ?? self.application?.delegate?.window ?? nil }
set {
self._gestureView = newValue
self.panRecognizer.view?.removeGestureRecognizer(self.panRecognizer)
newValue?.addGestureRecognizer(self.panRecognizer)
}
}

/// An observable keyboard frame.
public let frame: Driver<CGRect>

Expand All @@ -36,6 +47,12 @@ public class RxKeyboard: NSObject {
fileprivate let disposeBag = DisposeBag()
fileprivate let panRecognizer = UIPanGestureRecognizer()

fileprivate let application: UIApplication? = {
let selector = NSSelectorFromString("sharedApplication")
return UIApplication.perform(selector)?.takeRetainedValue() as? UIApplication
}()
fileprivate var _gestureView: UIView?


// MARK: Initializing

Expand Down Expand Up @@ -93,10 +110,10 @@ public class RxKeyboard: NSObject {
.withLatestFrom(frameVariable.asObservable()) { ($0, $1) }
.flatMap { (gestureRecognizer, frame) -> Observable<CGRect> in
guard case .changed = gestureRecognizer.state,
let window = UIApplication.shared.windows.first,
let view = gestureRecognizer.view,
frame.origin.y < UIScreen.main.bounds.height
else { return .empty() }
let origin = gestureRecognizer.location(in: window)
let origin = gestureRecognizer.location(in: view)
var newFrame = frame
newFrame.origin.y = max(origin.y, UIScreen.main.bounds.height - frame.height)
return .just(newFrame)
Expand All @@ -112,8 +129,9 @@ public class RxKeyboard: NSObject {
NotificationCenter.default.rx.notification(.UIApplicationDidFinishLaunching)
.map { _ in Void() }
.startWith(Void()) // when RxKeyboard is initialized before UIApplication.window is created
.subscribe(onNext: { _ in
UIApplication.shared.windows.first?.addGestureRecognizer(self.panRecognizer)
.subscribe(onNext: { [weak self] _ in
guard let `self` = self else { return }
self.gestureView?.addGestureRecognizer(self.panRecognizer)
})
.disposed(by: self.disposeBag)
}
Expand Down