Skip to content

Commit

Permalink
Swift5 (#1)
Browse files Browse the repository at this point in the history
* Change to Swift 5 for DemoApp.

* Try upload different TwitterKit.framework.

* Update README.md.
Update Swift version for TwitterKitTest.

* Try to remove UIWebView.

* Try to migrate to WKWebView.

* Add setScalesPageToFit to WKWebView;
Clean up the code.

* Move TwitterKit.podspec to root folder.

* Add new pod, TwitterKit5.podspec.
  • Loading branch information
touren authored Sep 23, 2019
1 parent ac42e13 commit 06f4112
Show file tree
Hide file tree
Showing 19 changed files with 133 additions and 41 deletions.
5 changes: 3 additions & 2 deletions DemoApp/DemoApp.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -544,6 +544,7 @@
developmentRegion = English;
hasScannedForEncodings = 0;
knownRegions = (
English,
en,
Base,
);
Expand Down Expand Up @@ -926,7 +927,7 @@
PROVISIONING_PROFILE_SPECIFIER = "";
SWIFT_OBJC_BRIDGING_HEADER = "DemoApp/DemoApp-Bridging-Header.h";
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
SWIFT_VERSION = 3.0;
SWIFT_VERSION = 5.0;
};
name = Debug;
};
Expand All @@ -947,7 +948,7 @@
PRODUCT_NAME = "$(TARGET_NAME)";
PROVISIONING_PROFILE_SPECIFIER = "";
SWIFT_OBJC_BRIDGING_HEADER = "DemoApp/DemoApp-Bridging-Header.h";
SWIFT_VERSION = 3.0;
SWIFT_VERSION = 5.0;
};
name = Release;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ class AuthenticationViewController: UIViewController {

// MARK: - Actions

func home() {
@objc func home() {
delegate?.authenticationViewControllerDidTapHome(viewController: self)
}

Expand Down
6 changes: 3 additions & 3 deletions DemoApp/DemoApp/Authentication/LoginViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -120,11 +120,11 @@ class LoginViewController: UIViewController {

// MARK: - Actions

func backgroundTap() {
@objc func backgroundTap() {
dismiss(animated: true, completion: nil)
}

func login() {
@objc func login() {
TWTRTwitter.sharedInstance().logIn(with: self) { (session, error) in
if let session = session {
self.dismiss(animated: true) {
Expand All @@ -136,7 +136,7 @@ class LoginViewController: UIViewController {
}
}

func clearAccounts() {
@objc func clearAccounts() {
for session in TWTRTwitter.sharedInstance().sessionStore.existingUserSessions() {
if let session = session as? TWTRSession {
TWTRTwitter.sharedInstance().sessionStore.logOutUserID(session.userID)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class TwitterLoginCollectionViewCell: UICollectionViewCell {

// MARK: - Actions

func addAccount() {
@objc func addAccount() {
delegate?.loginCollectionViewCellDidTapAddAccountButton(cell: self)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ class TwitterSessionCollectionViewCell: UICollectionViewCell {

// MARK: - Actions

func logout() {
@objc func logout() {
if let session = session {
delegate?.sessionCollectionViewCell(collectionViewCell: self, didTapLogoutFor: session)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,9 +151,9 @@ extension TweetComposerViewController: UIImagePickerControllerDelegate, UINaviga
}
}

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
private func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
picker.dismiss(animated: true)
if let image = info[UIImagePickerControllerOriginalImage] as? UIImage {
if let image = info[UIImagePickerController.InfoKey.originalImage.rawValue] as? UIImage {
let composer = TWTRComposerViewController(initialText: "Check out this great image: ", image: image, videoURL: nil)
composer.delegate = self
self.present(composer, animated: true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,19 +44,19 @@ class ESPNTimelineViewController: TWTRTimelineViewController {
@available(iOS 8.0, *)
override func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {

let moreRowAction = UITableViewRowAction(style: UITableViewRowActionStyle.default, title: "More", handler:{action, indexpath in
let moreRowAction = UITableViewRowAction(style: UITableViewRowAction.Style.default, title: "More", handler:{action, indexpath in
print("More Action")
})
moreRowAction.backgroundColor = UIColor(red: 0.298, green: 0.851, blue: 0.3922, alpha: 1.0);

let deleteRowAction = UITableViewRowAction(style: UITableViewRowActionStyle.default, title: "Delete", handler:{action, indexpath in
let deleteRowAction = UITableViewRowAction(style: UITableViewRowAction.Style.default, title: "Delete", handler:{action, indexpath in
print("Delete Action")
});

return [deleteRowAction, moreRowAction]
}

override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ class SearchTimelineViewController: TWTRTimelineViewController, DZNEmptyDataSetS

func title(forEmptyDataSet scrollView: UIScrollView) -> NSAttributedString {
let text = "Could not find Tweets.";
let attributes = [NSFontAttributeName: UIFont.boldSystemFont(ofSize: 18),
NSForegroundColorAttributeName: UIColor.darkGray]
let attributes = [NSAttributedString.Key.font: UIFont.boldSystemFont(ofSize: 18),
NSAttributedString.Key.foregroundColor: UIColor.darkGray]

return NSAttributedString(string: text, attributes: attributes)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class TweetOptionTableViewCell: UITableViewCell {

// MARK: - Init

required override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
required override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)

contentView.addSubview(titleLabel)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ class TweetViewStylerViewController: UIViewController {

// MARK: - Actions

func updateTweetView() {
@objc func updateTweetView() {
if let tweet = tweet {
configureTweetView(with: tweet)
}
Expand Down
2 changes: 1 addition & 1 deletion DemoApp/DemoApp/Extensions/ViewExtensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import Foundation


extension UIViewController {
func addVisualConstraints(format: String, views: [String: AnyObject], options: NSLayoutFormatOptions = [], metrics: [String : AnyObject]? = nil) {
func addVisualConstraints(format: String, views: [String: AnyObject], options: NSLayoutConstraint.FormatOptions = [], metrics: [String : AnyObject]? = nil) {
let constraints = NSLayoutConstraint.constraints(withVisualFormat: format, options: options, metrics: metrics, views: views)
constraints.forEach { $0.isActive = true }
}
Expand Down
2 changes: 1 addition & 1 deletion DemoApp/DemoApp/Navigation/HomeViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class HomeViewController: DemoCollectionViewController {

// MARK: - Actions

func didTapProfile() {
@objc func didTapProfile() {
delegate?.homeViewControllerDidTapProfileButton(viewController: self)
}
}
8 changes: 4 additions & 4 deletions DemoApp/DemoApp/Navigation/RootAnimationController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ struct RootAnimationController {
func transition(using transitionContext: RootTransitionContext) {
let toViewController = transitionContext.toViewController

transitionContext.contentViewController.addChildViewController(toViewController)
transitionContext.contentViewController.addChild(toViewController)
toViewController.view.frame = transitionContext.contentViewController.view.bounds
transitionContext.contentViewController.view.addSubview(toViewController.view)
transitionContext.fromViewController?.willMove(toParentViewController: nil)
transitionContext.toViewController.willMove(toParentViewController: transitionContext.contentViewController)
transitionContext.fromViewController?.willMove(toParent: nil)
transitionContext.toViewController.willMove(toParent: transitionContext.contentViewController)

animateTransition(using: transitionContext)
}
Expand All @@ -41,7 +41,7 @@ struct RootAnimationController {

private func handleAnimationCompletion(using transitionContext: RootTransitionContext?) {
transitionContext?.fromViewController?.view.removeFromSuperview()
transitionContext?.fromViewController?.removeFromParentViewController()
transitionContext?.fromViewController?.removeFromParent()
if let constraints = transitionContext?.fromViewController?.view.constraints {
transitionContext?.fromViewController?.view.removeConstraints(constraints)
}
Expand Down
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,25 @@
**How to build and use your customized TwitterKit pod**
1. Open the DemoApp project and there is a sub project TwitterKit.
2. Select schema: TwitterKit ==> Generic iOS Device, `Clean`, `Run` and you get a TwitterKit.framework with architecture: armv7, arm64
3. Select schema: TwitterKit ==> any iOS Simulator, `Clean`, `Run` and you get a TwitterKit.framework with architecture: x86_64
4. Use `lipo` to merge above two framework into one, which include architecture: armv7, arm64, x84_64
```
lipo -create -output TwitterKit <Framework built in step 2>/TwitterKit <Framework built in step 3>/TwitterKit
```
Check the new TwitterKit is correct.
```
lipo -archs TwitterKit
```
Will return `x86_64 armv7 arm64 `
5. Replace the `TwitterKit` in framework created in step 2 with the merged one.
6. Create a folder `iOS`, move the framework folder in step 5 inside, zip the `iOS` folder and you get TwitterKit pod zip file.
7. Upload the zip file somewhere and get a [URL](https://swarm-dev.s3.amazonaws.com/pods/twitterkit/ios/5.0.0/TwitterKit.zip) points to it.
8. Change your [podspec file](https://raw.githubusercontent.com/touren/twitter-kit-ios/Swift5/TwitterKit/TwitterKit.podspec) as: s.source = { :http => "<URL created in step 7>" }
9. Change your Podfile as: `pod "TwitterKit"` ==> `pod "TwitterKit", :podspec => "<URL point to the podspec created in step 8>"`
----
**Twitter will be discontinuing support for Twitter Kit on October 31, 2018. [Read the blog post here](https://blog.twitter.com/developer/en_us/topics/tools/2018/discontinuing-support-for-twitter-kit-sdk.html).**
# Twitter Kit for iOS
Expand Down
9 changes: 7 additions & 2 deletions TwitterKit/TwitterKit.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -498,6 +498,7 @@
AAF0C9ED2011998F0057F438 /* TwitterShareExtensionUI.h in Headers */ = {isa = PBXBuildFile; fileRef = AAF0C9EC2011998F0057F438 /* TwitterShareExtensionUI.h */; };
AAF0C9F120119C0A0057F438 /* CoreLocation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = AAF0C9EE20119BB40057F438 /* CoreLocation.framework */; };
AAF0C9F220119C2C0057F438 /* MapKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = AAF0C9F020119BED0057F438 /* MapKit.framework */; };
ACB17A0F2335DC330078FDF3 /* WebKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = ACB17A0C2335DC320078FDF3 /* WebKit.framework */; };
BF318EB21ADF320A0082353A /* TWTRDateFormatter.h in Headers */ = {isa = PBXBuildFile; fileRef = 9D0AE5A71AC7359D00884B45 /* TWTRDateFormatter.h */; settings = {ATTRIBUTES = (Private, ); }; };
BF318F031AE03B400082353A /* TWTRComposer.h in Headers */ = {isa = PBXBuildFile; fileRef = F5154B8119D4E6130054DECF /* TWTRComposer.h */; settings = {ATTRIBUTES = (Public, ); }; };
BF318F041AE03B4E0082353A /* TWTRTweetView.h in Headers */ = {isa = PBXBuildFile; fileRef = 376CFC20194A7F7F00E982FB /* TWTRTweetView.h */; settings = {ATTRIBUTES = (Public, ); }; };
Expand Down Expand Up @@ -1223,6 +1224,7 @@
AAF0C9EC2011998F0057F438 /* TwitterShareExtensionUI.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = TwitterShareExtensionUI.h; sourceTree = "<group>"; };
AAF0C9EE20119BB40057F438 /* CoreLocation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = CoreLocation.framework; path = System/Library/Frameworks/CoreLocation.framework; sourceTree = SDKROOT; };
AAF0C9F020119BED0057F438 /* MapKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = MapKit.framework; path = System/Library/Frameworks/MapKit.framework; sourceTree = SDKROOT; };
ACB17A0C2335DC320078FDF3 /* WebKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = WebKit.framework; path = Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS12.4.sdk/System/Library/Frameworks/WebKit.framework; sourceTree = DEVELOPER_DIR; };
BF45C7DC1A1AAE24009C58BE /* TWTRAutoLayoutDebugging.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = TWTRAutoLayoutDebugging.h; sourceTree = "<group>"; };
BF45C7DD1A1AAE24009C58BE /* TWTRAutoLayoutDebugging.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = TWTRAutoLayoutDebugging.m; sourceTree = "<group>"; };
BF8EF2DE1AFD4F65008B4829 /* TWTRSessionTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = TWTRSessionTests.m; sourceTree = "<group>"; };
Expand Down Expand Up @@ -1382,6 +1384,7 @@
isa = PBXFrameworksBuildPhase;
buildActionMask = 2147483647;
files = (
ACB17A0F2335DC330078FDF3 /* WebKit.framework in Frameworks */,
AA3E099220119CC000792255 /* MobileCoreServices.framework in Frameworks */,
AAF0C9F220119C2C0057F438 /* MapKit.framework in Frameworks */,
AAF0C9F120119C0A0057F438 /* CoreLocation.framework in Frameworks */,
Expand All @@ -1408,6 +1411,7 @@
208ACAB41FB2800900008F42 /* Frameworks */ = {
isa = PBXGroup;
children = (
ACB17A0C2335DC320078FDF3 /* WebKit.framework */,
AA3E099B20125EC600792255 /* MapKit.framework */,
AA3E099120119CA500792255 /* MobileCoreServices.framework */,
AAF0C9F020119BED0057F438 /* MapKit.framework */,
Expand Down Expand Up @@ -2978,6 +2982,7 @@
developmentRegion = English;
hasScannedForEncodings = 0;
knownRegions = (
English,
en,
ko,
ja,
Expand Down Expand Up @@ -3454,7 +3459,7 @@
SDKROOT = iphoneos;
SWIFT_OBJC_BRIDGING_HEADER = "TwitterKitTests/TwitterKit Tests-Bridging-Header.h";
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
SWIFT_VERSION = 3.0;
SWIFT_VERSION = 5.0;
WRAPPER_EXTENSION = xctest;
};
name = Debug;
Expand All @@ -3473,7 +3478,7 @@
SDKROOT = iphoneos;
SWIFT_OBJC_BRIDGING_HEADER = "TwitterKitTests/TwitterKit Tests-Bridging-Header.h";
SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule";
SWIFT_VERSION = 3.0;
SWIFT_VERSION = 5.0;
WRAPPER_EXTENSION = xctest;
};
name = Release;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,15 @@
*/

#import <UIKit/UIKit.h>
#import <WebKit/WebKit.h>

@class TWTRWebViewController;

typedef BOOL (^TWTRWebViewControllerShouldLoadCompletion)(UIViewController *controller, NSURLRequest *request, UIWebViewNavigationType navigationType);
typedef void (^TWTRWebViewControllerCancelCompletion)(TWTRWebViewController *webViewController);
typedef void (^TWTRWebViewControllerHandleError)(NSError *error);

@interface TWTRWebViewController : UIViewController
@interface TWTRWebViewController : UIViewController <WKNavigationDelegate>

@property (nonatomic, strong) NSURLRequest *request;
@property (nonatomic, copy) TWTRWebViewControllerShouldLoadCompletion shouldStartLoadWithRequest;
Expand Down
70 changes: 57 additions & 13 deletions TwitterKit/TwitterKit/Social/Identity/TWTRWebViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,41 @@
#import "TWTRWebViewController.h"
#import <TwitterCore/TWTRAuthenticationConstants.h>

@interface TWTRWebViewController () <UIWebViewDelegate>
@interface TWTRWebViewController () <WKNavigationDelegate>

@property (nonatomic, strong) UIWebView *webView;
@property (nonatomic, strong) WKWebView *webView;
@property (nonatomic, assign) BOOL showCancelButton;
@property (nonatomic, copy) TWTRWebViewControllerCancelCompletion cancelCompletion;

@end

@implementation TWTRWebViewController

// Conversion from UIWebViewNavigationType to WKNavigationType
+ (UIWebViewNavigationType)_enumHelperForNavigationType:(WKNavigationType)wkNavigationType {
switch (wkNavigationType) {
case WKNavigationTypeLinkActivated:
return UIWebViewNavigationTypeLinkClicked;
break;
case WKNavigationTypeFormSubmitted:
return UIWebViewNavigationTypeFormSubmitted;
break;
case WKNavigationTypeBackForward:
return UIWebViewNavigationTypeBackForward;
break;
case WKNavigationTypeReload:
return UIWebViewNavigationTypeReload;
break;
case WKNavigationTypeFormResubmitted:
return UIWebViewNavigationTypeFormResubmitted;
break;
case WKNavigationTypeOther:
default:
return UIWebViewNavigationTypeOther;
break;
}
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
Expand Down Expand Up @@ -65,30 +90,32 @@ - (void)load

- (void)loadView
{
[self setWebView:[[UIWebView alloc] init]];
[[self webView] setScalesPageToFit:YES];
[[self webView] setDelegate:self];
[self initWebView];
[self setView:[self webView]];
}

#pragma mark - UIWebview delegate
#pragma mark - WKWebview delegate

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler {
NSURLRequest* request = navigationAction.request;
if (![self whitelistedDomain:request]) {
// Open in Safari if request is not whitelisted
NSLog(@"Opening link in Safari browser, as the host is not whitelisted: %@", request.URL);
[[UIApplication sharedApplication] openURL:request.URL];
return NO;
decisionHandler(WKNavigationActionPolicyCancel);
return;
}
WKNavigationActionPolicy decision = WKNavigationActionPolicyAllow;
if ([self shouldStartLoadWithRequest]) {
return [self shouldStartLoadWithRequest](self, request, navigationType);
if (![self shouldStartLoadWithRequest](self, request, [TWTRWebViewController _enumHelperForNavigationType:navigationAction.navigationType])) {
decision = WKNavigationActionPolicyCancel;
};
}
return YES;

decisionHandler(decision);
}

- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
- (void)webView:(WKWebView *)webView didFailNavigation:(WKNavigation *)navigation withError:(NSError *)error {
if (self.errorHandler) {
self.errorHandler(error);
self.errorHandler = nil;
Expand Down Expand Up @@ -120,4 +147,21 @@ - (void)enableCancelButtonWithCancelCompletion:(TWTRWebViewControllerCancelCompl
[self setCancelCompletion:cancelCompletion];
}

- (void)initWebView {
// From: https://stackoverflow.com/questions/26295277/wkwebview-equivalent-for-uiwebviews-scalespagetofit
NSString *jScript = @"var meta = document.createElement('meta'); meta.setAttribute('name', 'viewport'); meta.setAttribute('content', 'width=device-width'); document.getElementsByTagName('head')[0].appendChild(meta);";

WKUserScript *wkUScript = [[WKUserScript alloc] initWithSource:jScript injectionTime:WKUserScriptInjectionTimeAtDocumentEnd forMainFrameOnly:YES];
WKUserContentController *wkUController = [[WKUserContentController alloc] init];
[wkUController addUserScript:wkUScript];

WKWebViewConfiguration *wkWebConfig = [[WKWebViewConfiguration alloc] init];
wkWebConfig.userContentController = wkUController;

WKWebView* wkWebV = [[WKWebView alloc] initWithFrame:self.view.frame configuration:wkWebConfig];

[self setWebView:wkWebV];
[[self webView] setNavigationDelegate:self];

}
@end
Loading

0 comments on commit 06f4112

Please sign in to comment.