From 06f4112a9274c1771347e1918317d51f75959875 Mon Sep 17 00:00:00 2001 From: Tao Ren Date: Mon, 23 Sep 2019 17:14:07 -0400 Subject: [PATCH] Swift5 (#1) * 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. --- DemoApp/DemoApp.xcodeproj/project.pbxproj | 5 +- .../AuthenticationViewController.swift | 2 +- .../Authentication/LoginViewController.swift | 6 +- .../TwitterLoginCollectionViewCell.swift | 2 +- .../TwitterSessionCollectionViewCell.swift | 2 +- .../TweetComposerViewController.swift | 4 +- .../ESPNTimelineViewController.swift | 6 +- .../SearchTimelineViewController.swift | 4 +- .../TweetOptionTableViewCell.swift | 2 +- .../TweetViewStylerViewController.swift | 2 +- .../DemoApp/Extensions/ViewExtensions.swift | 2 +- .../Navigation/HomeViewController.swift | 2 +- .../Navigation/RootAnimationController.swift | 8 +-- README.md | 22 ++++++ .../TwitterKit.xcodeproj/project.pbxproj | 9 ++- .../Social/Identity/TWTRWebViewController.h | 3 +- .../Social/Identity/TWTRWebViewController.m | 70 +++++++++++++++---- .../Identity/TWTRWebViewControllerTests.m | 7 +- TwitterKit5.podspec | 16 +++++ 19 files changed, 133 insertions(+), 41 deletions(-) create mode 100644 TwitterKit5.podspec diff --git a/DemoApp/DemoApp.xcodeproj/project.pbxproj b/DemoApp/DemoApp.xcodeproj/project.pbxproj index 19d480e8..f29f8ceb 100644 --- a/DemoApp/DemoApp.xcodeproj/project.pbxproj +++ b/DemoApp/DemoApp.xcodeproj/project.pbxproj @@ -544,6 +544,7 @@ developmentRegion = English; hasScannedForEncodings = 0; knownRegions = ( + English, en, Base, ); @@ -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; }; @@ -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; }; diff --git a/DemoApp/DemoApp/Authentication/AuthenticationViewController.swift b/DemoApp/DemoApp/Authentication/AuthenticationViewController.swift index 244c4d6f..b93cd890 100644 --- a/DemoApp/DemoApp/Authentication/AuthenticationViewController.swift +++ b/DemoApp/DemoApp/Authentication/AuthenticationViewController.swift @@ -76,7 +76,7 @@ class AuthenticationViewController: UIViewController { // MARK: - Actions - func home() { + @objc func home() { delegate?.authenticationViewControllerDidTapHome(viewController: self) } diff --git a/DemoApp/DemoApp/Authentication/LoginViewController.swift b/DemoApp/DemoApp/Authentication/LoginViewController.swift index bd04d9d0..2509c95c 100644 --- a/DemoApp/DemoApp/Authentication/LoginViewController.swift +++ b/DemoApp/DemoApp/Authentication/LoginViewController.swift @@ -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) { @@ -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) diff --git a/DemoApp/DemoApp/Authentication/TwitterLoginCollectionViewCell.swift b/DemoApp/DemoApp/Authentication/TwitterLoginCollectionViewCell.swift index 02bb25c8..5383588f 100644 --- a/DemoApp/DemoApp/Authentication/TwitterLoginCollectionViewCell.swift +++ b/DemoApp/DemoApp/Authentication/TwitterLoginCollectionViewCell.swift @@ -48,7 +48,7 @@ class TwitterLoginCollectionViewCell: UICollectionViewCell { // MARK: - Actions - func addAccount() { + @objc func addAccount() { delegate?.loginCollectionViewCellDidTapAddAccountButton(cell: self) } diff --git a/DemoApp/DemoApp/Authentication/TwitterSessionCollectionViewCell.swift b/DemoApp/DemoApp/Authentication/TwitterSessionCollectionViewCell.swift index ebd23da4..435d1867 100644 --- a/DemoApp/DemoApp/Authentication/TwitterSessionCollectionViewCell.swift +++ b/DemoApp/DemoApp/Authentication/TwitterSessionCollectionViewCell.swift @@ -112,7 +112,7 @@ class TwitterSessionCollectionViewCell: UICollectionViewCell { // MARK: - Actions - func logout() { + @objc func logout() { if let session = session { delegate?.sessionCollectionViewCell(collectionViewCell: self, didTapLogoutFor: session) } diff --git a/DemoApp/DemoApp/Demos/Complex Tweet Demo/TweetComposerViewController.swift b/DemoApp/DemoApp/Demos/Complex Tweet Demo/TweetComposerViewController.swift index a6f096ed..4f5d35cd 100644 --- a/DemoApp/DemoApp/Demos/Complex Tweet Demo/TweetComposerViewController.swift +++ b/DemoApp/DemoApp/Demos/Complex Tweet Demo/TweetComposerViewController.swift @@ -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) diff --git a/DemoApp/DemoApp/Demos/Timelines Demo/ESPNTimelineViewController.swift b/DemoApp/DemoApp/Demos/Timelines Demo/ESPNTimelineViewController.swift index e107b02e..5fbbde5b 100644 --- a/DemoApp/DemoApp/Demos/Timelines Demo/ESPNTimelineViewController.swift +++ b/DemoApp/DemoApp/Demos/Timelines Demo/ESPNTimelineViewController.swift @@ -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) { } diff --git a/DemoApp/DemoApp/Demos/Timelines Demo/SearchTimelineViewController.swift b/DemoApp/DemoApp/Demos/Timelines Demo/SearchTimelineViewController.swift index 690b5d9a..da943e0c 100644 --- a/DemoApp/DemoApp/Demos/Timelines Demo/SearchTimelineViewController.swift +++ b/DemoApp/DemoApp/Demos/Timelines Demo/SearchTimelineViewController.swift @@ -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) } diff --git a/DemoApp/DemoApp/Demos/TweetView demo/TweetOptionTableViewCell.swift b/DemoApp/DemoApp/Demos/TweetView demo/TweetOptionTableViewCell.swift index a36f8b13..b10c1305 100644 --- a/DemoApp/DemoApp/Demos/TweetView demo/TweetOptionTableViewCell.swift +++ b/DemoApp/DemoApp/Demos/TweetView demo/TweetOptionTableViewCell.swift @@ -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) diff --git a/DemoApp/DemoApp/Demos/TweetView demo/TweetViewStylerViewController.swift b/DemoApp/DemoApp/Demos/TweetView demo/TweetViewStylerViewController.swift index 1d3edbc1..0cf7d912 100644 --- a/DemoApp/DemoApp/Demos/TweetView demo/TweetViewStylerViewController.swift +++ b/DemoApp/DemoApp/Demos/TweetView demo/TweetViewStylerViewController.swift @@ -81,7 +81,7 @@ class TweetViewStylerViewController: UIViewController { // MARK: - Actions - func updateTweetView() { + @objc func updateTweetView() { if let tweet = tweet { configureTweetView(with: tweet) } diff --git a/DemoApp/DemoApp/Extensions/ViewExtensions.swift b/DemoApp/DemoApp/Extensions/ViewExtensions.swift index a6ed0e84..2de9319c 100644 --- a/DemoApp/DemoApp/Extensions/ViewExtensions.swift +++ b/DemoApp/DemoApp/Extensions/ViewExtensions.swift @@ -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 } } diff --git a/DemoApp/DemoApp/Navigation/HomeViewController.swift b/DemoApp/DemoApp/Navigation/HomeViewController.swift index 49244d95..6e45239a 100644 --- a/DemoApp/DemoApp/Navigation/HomeViewController.swift +++ b/DemoApp/DemoApp/Navigation/HomeViewController.swift @@ -43,7 +43,7 @@ class HomeViewController: DemoCollectionViewController { // MARK: - Actions - func didTapProfile() { + @objc func didTapProfile() { delegate?.homeViewControllerDidTapProfileButton(viewController: self) } } diff --git a/DemoApp/DemoApp/Navigation/RootAnimationController.swift b/DemoApp/DemoApp/Navigation/RootAnimationController.swift index 893143a8..fe9b5edc 100644 --- a/DemoApp/DemoApp/Navigation/RootAnimationController.swift +++ b/DemoApp/DemoApp/Navigation/RootAnimationController.swift @@ -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) } @@ -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) } diff --git a/README.md b/README.md index af9bfbdf..172bdc4a 100644 --- a/README.md +++ b/README.md @@ -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 /TwitterKit /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 => "" } +9. Change your Podfile as: `pod "TwitterKit"` ==> `pod "TwitterKit", :podspec => ""` + +---- + **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 diff --git a/TwitterKit/TwitterKit.xcodeproj/project.pbxproj b/TwitterKit/TwitterKit.xcodeproj/project.pbxproj index 16cac24d..57971a6b 100644 --- a/TwitterKit/TwitterKit.xcodeproj/project.pbxproj +++ b/TwitterKit/TwitterKit.xcodeproj/project.pbxproj @@ -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, ); }; }; @@ -1223,6 +1224,7 @@ AAF0C9EC2011998F0057F438 /* TwitterShareExtensionUI.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = TwitterShareExtensionUI.h; sourceTree = ""; }; 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 = ""; }; BF45C7DD1A1AAE24009C58BE /* TWTRAutoLayoutDebugging.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = TWTRAutoLayoutDebugging.m; sourceTree = ""; }; BF8EF2DE1AFD4F65008B4829 /* TWTRSessionTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = TWTRSessionTests.m; sourceTree = ""; }; @@ -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 */, @@ -1408,6 +1411,7 @@ 208ACAB41FB2800900008F42 /* Frameworks */ = { isa = PBXGroup; children = ( + ACB17A0C2335DC320078FDF3 /* WebKit.framework */, AA3E099B20125EC600792255 /* MapKit.framework */, AA3E099120119CA500792255 /* MobileCoreServices.framework */, AAF0C9F020119BED0057F438 /* MapKit.framework */, @@ -2978,6 +2982,7 @@ developmentRegion = English; hasScannedForEncodings = 0; knownRegions = ( + English, en, ko, ja, @@ -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; @@ -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; diff --git a/TwitterKit/TwitterKit/Social/Identity/TWTRWebViewController.h b/TwitterKit/TwitterKit/Social/Identity/TWTRWebViewController.h index b61476ca..3b6d6338 100644 --- a/TwitterKit/TwitterKit/Social/Identity/TWTRWebViewController.h +++ b/TwitterKit/TwitterKit/Social/Identity/TWTRWebViewController.h @@ -20,6 +20,7 @@ */ #import +#import @class TWTRWebViewController; @@ -27,7 +28,7 @@ typedef BOOL (^TWTRWebViewControllerShouldLoadCompletion)(UIViewController *cont typedef void (^TWTRWebViewControllerCancelCompletion)(TWTRWebViewController *webViewController); typedef void (^TWTRWebViewControllerHandleError)(NSError *error); -@interface TWTRWebViewController : UIViewController +@interface TWTRWebViewController : UIViewController @property (nonatomic, strong) NSURLRequest *request; @property (nonatomic, copy) TWTRWebViewControllerShouldLoadCompletion shouldStartLoadWithRequest; diff --git a/TwitterKit/TwitterKit/Social/Identity/TWTRWebViewController.m b/TwitterKit/TwitterKit/Social/Identity/TWTRWebViewController.m index 9fc6101a..1091a6e4 100644 --- a/TwitterKit/TwitterKit/Social/Identity/TWTRWebViewController.m +++ b/TwitterKit/TwitterKit/Social/Identity/TWTRWebViewController.m @@ -18,9 +18,9 @@ #import "TWTRWebViewController.h" #import -@interface TWTRWebViewController () +@interface TWTRWebViewController () -@property (nonatomic, strong) UIWebView *webView; +@property (nonatomic, strong) WKWebView *webView; @property (nonatomic, assign) BOOL showCancelButton; @property (nonatomic, copy) TWTRWebViewControllerCancelCompletion cancelCompletion; @@ -28,6 +28,31 @@ @interface TWTRWebViewController () @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]; @@ -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; @@ -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 diff --git a/TwitterKit/TwitterKitTests/SocialTests/Identity/TWTRWebViewControllerTests.m b/TwitterKit/TwitterKitTests/SocialTests/Identity/TWTRWebViewControllerTests.m index fbbd46b1..b1d60fcb 100644 --- a/TwitterKit/TwitterKitTests/SocialTests/Identity/TWTRWebViewControllerTests.m +++ b/TwitterKit/TwitterKitTests/SocialTests/Identity/TWTRWebViewControllerTests.m @@ -19,12 +19,15 @@ #import "TWTRTestCase.h" #import "TWTRWebViewController.h" -@interface TWTRWebViewController () +@interface TWTRWebViewController () -@property (nonatomic, readonly) UIWebView *webView; +@property (nonatomic, readonly) WKWebView *webView; +- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler; - (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType; +} + @end @interface TWTRWebViewControllerTests : TWTRTestCase diff --git a/TwitterKit5.podspec b/TwitterKit5.podspec new file mode 100644 index 00000000..9e20743e --- /dev/null +++ b/TwitterKit5.podspec @@ -0,0 +1,16 @@ +Pod::Spec.new do |s| + s.name = "TwitterKit5" + s.version = "5.0.1" + s.summary = "Increase user engagement and app growth." + s.homepage = "https://github.com/touren/twitter-kit-ios" + s.documentation_url = "https://github.com/twitter/twitter-kit-ios/wiki" + s.social_media_url = "https://twitter.com/TwitterDev" + s.authors = "Twitter" + s.platform = :ios, "9.0" + s.source = { :http => "https://s3.amazonaws.com/cocoapods.tao/twitterkit/ios/#{s.version}/TwitterKit.zip" } + s.vendored_frameworks = "iOS/TwitterKit.framework" + s.license = { :type => "Commercial", :text => "Twitter Kit: Copyright Twitter, Inc. All Rights Reserved. Use of this software is subject to the terms and conditions of the Twitter Kit Agreement located at https://dev.twitter.com/overview/terms/twitterkit and the Developer Agreement located at https://dev.twitter.com/overview/terms/agreement. OSS: https://github.com/twitter/twitter-kit-ios/blob/master/OS_LICENSES.md"} + s.resources = ["iOS/TwitterKit.framework/TwitterKitResources.bundle", "iOS/TwitterKit.framework/TwitterShareExtensionUIResources.bundle"] + s.frameworks = "CoreText", "QuartzCore", "CoreData", "CoreGraphics", "Foundation", "Security", "UIKit", "CoreMedia", "AVFoundation", "SafariServices" + s.dependency "TwitterCore", ">= 3.1.0" +end