diff --git a/Podfile b/Podfile index 74af1a1eb63c..6a63f734ffd5 100644 --- a/Podfile +++ b/Podfile @@ -14,7 +14,7 @@ pod 'UIDeviceIdentifier', '~> 0.1' pod 'SVProgressHUD', :git => 'https://github.com/TransitApp/SVProgressHUD.git', :commit => '2ac46ae2f2dd37db153ce1daea73b1273ac2bba3' pod 'AMPopTip', '~> 0.7' pod 'wpxmlrpc', '~> 0.7' -pod 'WordPressApi', '~> 0.3.1' +pod 'WordPressApi', '~> 0.3.4' pod 'NSObject-SafeExpectations', '0.0.2' pod 'Mixpanel', '2.5.4' pod 'CocoaLumberjack', '~>1.9' diff --git a/Podfile.lock b/Podfile.lock index 0cbe01a0d591..397c47e9f778 100644 --- a/Podfile.lock +++ b/Podfile.lock @@ -1,5 +1,5 @@ PODS: - - 1PasswordExtension (1.1) + - 1PasswordExtension (1.1.2) - AFNetworking (2.5.1): - AFNetworking/NSURLConnection (= 2.5.1) - AFNetworking/NSURLSession (= 2.5.1) @@ -35,11 +35,11 @@ PODS: - DTFoundation/DTAnimatedGIF (~> 1.7.1) - DTFoundation/DTHTMLParser (~> 1.7.1) - DTFoundation/UIKit (~> 1.7.1) - - DTFoundation/Core (1.7.4) - - DTFoundation/DTAnimatedGIF (1.7.4) - - DTFoundation/DTHTMLParser (1.7.4): + - DTFoundation/Core (1.7.5) + - DTFoundation/DTAnimatedGIF (1.7.5) + - DTFoundation/DTHTMLParser (1.7.5): - DTFoundation/Core - - DTFoundation/UIKit (1.7.4): + - DTFoundation/UIKit (1.7.5): - DTFoundation/Core - EmailChecker (0.1) - Google-Diff-Match-Patch (0.0.1) @@ -110,7 +110,7 @@ PODS: - WordPress-iOS-Shared (0.2.2): - AFNetworking (~> 2.5.1) - CocoaLumberjack (~> 1.9) - - WordPressApi (0.3.1): + - WordPressApi (0.3.4): - AFNetworking (~> 2.5.1) - wpxmlrpc (~> 0.7) - WordPressCom-Analytics-iOS (0.0.26) @@ -152,7 +152,7 @@ DEPENDENCIES: - WordPress-AppbotX (from `https://github.com/wordpress-mobile/appbotx.git`, commit `303b8068530389ea87afde38b77466d685fe3210`) - WordPress-iOS-Editor (from `git://github.com/wordpress-mobile/WordPress-Editor-iOS`, commit `1051403661ca8dbbdbc691b2bac8f30f1906cf15`) - WordPress-iOS-Shared (= 0.2.2) - - WordPressApi (~> 0.3.1) + - WordPressApi (~> 0.3.4) - WordPressCom-Analytics-iOS (= 0.0.26) - WordPressCom-Stats-iOS (from `git://github.com/wordpress-mobile/WordPressCom-Stats-iOS`, commit `3d9cc7988d0b8df44a76c817415d4b36b4201e49`) - wpxmlrpc (~> 0.7) @@ -200,14 +200,14 @@ CHECKOUT OPTIONS: :git: git://github.com/wordpress-mobile/WordPressCom-Stats-iOS SPEC CHECKSUMS: - 1PasswordExtension: 37d1488cf049b8bc0811beb7b52a19f8f803f7c8 + 1PasswordExtension: 38b5be7dd6dbe786c98f15d51d50ad21150db60a AFNetworking: 8bee59492a6ff15d69130efa4d0dc67e0094a52a AMPopTip: dc5bdb3a88bc2ed0a7ac9799e0a5d47c5211721f CocoaLumberjack: 205769c032b5fef85b92472046bcc8b7e7c8a817 CrashlyticsLumberjack: 192a08e07648e4e09d44052356b53ab839cc8a1b CTAssetsPickerController: 319a91106b7b323446b39ab944a3f14db0a1ca46 DTCoreText: 84eb8ba2e70448fdd9d837540e371f9f587b65ba - DTFoundation: 721a63964fb9acd64cb8c9d3d1642ee60bac3d6a + DTFoundation: bea4e07459f0f88a5aa3b8258fab1576bbe2e58c EmailChecker: e909e6d113fac7ad5e3c304fabce30fc4a79c80e Google-Diff-Match-Patch: 1f9c0b972c351d9c0a4fee137eb96896460477cd google-plus-ios-sdk: bb7b918c2ee0d5f5e389c4fddc06d71995481f67 @@ -234,7 +234,7 @@ SPEC CHECKSUMS: WordPress-AppbotX: 8bb0ad23af141e85db282ce09b58e5b3604cf54c WordPress-iOS-Editor: 0e53faba113a3b81439226acd4265835fe985bf6 WordPress-iOS-Shared: f66cc48dae11681d24c9abf36724a6ec28014409 - WordPressApi: 95a1b20372c3a5052f4d3bc4d333f72009c3d986 + WordPressApi: 099a644955625cbae926c668fc6f009a7f5972d8 WordPressCom-Analytics-iOS: 3616aae427dbe1bdec6169ed3e1ac190af63f09e WordPressCom-Stats-iOS: 1aeb8c7279bd5ef5a4b6528696360d1315352637 wpxmlrpc: 75ad2e4663c3a65948f5ae0799d948a8445bb0cf diff --git a/WordPress/Classes/Categories/UIDevice+Helpers.h b/WordPress/Classes/Categories/UIDevice+Helpers.h new file mode 100644 index 000000000000..e318d1d239f3 --- /dev/null +++ b/WordPress/Classes/Categories/UIDevice+Helpers.h @@ -0,0 +1,6 @@ +#import + +@interface UIDevice (Helpers) +- (NSString *)wordPressUserAgent; +- (NSString *)wordPressIdentifier; +@end diff --git a/WordPress/Classes/Categories/UIDevice+WordPressIdentifier.m b/WordPress/Classes/Categories/UIDevice+Helpers.m similarity index 61% rename from WordPress/Classes/Categories/UIDevice+WordPressIdentifier.m rename to WordPress/Classes/Categories/UIDevice+Helpers.m index 97da4d1797fb..7d4c9cf4f88e 100644 --- a/WordPress/Classes/Categories/UIDevice+WordPressIdentifier.m +++ b/WordPress/Classes/Categories/UIDevice+Helpers.m @@ -1,10 +1,22 @@ -#import "UIDevice+WordPressIdentifier.h" +#import "UIDevice+Helpers.h" static NSString * const WordPressIdentifierDefaultsKey = @"WordPressIdentifier"; -@implementation UIDevice (WordPressIdentifier) +@implementation UIDevice (Helpers) -- (NSString *)wordpressIdentifier +- (NSString *)wordPressUserAgent +{ + NSString *appVersion = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"]; + NSString *userAgent = [NSString stringWithFormat:@"wp-iphone/%@ (%@ %@, %@) Mobile", + appVersion, + self.systemName, + self.systemVersion, + self.model]; + + return userAgent; +} + +- (NSString *)wordPressIdentifier { NSString *uuid; if ([[UIDevice currentDevice] respondsToSelector:@selector(identifierForVendor)]) { diff --git a/WordPress/Classes/Categories/UIDevice+WordPressIdentifier.h b/WordPress/Classes/Categories/UIDevice+WordPressIdentifier.h deleted file mode 100644 index 7318a9af218c..000000000000 --- a/WordPress/Classes/Categories/UIDevice+WordPressIdentifier.h +++ /dev/null @@ -1,5 +0,0 @@ -#import - -@interface UIDevice (WordPressIdentifier) -- (NSString *)wordpressIdentifier; -@end diff --git a/WordPress/Classes/Models/Blog+Jetpack.m b/WordPress/Classes/Models/Blog+Jetpack.m index 9284396451e7..7e07b7adffc0 100644 --- a/WordPress/Classes/Models/Blog+Jetpack.m +++ b/WordPress/Classes/Models/Blog+Jetpack.m @@ -76,7 +76,7 @@ - (void)validateJetpackUsername:(NSString *)username AFHTTPRequestOperationManager* operationManager = [[AFHTTPRequestOperationManager alloc] initWithBaseURL:[NSURL URLWithString:BlogJetpackApiBaseUrl]]; - NSString* userAgent = [[WordPressAppDelegate sharedWordPressApplicationDelegate] applicationUserAgent]; + NSString *userAgent = [[WordPressAppDelegate sharedWordPressApplicationDelegate] applicationUserAgent]; operationManager.requestSerializer = [[AFJSONRequestSerializer alloc] init]; [operationManager.requestSerializer setAuthorizationHeaderFieldWithUsername:username password:password]; @@ -157,7 +157,7 @@ - (void)saveJetpackUsername:(NSString *)username password:password success:^(NSString *authToken) { AccountService *accountService = [[AccountService alloc] initWithManagedObjectContext:self.managedObjectContext]; - WPAccount *account = [accountService createOrUpdateWordPressComAccountWithUsername:username password:password authToken:authToken]; + WPAccount *account = [accountService createOrUpdateWordPressComAccountWithUsername:username authToken:authToken]; self.jetpackAccount = account; [account addJetpackBlogsObject:self]; [self dataSave]; diff --git a/WordPress/Classes/Models/Blog.h b/WordPress/Classes/Models/Blog.h index 5e68b50bd068..d2c61a5b5eda 100644 --- a/WordPress/Classes/Models/Blog.h +++ b/WordPress/Classes/Models/Blog.h @@ -46,6 +46,7 @@ @property (nonatomic, weak, readonly) NSString *version; @property (nonatomic, strong, readonly) NSString *username; @property (nonatomic, strong, readonly) NSString *password; +@property (nonatomic, strong, readonly) NSString *authToken; /** diff --git a/WordPress/Classes/Models/Blog.m b/WordPress/Classes/Models/Blog.m index 71d201354d32..c72ba61cacfb 100644 --- a/WordPress/Classes/Models/Blog.m +++ b/WordPress/Classes/Models/Blog.m @@ -307,10 +307,8 @@ - (void)setXmlrpc:(NSString *)xmlrpc - (NSArray *)getXMLRPCArgsWithExtra:(id)extra { NSMutableArray *result = [NSMutableArray array]; - NSString *password = self.password; - if (!password) { - password = @""; - } + NSString *password = self.password ?: [NSString string]; + [result addObject:self.blogID]; [result addObject:self.username]; [result addObject:password]; @@ -342,10 +340,12 @@ - (NSString *)username - (NSString *)password { - WPAccount *account = self.account; - NSString *password = account.password ?: @""; + return self.account.password ?: @""; +} - return password; +- (NSString *)authToken +{ + return self.account.authToken; } - (BOOL)supportsFeaturedImages diff --git a/WordPress/Classes/Models/WPAccount.h b/WordPress/Classes/Models/WPAccount.h index 8c4490749d8a..0030d06feef6 100644 --- a/WordPress/Classes/Models/WPAccount.h +++ b/WordPress/Classes/Models/WPAccount.h @@ -24,7 +24,7 @@ @property (nonatomic, strong) Blog *defaultBlog; /** - The account's password + The self hosted account's password Note that the password is stored using the keychain, not core data */ diff --git a/WordPress/Classes/Models/WPAccount.m b/WordPress/Classes/Models/WPAccount.m index 9481f67c8b88..d0a42b7f4716 100644 --- a/WordPress/Classes/Models/WPAccount.m +++ b/WordPress/Classes/Models/WPAccount.m @@ -52,6 +52,10 @@ - (void)didTurnIntoFault - (NSString *)password { + if (self.isWpcom) { + return nil; + } + return [SFHFKeychainUtils getPasswordForUsername:self.username andServiceName:self.xmlrpc error:nil]; } diff --git a/WordPress/Classes/Networking/PostServiceRemoteXMLRPC.m b/WordPress/Classes/Networking/PostServiceRemoteXMLRPC.m index 6c0eaaeb5777..6f4e9bd17d66 100644 --- a/WordPress/Classes/Networking/PostServiceRemoteXMLRPC.m +++ b/WordPress/Classes/Networking/PostServiceRemoteXMLRPC.m @@ -113,7 +113,10 @@ - (void)updatePost:(RemotePost *)post success:(void (^)(RemotePost *))success failure:(void (^)(NSError *))failure { - NSParameterAssert([post.postID integerValue] > 0); + NSParameterAssert(post.postID.integerValue > 0); + NSParameterAssert(blog.username); + NSParameterAssert(blog.password); + if ([post.postID integerValue] <= 0) { if (failure) { NSDictionary *userInfo = @{NSLocalizedDescriptionKey: @"Can't edit a post if it's not in the server"}; @@ -127,6 +130,7 @@ - (void)updatePost:(RemotePost *)post NSDictionary *extraParameters = [self parametersWithRemotePost:post]; NSArray *parameters = @[post.postID, blog.username, blog.password, extraParameters]; + [self.api callMethod:@"metaWeblog.editPost" parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) { diff --git a/WordPress/Classes/Services/AccountService.h b/WordPress/Classes/Services/AccountService.h index c0a46f6aeb93..105d831b3d32 100644 --- a/WordPress/Classes/Services/AccountService.h +++ b/WordPress/Classes/Services/AccountService.h @@ -52,13 +52,11 @@ extern NSString *const WPAccountEmailAndDefaultBlogUpdatedNotification; Uses a background managed object context. @param username the WordPress.com account's username - @param password the WordPress.com account's password - @param authToken the OAuth2 token returned by signIntoWordPressDotComWithUsername:password:success:failure: + @param authToken the OAuth2 token returned by signIntoWordPressDotComWithUsername:authToken: @return a WordPress.com `WPAccount` object for the given `username` - @see createOrUpdateWordPressComAccountWithUsername:password:authToken:context: + @see createOrUpdateWordPressComAccountWithUsername:authToken: */ - (WPAccount *)createOrUpdateWordPressComAccountWithUsername:(NSString *)username - password:(NSString *)password authToken:(NSString *)authToken; /** @@ -94,4 +92,9 @@ extern NSString *const WPAccountEmailAndDefaultBlogUpdatedNotification; */ - (void)updateEmailAndDefaultBlogForWordPressComAccount:(WPAccount *)account; +/** + Removes your default WordPress.com password from the keychain, if needed. + */ +- (void)removeWordPressComAccountPasswordIfNeeded; + @end diff --git a/WordPress/Classes/Services/AccountService.m b/WordPress/Classes/Services/AccountService.m index b4e6f01f21f9..04d8e6db4349 100644 --- a/WordPress/Classes/Services/AccountService.m +++ b/WordPress/Classes/Services/AccountService.m @@ -11,6 +11,7 @@ #import "NSString+XMLExtensions.h" static NSString * const DefaultDotcomAccountUUIDDefaultsKey = @"AccountDefaultDotcomUUID"; +static NSString * const DefaultDotcomAccountPasswordRemovedKey = @"DefaultDotcomAccountPasswordRemovedKey"; @interface AccountService () @@ -150,16 +151,14 @@ - (void)removeDefaultWordPressComAccount Uses a background managed object context. @param username the WordPress.com account's username - @param password the WordPress.com account's password - @param authToken the OAuth2 token returned by signIntoWordPressDotComWithUsername:password:success:failure: + @param authToken the OAuth2 token returned by signIntoWordPressDotComWithUsername:authToken: @return a WordPress.com `WPAccount` object for the given `username` - @see createOrUpdateWordPressComAccountWithUsername:password:authToken:context: + @see createOrUpdateWordPressComAccountWithUsername:password:authToken: */ - (WPAccount *)createOrUpdateWordPressComAccountWithUsername:(NSString *)username - password:(NSString *)password authToken:(NSString *)authToken { - WPAccount *account = [self createOrUpdateSelfHostedAccountWithXmlrpc:WordPressDotcomXMLRPCKey username:username andPassword:password]; + WPAccount *account = [self createOrUpdateSelfHostedAccountWithXmlrpc:WordPressDotcomXMLRPCKey username:username andPassword:nil]; account.authToken = authToken; account.isWpcom = YES; [[ContextManager sharedInstance] saveContext:self.managedObjectContext]; @@ -251,4 +250,19 @@ - (void)updateEmailAndDefaultBlogForWordPressComAccount:(WPAccount *)account }]; } +- (void)removeWordPressComAccountPasswordIfNeeded +{ + // Let's do this just once! + NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; + if ([defaults boolForKey:DefaultDotcomAccountPasswordRemovedKey]) { + return; + } + + WPAccount *account = [self defaultWordPressComAccount]; + account.password = nil; + + [defaults setBool:YES forKey:DefaultDotcomAccountPasswordRemovedKey]; + [defaults synchronize]; +} + @end diff --git a/WordPress/Classes/System/WordPressAppDelegate.m b/WordPress/Classes/System/WordPressAppDelegate.m index e3991b815b10..f9635bd4a659 100644 --- a/WordPress/Classes/System/WordPressAppDelegate.m +++ b/WordPress/Classes/System/WordPressAppDelegate.m @@ -20,7 +20,7 @@ #import "NSString+HTML.h" #import "PocketAPI.h" #import "ReaderPost.h" -#import "UIDevice+WordPressIdentifier.h" +#import "UIDevice+Helpers.h" #import "WordPressComApiCredentials.h" #import "WPAccount.h" #import "AccountService.h" @@ -127,6 +127,9 @@ - (BOOL)application:(UIApplication *)application willFinishLaunchingWithOptions: [self toggleExtraDebuggingIfNeeded]; [self removeCredentialsForDebug]; + // Stop Storing WordPress.com passwords + [self removeWordPressComPassword]; + // Stats and feedback [SupportViewController checkIfFeedbackShouldBeEnabled]; @@ -836,23 +839,21 @@ - (void)cleanUnusedMediaFileFromTmpDir }]; } -#pragma mark - Networking setup, User agents + + +#pragma mark - User agents - (void)setupUserAgent { // Keep a copy of the original userAgent for use with certain webviews in the app. - UIWebView *webView = [[UIWebView alloc] init]; - NSString *defaultUA = [webView stringByEvaluatingJavaScriptFromString:@"navigator.userAgent"]; - - NSString *appVersion = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"]; - [[NSUserDefaults standardUserDefaults] setObject:appVersion forKey:@"version_preference"]; - NSString *appUA = [NSString stringWithFormat:@"wp-iphone/%@ (%@ %@, %@) Mobile", - appVersion, - [[UIDevice currentDevice] systemName], - [[UIDevice currentDevice] systemVersion], - [[UIDevice currentDevice] model] - ]; - NSDictionary *dictionary = [[NSDictionary alloc] initWithObjectsAndKeys: appUA, @"UserAgent", defaultUA, @"DefaultUserAgent", appUA, @"AppUserAgent", nil]; + NSString *defaultUA = [[[UIWebView alloc] init] stringByEvaluatingJavaScriptFromString:@"navigator.userAgent"]; + NSString *wordPressUserAgent = [[UIDevice currentDevice] wordPressUserAgent]; + + NSDictionary *dictionary = @{ + @"UserAgent" : wordPressUserAgent, + @"DefaultUserAgent" : defaultUA, + @"AppUserAgent" : wordPressUserAgent + }; [[NSUserDefaults standardUserDefaults] registerDefaults:dictionary]; } @@ -871,7 +872,7 @@ - (void)useAppUserAgent NSDictionary *dictionary = [[NSDictionary alloc] initWithObjectsAndKeys:ua, @"UserAgent", nil]; // We have to call registerDefaults else the change isn't picked up by UIWebViews. [[NSUserDefaults standardUserDefaults] registerDefaults:dictionary]; - + DDLogVerbose(@"User-Agent set to: %@", ua); } @@ -880,16 +881,18 @@ - (NSString *)applicationUserAgent return [[NSUserDefaults standardUserDefaults] objectForKey:@"UserAgent"]; } + +#pragma mark - Networking setup + - (void)setupSingleSignOn { NSManagedObjectContext *context = [[ContextManager sharedInstance] mainContext]; AccountService *accountService = [[AccountService alloc] initWithManagedObjectContext:context]; WPAccount *defaultAccount = [accountService defaultWordPressComAccount]; - - if ([defaultAccount username]) { - [[WPComOAuthController sharedController] setWordPressComUsername:[defaultAccount username]]; - [[WPComOAuthController sharedController] setWordPressComPassword:[defaultAccount password]]; - } + WPComOAuthController *oAuthController = [WPComOAuthController sharedController]; + + [oAuthController setWordPressComUsername:defaultAccount.username]; + [oAuthController setWordPressComAuthToken:defaultAccount.authToken]; } - (void)setupReachability @@ -1060,6 +1063,17 @@ + (void)fixKeychainAccess DDLogVerbose(@"End keychain fixing"); } +#pragma mark - WordPress.com Accounts + +- (void)removeWordPressComPassword +{ + // Nuke WordPress.com stored passwords, since it's no longer required. + NSManagedObjectContext *mainContext = [[ContextManager sharedInstance] mainContext]; + AccountService *accountService = [[AccountService alloc] initWithManagedObjectContext:mainContext]; + [accountService removeWordPressComAccountPasswordIfNeeded]; +} + + #pragma mark - Debugging and logging - (void)printDebugLaunchInfoWithLaunchOptions:(NSDictionary *)launchOptions @@ -1082,9 +1096,9 @@ - (void)printDebugLaunchInfoWithLaunchOptions:(NSDictionary *)launchOptions #endif DDLogInfo(@"Extra debug: %@", extraDebug ? @"YES" : @"NO"); DDLogInfo(@"Device model: %@ (%@)", [UIDeviceHardware platformString], [UIDeviceHardware platform]); - DDLogInfo(@"OS: %@ %@", [device systemName], [device systemVersion]); + DDLogInfo(@"OS: %@ %@", device.systemName, device.systemVersion); DDLogInfo(@"Language: %@", currentLanguage); - DDLogInfo(@"UDID: %@", [device wordpressIdentifier]); + DDLogInfo(@"UDID: %@", device.wordPressIdentifier); DDLogInfo(@"APN token: %@", [NotificationsManager registeredPushNotificationsToken]); DDLogInfo(@"Launch options: %@", launchOptions); @@ -1228,8 +1242,6 @@ - (void)toggleExtraDebuggingIfNeeded - (void)handleDefaultAccountChangedNotification:(NSNotification *)notification { - [self toggleExtraDebuggingIfNeeded]; - // If the notification object is not nil, then it's a login if (notification.object) { [self loginSimperium]; @@ -1249,6 +1261,9 @@ - (void)handleDefaultAccountChangedNotification:(NSNotification *)notification [self showWelcomeScreenIfNeededAnimated:NO]; [self removeTodayWidgetConfiguration]; } + + [self toggleExtraDebuggingIfNeeded]; + [self setupSingleSignOn]; } #pragma mark - Today Extension diff --git a/WordPress/Classes/Utility/Analytics/WPAnalyticsTrackerWPCom.m b/WordPress/Classes/Utility/Analytics/WPAnalyticsTrackerWPCom.m index 7b525183244c..e25fc609b8ea 100644 --- a/WordPress/Classes/Utility/Analytics/WPAnalyticsTrackerWPCom.m +++ b/WordPress/Classes/Utility/Analytics/WPAnalyticsTrackerWPCom.m @@ -30,9 +30,10 @@ - (void)pingWPComStatsEndpoint:(NSString *)statName { int x = arc4random(); NSString *statsURL = [NSString stringWithFormat:@"%@%@%@%@%d" , WPMobileReaderURL, @"&template=stats&stats_name=", statName, @"&rnd=", x]; + NSString *userAgent = [[WordPressAppDelegate sharedWordPressApplicationDelegate] applicationUserAgent]; + NSMutableURLRequest* request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:statsURL]]; - WordPressAppDelegate *appDelegate = (WordPressAppDelegate *)[[UIApplication sharedApplication] delegate]; - [request setValue:[appDelegate applicationUserAgent] forHTTPHeaderField:@"User-Agent"]; + [request setValue:userAgent forHTTPHeaderField:@"User-Agent"]; NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request delegate:nil]; [conn start]; } diff --git a/WordPress/Classes/Utility/NotificationsManager.m b/WordPress/Classes/Utility/NotificationsManager.m index 7bf4284cc922..8a24b960f632 100644 --- a/WordPress/Classes/Utility/NotificationsManager.m +++ b/WordPress/Classes/Utility/NotificationsManager.m @@ -3,7 +3,6 @@ #import "WPTabBarController.h" #import "WordPressAppDelegate.h" -#import "UIDevice+WordPressIdentifier.h" #import "WordPressComApi.h" #import diff --git a/WordPress/Classes/Utility/WPURLRequest.h b/WordPress/Classes/Utility/WPURLRequest.h new file mode 100644 index 000000000000..f23f4a60e456 --- /dev/null +++ b/WordPress/Classes/Utility/WPURLRequest.h @@ -0,0 +1,35 @@ +#import + +@interface WPURLRequest : NSURLRequest + + +/** + Returns a NSURLRequest targetting the specified URL, with it's User-Agent field set up. + + @param url The endpoint URL + @param userAgent The User Agent that should be used. Can be nil. + @return a `NSURLRequest` instance for the specified URL, with a given UserAgent set. + */ + ++ (NSURLRequest *)requestWithURL:(NSURL *)url userAgent:(NSString *)userAgent; + + +/** + Returns a NSURLRequest to authenticate the user against a `wp-login.php` endpoint. + + @param loginUrl The authentication endpoint URL. + @param redirectURL The URL that should be redirected to, once authentication is complete. + @param username The user's username. + @param password The user's password. + @param bearerToken The OAuth token (if available). Can be nil. + @param userAgent The User Agent that should be used. Can be nil. + @return a `NSURLRequest` with a URL containing the authentication credentials, as provided. + */ ++ (NSURLRequest *)requestForAuthenticationWithURL:(NSURL *)loginUrl + redirectURL:(NSURL *)redirectURL + username:(NSString *)username + password:(NSString *)password + bearerToken:(NSString *)bearerToken + userAgent:(NSString *)userAgent; + +@end diff --git a/WordPress/Classes/Utility/WPURLRequest.m b/WordPress/Classes/Utility/WPURLRequest.m new file mode 100644 index 000000000000..d58d5b727236 --- /dev/null +++ b/WordPress/Classes/Utility/WPURLRequest.m @@ -0,0 +1,69 @@ +#import "WPURLRequest.h" +#import "NSString+Helpers.h" + + +@implementation WPURLRequest + ++ (NSURLRequest *)requestWithURL:(NSURL *)url userAgent:(NSString *)userAgent +{ + return [self mutableRequestWithURL:url userAgent:userAgent]; +} + ++ (NSURLRequest *)requestForAuthenticationWithURL:(NSURL *)loginUrl + redirectURL:(NSURL *)redirectURL + username:(NSString *)username + password:(NSString *)password + bearerToken:(NSString *)bearerToken + userAgent:(NSString *)userAgent +{ + NSParameterAssert(loginUrl); + NSParameterAssert(redirectURL); + NSParameterAssert(username); + NSParameterAssert(password != nil || bearerToken != nil); + + NSMutableURLRequest *request = [self mutableRequestWithURL:loginUrl userAgent:userAgent]; + + // If we've got a token, let's make sure the password never gets sent + NSString *encodedPassword = bearerToken.length == 0 ? [password stringByUrlEncoding] : nil; + encodedPassword = encodedPassword ?: [NSString string]; + + // Method! + [request setHTTPMethod:@"POST"]; + + // Auth Body + NSString *requestBody = [NSString stringWithFormat:@"%@=%@&%@=%@&%@=%@", + @"log", [username stringByUrlEncoding], + @"pwd", encodedPassword, + @"redirect_to", [redirectURL.absoluteString stringByUrlEncoding]]; + + request.HTTPBody = [requestBody dataUsingEncoding:NSUTF8StringEncoding]; + + // Auth Headers + [request setValue:[NSString stringWithFormat:@"%d", requestBody.length] forHTTPHeaderField:@"Content-Length"]; + [request addValue:@"*/*" forHTTPHeaderField:@"Accept"]; + + // Bearer Token + if (bearerToken) { + [request setValue:[NSString stringWithFormat:@"Bearer %@", bearerToken] forHTTPHeaderField:@"Authorization"]; + } + + return request; +} + +#pragma mark - Private Methods + ++ (NSMutableURLRequest *)mutableRequestWithURL:(NSURL *)url userAgent:(NSString *)userAgent +{ + NSParameterAssert(url); + + NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; + request.cachePolicy = NSURLRequestReturnCacheDataElseLoad; + + if (userAgent) { + [request setValue:userAgent forHTTPHeaderField:@"User-Agent"]; + } + + return request; +} + +@end diff --git a/WordPress/Classes/Utility/WPWebViewController.h b/WordPress/Classes/Utility/WPWebViewController.h index 9eca15cb497e..b565bf496d71 100644 --- a/WordPress/Classes/Utility/WPWebViewController.h +++ b/WordPress/Classes/Utility/WPWebViewController.h @@ -1,12 +1,9 @@ -#import +#import -@interface WPWebViewController : UIViewController +@interface WPWebViewController : UIViewController -@property (nonatomic, strong) NSURL *url; -@property (nonatomic, strong) NSURL *wpLoginURL; -@property (nonatomic, strong) NSString *username; -@property (nonatomic, strong) NSString *password; -@property (nonatomic, weak) IBOutlet UIWebView *webView; +// Interface +@property (nonatomic, weak) IBOutlet UIWebView *webView; @property (nonatomic, strong) IBOutlet UIToolbar *toolbar; @property (nonatomic, strong) IBOutlet UIView *loadingView; @property (nonatomic, strong) IBOutlet UIActivityIndicatorView *activityIndicator; @@ -17,19 +14,28 @@ @property (nonatomic, strong) IBOutlet UIBarButtonItem *refreshButton; @property (nonatomic, strong) IBOutlet UIBarButtonItem *optionsButton; @property (nonatomic, strong) UIBarButtonItem *spinnerButton; -@property (strong, nonatomic) NSTimer *statusTimer; -@property (nonatomic) BOOL hidesLinkOptions; +@property (nonatomic, strong) NSTimer *statusTimer; +@property (nonatomic, assign) BOOL hidesLinkOptions; + +// Endpoint! +@property (nonatomic, strong) NSURL *url; + +// Authentication +@property (nonatomic, strong) NSURL *wpLoginURL; +@property (nonatomic, strong) NSString *username; +@property (nonatomic, strong) NSString *password; +@property (nonatomic, strong) NSString *authToken; + +// Reader variables +@property (nonatomic, strong) NSString *detailContent; +@property (nonatomic, strong) NSString *detailHTML; +@property (nonatomic, strong) NSString *readerAllItems; +@property (nonatomic, assign) BOOL shouldScrollToBottom; -//reader variables -@property (nonatomic,strong) NSString *detailContent; -@property (nonatomic,strong) NSString *detailHTML; -@property (nonatomic,strong) NSString *readerAllItems; -@property (nonatomic) BOOL shouldScrollToBottom; +- (IBAction)showLinkOptions; +- (IBAction)dismiss; +- (IBAction)goForward; +- (IBAction)goBack; +- (IBAction)reload; -- (void) showCloseButton; -- (IBAction) showLinkOptions; -- (IBAction) dismiss; -- (IBAction) goForward; -- (IBAction) goBack; -- (IBAction) reload; @end diff --git a/WordPress/Classes/Utility/WPWebViewController.m b/WordPress/Classes/Utility/WPWebViewController.m index 55e76817d94c..99ff648d27d1 100644 --- a/WordPress/Classes/Utility/WPWebViewController.m +++ b/WordPress/Classes/Utility/WPWebViewController.m @@ -3,6 +3,8 @@ #import "ReachabilityUtils.h" #import "WPActivityDefaults.h" #import "NSString+Helpers.h" +#import "UIDevice+Helpers.h" +#import "WPURLRequest.h" #import "WPCookie.h" #import "Constants.h" #import "WPError.h" @@ -11,9 +13,11 @@ @interface WPWebViewController () -@property (weak, readonly) UIScrollView *scrollView; -@property (nonatomic) BOOL isLoading, needsLogin, hasLoadedContent; +@property (nonatomic, weak, readonly) UIScrollView *scrollView; @property (nonatomic, strong) UIPopoverController *popover; +@property (nonatomic, assign) BOOL isLoading; +@property (nonatomic, assign) BOOL needsLogin; +@property (nonatomic, assign) BOOL hasLoadedContent; @end @@ -22,7 +26,7 @@ @implementation WPWebViewController - (void)dealloc { _webView.delegate = nil; - if ([_webView isLoading]) { + if (_webView.isLoading) { [_webView stopLoading]; } _statusTimer = nil; @@ -35,7 +39,7 @@ - (BOOL)hidesBottomBarWhenPushed - (void)viewDidLoad { - DDLogInfo(@"%@ %@", self, NSStringFromSelector(_cmd)); + DDLogMethod(); [super viewDidLoad]; if (IS_IPHONE) { @@ -99,7 +103,7 @@ - (void)viewDidLoad - (void)viewWillAppear:(BOOL)animated { - DDLogInfo(@"%@ %@", self, NSStringFromSelector(_cmd)); + DDLogMethod() [super viewWillAppear:animated]; if ( self.detailContent == nil ) { @@ -122,7 +126,7 @@ - (void)viewWillAppear:(BOOL)animated - (void)viewWillDisappear:(BOOL)animated { - DDLogInfo(@"%@ %@", self, NSStringFromSelector(_cmd)); + DDLogMethod() [self setStatusTimer:nil]; [super viewWillDisappear:animated]; } @@ -235,7 +239,7 @@ - (void)loadURL:(NSURL *)webURL - (void)refreshWebView { - DDLogInfo(@"%@ %@", self, NSStringFromSelector(_cmd)); + DDLogMethod() if (![ReachabilityUtils isInternetReachable]) { __weak WPWebViewController *weakSelf = self; @@ -253,41 +257,10 @@ - (void)refreshWebView [self retryWithLogin]; return; } - - NSURL *webURL; - if (self.needsLogin) { - if (self.wpLoginURL != nil) { - webURL = self.wpLoginURL; - } else { //try to guess the login URL - webURL = [[NSURL alloc] initWithScheme:self.url.scheme host:self.url.host path:@"/wp-login.php"]; - } - } else { - webURL = self.url; - } - - WordPressAppDelegate *appDelegate = (WordPressAppDelegate *)[[UIApplication sharedApplication] delegate]; - NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:webURL]; - request.cachePolicy = NSURLRequestReturnCacheDataElseLoad; - [request setValue:[appDelegate applicationUserAgent] forHTTPHeaderField:@"User-Agent"]; - - [request setCachePolicy:NSURLRequestReturnCacheDataElseLoad]; - if (self.needsLogin) { - NSString *request_body = [NSString stringWithFormat:@"log=%@&pwd=%@&redirect_to=%@", - [self.username stringByUrlEncoding], - [self.password stringByUrlEncoding], - [[self.url absoluteString] stringByUrlEncoding]]; - - if ( self.wpLoginURL != nil ) { - [request setURL: self.wpLoginURL]; - } else { - [request setURL:[[NSURL alloc] initWithScheme:self.url.scheme host:self.url.host path:@"/wp-login.php"]]; - } - - [request setHTTPBody:[request_body dataUsingEncoding:NSUTF8StringEncoding]]; - [request setValue:[NSString stringWithFormat:@"%d", [request_body length]] forHTTPHeaderField:@"Content-Length"]; - [request addValue:@"*/*" forHTTPHeaderField:@"Accept"]; - [request setHTTPMethod:@"POST"]; - } + + NSURLRequest *request = [self newRequestForWebsite]; + NSAssert(request, @"We should have a valid request here!"); + [self.webView loadRequest:request]; } @@ -299,7 +272,7 @@ - (void)retryWithLogin - (void)setUrl:(NSURL *)theURL { - DDLogInfo(@"%@ %@", self, NSStringFromSelector(_cmd)); + DDLogMethod() if (_url != theURL) { _url = theURL; if (_url && self.webView) { @@ -557,7 +530,7 @@ - (void)webViewDidStartLoad:(UIWebView *)aWebView - (void)webViewDidFinishLoad:(UIWebView *)aWebView { - DDLogInfo(@"%@ %@", self, NSStringFromSelector(_cmd)); + DDLogMethod() [self setLoading:NO]; CGSize webviewSize = self.view.frame.size; @@ -601,51 +574,24 @@ - (void)webViewDidFinishLoad:(UIWebView *)aWebView } } -#pragma mark - MFMailComposeViewControllerDelegate - -- (void)mailComposeController:(MFMailComposeViewController*)controller - didFinishWithResult:(MFMailComposeResult)result - error:(NSError*)error -{ - [self dismissViewControllerAnimated:YES completion:nil]; -} - -#pragma mark - custom methods -//Returns true if the ToAddress field was found any of the sub views and made first responder -//passing in @"MFComposeSubjectView" as the value for field makes the subject become first responder -//passing in @"MFComposeTextContentView" as the value for field makes the body become first responder -//passing in @"RecipientTextField" as the value for field makes the to address field become first responder -- (BOOL) setMFMailFieldAsFirstResponder:(UIView*)view mfMailField:(NSString*)field -{ - for (UIView *subview in view.subviews) { - - NSString *className = [NSString stringWithFormat:@"%@", [subview class]]; - if ([className isEqualToString:field]) { - //Found the sub view we need to set as first responder - [subview becomeFirstResponder]; - return YES; - } - - if ([subview.subviews count] > 0) { - if ([self setMFMailFieldAsFirstResponder:subview mfMailField:field]){ - //Field was found and made first responder in a subview - return YES; - } - } - } - //field not found in this view. - return NO; -} +#pragma mark - Requests Helpers -- (void)showCloseButton +- (NSURLRequest *)newRequestForWebsite { - if (IS_IPAD) { - if (self.navigationController.navigationBarHidden) { - UINavigationItem *topItem = self.iPadNavBar.topItem; - topItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Close", @"") style:[WPStyleGuide barButtonStyleForBordered] target:self action:@selector(dismiss)]; - } + NSString *userAgent = [[WordPressAppDelegate sharedWordPressApplicationDelegate] applicationUserAgent]; + if (!self.needsLogin) { + return [WPURLRequest requestWithURL:self.url userAgent:userAgent]; } + + NSURL *loginURL = self.wpLoginURL ?: [[NSURL alloc] initWithScheme:self.url.scheme host:self.url.host path:@"/wp-login.php"]; + + return [WPURLRequest requestForAuthenticationWithURL:loginURL + redirectURL:self.url + username:self.username + password:self.password + bearerToken:self.authToken + userAgent:userAgent]; } @end diff --git a/WordPress/Classes/ViewRelated/Blog/BlogDetailsViewController.m b/WordPress/Classes/ViewRelated/Blog/BlogDetailsViewController.m index 6853b4810b04..db34475d5c76 100644 --- a/WordPress/Classes/ViewRelated/Blog/BlogDetailsViewController.m +++ b/WordPress/Classes/ViewRelated/Blog/BlogDetailsViewController.m @@ -389,11 +389,12 @@ - (void)showViewSiteForBlog:(Blog *)blog // Do nothing } else { WPWebViewController *webViewController = [[WPWebViewController alloc] init]; - [webViewController setUrl:[NSURL URLWithString:blogURL]]; - if ([blog isPrivate]) { - [webViewController setUsername:blog.username]; - [webViewController setPassword:blog.password]; - [webViewController setWpLoginURL:[NSURL URLWithString:blog.loginUrl]]; + webViewController.url = [NSURL URLWithString:blogURL]; + if (blog.isPrivate) { + webViewController.authToken = blog.authToken; + webViewController.username = blog.username; + webViewController.password = blog.password; + webViewController.wpLoginURL = [NSURL URLWithString:blog.loginUrl]; } [self.navigationController pushViewController:webViewController animated:YES]; } diff --git a/WordPress/Classes/ViewRelated/Blog/EditSiteViewController.h b/WordPress/Classes/ViewRelated/Blog/EditSiteViewController.h index 9ae2512d782c..12ea6ee1d4f6 100644 --- a/WordPress/Classes/ViewRelated/Blog/EditSiteViewController.h +++ b/WordPress/Classes/ViewRelated/Blog/EditSiteViewController.h @@ -6,19 +6,12 @@ @class Blog; -@interface EditSiteViewController : UIViewController +@interface EditSiteViewController : UIViewController -@property (nonatomic, weak) IBOutlet UITableView *tableView; -@property (nonatomic, strong) UIBarButtonItem *saveButton; -@property (nonatomic, weak) id delegate; -@property (nonatomic, assign) BOOL isCancellable; -@property (nonatomic, strong) NSString *blogId; -@property (nonatomic, strong) NSArray *subsites; -@property (nonatomic, strong) Blog *blog; -@property (nonatomic, strong) NSString *password, *username, *url; -@property (nonatomic, assign) BOOL geolocationEnabled; -@property (nonatomic, assign) BOOL isSiteDotCom; +@property (nonatomic, weak, readwrite) id delegate; +@property (nonatomic, assign, readwrite) BOOL isCancellable; +@property (nonatomic, strong, readonly) Blog *blog; -- (id)initWithBlog:(Blog *)blog; +- (instancetype)initWithBlog:(Blog *)blog; @end diff --git a/WordPress/Classes/ViewRelated/Blog/EditSiteViewController.m b/WordPress/Classes/ViewRelated/Blog/EditSiteViewController.m index 4ab354e2acfa..80589606c72d 100644 --- a/WordPress/Classes/ViewRelated/Blog/EditSiteViewController.m +++ b/WordPress/Classes/ViewRelated/Blog/EditSiteViewController.m @@ -21,19 +21,37 @@ static NSString *const PushNotificationsCellIdentifier = @"PushNotificationsCellIdentifier"; static NSString *const JetpackConnectedCellIdentifier = @"JetpackConnectedCellIdentifier"; -@interface EditSiteViewController () +@interface EditSiteViewController () -@property (nonatomic, copy) NSString *startingPwd, *startingUser, *startingUrl; -@property (nonatomic, weak) UITextField *lastTextField, *usernameTextField, *passwordTextField, *urlTextField; +@property (nonatomic, weak) IBOutlet UITableView *tableView; +@property (nonatomic, strong) UIBarButtonItem *saveButton; @property (nonatomic, strong) UIActivityIndicatorView *savingIndicator; +@property (nonatomic, weak) UITextField *lastTextField; +@property (nonatomic, weak) UITextField *usernameTextField; +@property (nonatomic, weak) UITextField *passwordTextField; +@property (nonatomic, weak) UITextField *urlTextField; + @property (nonatomic, strong) NSMutableDictionary *notificationPreferences; -@property (nonatomic) BOOL isKeyboardVisible; + +@property (nonatomic, strong) Blog *blog; +@property (nonatomic, strong) NSString *authToken; +@property (nonatomic, strong) NSString *username; +@property (nonatomic, strong) NSString *password; +@property (nonatomic, strong) NSString *url; + +@property (nonatomic, copy) NSString *startingPwd; +@property (nonatomic, copy) NSString *startingUser; +@property (nonatomic, copy) NSString *startingUrl; + +@property (nonatomic, assign) BOOL geolocationEnabled; +@property (nonatomic, assign) BOOL isSiteDotCom; +@property (nonatomic, assign) BOOL isKeyboardVisible; @end @implementation EditSiteViewController -- (id)initWithBlog:(Blog *)blog +- (instancetype)initWithBlog:(Blog *)blog { self = [super init]; if (self) { @@ -49,7 +67,7 @@ - (void)dealloc - (void)viewDidLoad { - DDLogInfo(@"%@ %@", self, NSStringFromSelector(_cmd)); + DDLogMethod(); [super viewDidLoad]; if (self.blog) { @@ -58,6 +76,7 @@ - (void)viewDidLoad [WPStyleGuide configureColorsForView:self.view andTableView:self.tableView]; self.url = self.blog.url; + self.authToken = self.blog.authToken; self.username = self.blog.username; self.password = self.blog.password; @@ -467,7 +486,6 @@ - (void)validateXmlprcURL:(NSURL *)xmlRpcURL [api getBlogOptionsWithSuccess:^(id options){ if ([options objectForKey:@"wordpress.com"] != nil) { self.isSiteDotCom = YES; - self.blogId = [options stringForKeyPath:@"blog_id.value"]; [self loginForSiteWithXmlRpcUrl:[NSURL URLWithString:@"https://wordpress.com/xmlrpc.php"]]; } else { self.isSiteDotCom = NO; @@ -484,7 +502,6 @@ - (void)loginForSiteWithXmlRpcUrl:(NSURL *)xmlRpcURL WordPressXMLRPCApi *api = [WordPressXMLRPCApi apiWithXMLRPCEndpoint:xmlRpcURL username:self.usernameTextField.text password:self.passwordTextField.text]; [api getBlogsWithSuccess:^(NSArray *blogs) { [SVProgressHUD dismiss]; - self.subsites = blogs; [self validationSuccess:[xmlRpcURL absoluteString]]; } failure:^(NSError *error) { [SVProgressHUD dismiss]; @@ -546,12 +563,12 @@ - (void)validationDidFail:(NSError *)error if (error) { NSString *message; - if ([error code] == 403) { + if (error.code == 403) { message = NSLocalizedString(@"Please try entering your login details again.", @""); } else { message = [error localizedDescription]; } - if ([error.userInfo[AFNetworkingOperationFailingURLResponseErrorKey] statusCode] == 405) { + if (error.code == 405) { [WPError showAlertWithTitle:NSLocalizedString(@"Sorry, can't log in", @"") message:message withSupportButton:YES okPressedBlock:^(UIAlertView *alertView) { [self openSiteAdminFromAlert:alertView]; }]; @@ -579,10 +596,11 @@ - (void)openSiteAdminFromAlert:(UIAlertView *)alertView } WPWebViewController *webViewController = [[WPWebViewController alloc] init]; - [webViewController setUrl:[NSURL URLWithString:path]]; - [webViewController setUsername:self.username]; - [webViewController setPassword:self.password]; - [webViewController setWpLoginURL:[NSURL URLWithString:self.blog.loginUrl]]; + webViewController.url = [NSURL URLWithString:path]; + webViewController.authToken = self.authToken; + webViewController.username = self.username; + webViewController.password = self.password; + webViewController.wpLoginURL = [NSURL URLWithString:self.blog.loginUrl]; webViewController.shouldScrollToBottom = YES; [self.navigationController pushViewController:webViewController animated:YES]; } @@ -704,7 +722,6 @@ - (BOOL)canEditUsernameAndURL - (void)handleKeyboardDidShow:(NSNotification *)notification { - if (_isKeyboardVisible) { return; } diff --git a/WordPress/Classes/ViewRelated/Blog/WPcomLoginViewController.m b/WordPress/Classes/ViewRelated/Blog/WPcomLoginViewController.m index 3667da52133f..9ea2cfd1b6ab 100644 --- a/WordPress/Classes/ViewRelated/Blog/WPcomLoginViewController.m +++ b/WordPress/Classes/ViewRelated/Blog/WPcomLoginViewController.m @@ -333,7 +333,7 @@ - (void)signIn:(id)sender NSManagedObjectContext *context = [[ContextManager sharedInstance] mainContext]; AccountService *accountService = [[AccountService alloc] initWithManagedObjectContext:context]; - WPAccount *account = [accountService createOrUpdateWordPressComAccountWithUsername:username password:password authToken:authToken]; + WPAccount *account = [accountService createOrUpdateWordPressComAccountWithUsername:username authToken:authToken]; NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:1]; [loginController.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone]; diff --git a/WordPress/Classes/ViewRelated/NUX/CreateAccountAndBlogViewController.m b/WordPress/Classes/ViewRelated/NUX/CreateAccountAndBlogViewController.m index dd49d03dbca0..434d16692798 100644 --- a/WordPress/Classes/ViewRelated/NUX/CreateAccountAndBlogViewController.m +++ b/WordPress/Classes/ViewRelated/NUX/CreateAccountAndBlogViewController.m @@ -778,9 +778,7 @@ - (void)createUserAndSite NSManagedObjectContext *context = [[ContextManager sharedInstance] mainContext]; AccountService *accountService = [[AccountService alloc] initWithManagedObjectContext:context]; - _account = [accountService createOrUpdateWordPressComAccountWithUsername:_usernameField.text - password:_passwordField.text - authToken:authToken]; + _account = [accountService createOrUpdateWordPressComAccountWithUsername:_usernameField.text authToken:authToken]; _account.email = _emailField.text; if (![accountService defaultWordPressComAccount]) { [accountService setDefaultWordPressComAccount:_account]; diff --git a/WordPress/Classes/ViewRelated/NUX/LoginViewController.m b/WordPress/Classes/ViewRelated/NUX/LoginViewController.m index 4dd95014dc65..a73f6183ad84 100644 --- a/WordPress/Classes/ViewRelated/NUX/LoginViewController.m +++ b/WordPress/Classes/ViewRelated/NUX/LoginViewController.m @@ -979,22 +979,20 @@ - (void)signInForWPComForUsername:(NSString *)username andPassword:(NSString *)p success:^(NSString *authToken) { [self setAuthenticating:NO withStatusMessage:nil]; _userIsDotCom = YES; - [self createWordPressComAccountForUsername:username password:password authToken:authToken]; + [self createWordPressComAccountForUsername:username authToken:authToken]; } failure:^(NSError *error) { [self setAuthenticating:NO withStatusMessage:nil]; [self displayRemoteError:error]; }]; } -- (void)createWordPressComAccountForUsername:(NSString *)username - password:(NSString *)password - authToken:(NSString *)authToken +- (void)createWordPressComAccountForUsername:(NSString *)username authToken:(NSString *)authToken { [self setAuthenticating:YES withStatusMessage:NSLocalizedString(@"Getting account information", nil)]; NSManagedObjectContext *context = [[ContextManager sharedInstance] mainContext]; AccountService *accountService = [[AccountService alloc] initWithManagedObjectContext:context]; - WPAccount *account = [accountService createOrUpdateWordPressComAccountWithUsername:username password:password authToken:authToken]; + WPAccount *account = [accountService createOrUpdateWordPressComAccountWithUsername:username authToken:authToken]; BlogService *blogService = [[BlogService alloc] initWithManagedObjectContext:context]; [blogService syncBlogsForAccount:account diff --git a/WordPress/Classes/ViewRelated/Post/PostPreviewViewController.m b/WordPress/Classes/ViewRelated/Post/PostPreviewViewController.m index 99f5533cc736..9b3c2847d0d7 100644 --- a/WordPress/Classes/ViewRelated/Post/PostPreviewViewController.m +++ b/WordPress/Classes/ViewRelated/Post/PostPreviewViewController.m @@ -1,6 +1,6 @@ #import "PostPreviewViewController.h" #import "WordPressAppDelegate.h" -#import "NSString+Helpers.h" +#import "WPURLRequest.h" #import "Post.h" #import "Category.h" @@ -39,14 +39,15 @@ - (instancetype)initWithPost:(AbstractPost *)aPost shouldHideStatusBar:(BOOL)sho - (void)didReceiveMemoryWarning { - DDLogWarn(@"%@ %@", self, NSStringFromSelector(_cmd)); + DDLogMethod(); [super didReceiveMemoryWarning]; } - (void)viewDidLoad { + DDLogMethod(); + [super viewDidLoad]; - DDLogInfo(@"%@ %@", self, NSStringFromSelector(_cmd)); [self setupWebView]; [self setupLoadingView]; } @@ -56,8 +57,7 @@ - (void)viewWillAppear:(BOOL)animated [super viewWillAppear:animated]; [[WordPressAppDelegate sharedWordPressApplicationDelegate] useDefaultUserAgent]; if (self.shouldHideStatusBar && !IS_IPAD) { - [[UIApplication sharedApplication] setStatusBarHidden:YES - withAnimation:nil]; + [[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:nil]; } [self refreshWebView]; } @@ -215,21 +215,16 @@ - (void)showRealPreview [self showSimplePreview]; } else { if (needsLogin) { - NSString *wpLoginURL = [self.apost.blog loginUrl]; - NSURL *url = [NSURL URLWithString:wpLoginURL]; - NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:url]; - [req setHTTPMethod:@"POST"]; - [req addValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"]; - NSString *paramDataString = [NSString stringWithFormat:@"%@=%@&%@=%@&%@=%@", - @"log", [self.apost.blog.username stringByUrlEncoding], - @"pwd", [self.apost.blog.password stringByUrlEncoding], - @"redirect_to", [link stringByUrlEncoding]]; - - NSData *paramData = [paramDataString dataUsingEncoding:NSUTF8StringEncoding]; - [req setHTTPBody: paramData]; - [req setValue:[NSString stringWithFormat:@"%d", [paramData length]] forHTTPHeaderField:@"Content-Length"]; - [req addValue:@"*/*" forHTTPHeaderField:@"Accept"]; - [self.webView loadRequest:req]; + NSURL *loginURL = [NSURL URLWithString:self.apost.blog.loginUrl]; + NSURL *redirectURL = [NSURL URLWithString:link]; + + NSURLRequest *request = [WPURLRequest requestForAuthenticationWithURL:loginURL + redirectURL:redirectURL + username:self.apost.blog.username + password:self.apost.blog.password + bearerToken:self.apost.blog.authToken + userAgent:nil]; + [self.webView loadRequest:request]; DDLogInfo(@"Showing real preview (login) for %@", link); } else { [self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:link]]]; diff --git a/WordPress/Classes/ViewRelated/Reader/ReaderCommentsViewController.m b/WordPress/Classes/ViewRelated/Reader/ReaderCommentsViewController.m index 3a811a787786..a0e288f21bca 100644 --- a/WordPress/Classes/ViewRelated/Reader/ReaderCommentsViewController.m +++ b/WordPress/Classes/ViewRelated/Reader/ReaderCommentsViewController.m @@ -1102,7 +1102,7 @@ - (void)commentView:(CommentContentView *)commentView updatedAttachmentViewsForP - (void)commentCell:(UITableViewCell *)cell linkTapped:(NSURL *)url { WPWebViewController *controller = [[WPWebViewController alloc] init]; - [controller setUrl:url]; + controller.url = url; [self.navigationController pushViewController:controller animated:YES]; } @@ -1143,7 +1143,7 @@ - (void)richTextView:(WPRichTextView *)richTextView didReceiveLinkAction:(NSURL } WPWebViewController *controller = [[WPWebViewController alloc] init]; - [controller setUrl:linkURL]; + controller.url = linkURL; [self.navigationController pushViewController:controller animated:YES]; } diff --git a/WordPress/Classes/ViewRelated/Reader/WPWebAppViewController.m b/WordPress/Classes/ViewRelated/Reader/WPWebAppViewController.m index e39e020a9ac5..66ec389a6337 100644 --- a/WordPress/Classes/ViewRelated/Reader/WPWebAppViewController.m +++ b/WordPress/Classes/ViewRelated/Reader/WPWebAppViewController.m @@ -96,10 +96,10 @@ - (void)loadURL:(NSString *)url } NSHTTPCookieStorage *cookies = [NSHTTPCookieStorage sharedHTTPCookieStorage]; - WordPressAppDelegate *appDelegate = (WordPressAppDelegate *)[[UIApplication sharedApplication] delegate]; + NSString *userAgent = [[WordPressAppDelegate sharedWordPressApplicationDelegate] applicationUserAgent]; NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:url]]; NSDictionary *cookieHeader = [NSHTTPCookie requestHeaderFieldsWithCookies:[cookies cookiesForURL:request.URL]]; - [request setValue:appDelegate.applicationUserAgent forHTTPHeaderField:@"User-Agent"]; + [request setValue:userAgent forHTTPHeaderField:@"User-Agent"]; [request setValue:[cookieHeader valueForKey:@"Cookie"] forHTTPHeaderField:@"Cookie"]; [self.webView loadRequest:[self.webBridge authorizeHybridRequest:request]]; } diff --git a/WordPress/Classes/ViewRelated/Reader/WPWebBridge.m b/WordPress/Classes/ViewRelated/Reader/WPWebBridge.m index f5826d8756b6..5eefe0841503 100644 --- a/WordPress/Classes/ViewRelated/Reader/WPWebBridge.m +++ b/WordPress/Classes/ViewRelated/Reader/WPWebBridge.m @@ -1,6 +1,6 @@ #import "WPWebBridge.h" #import -#import "UIDevice+WordPressIdentifier.h" +#import "UIDevice+Helpers.h" static NSString *const HybridTokenSetting = @"WPWebAppHybridAuthToken"; static NSString *const AuthorizedHybridHost = @"en.wordpress.com"; @@ -66,7 +66,7 @@ + (NSString *)hybridAuthToken NSString *token = [defaults stringForKey:HybridTokenSetting]; if (token == nil) { - NSString *concat = [NSString stringWithFormat:@"%@--%d", [[UIDevice currentDevice] wordpressIdentifier], arc4random()]; + NSString *concat = [NSString stringWithFormat:@"%@--%d", [[UIDevice currentDevice] wordPressIdentifier], arc4random()]; const char *concat_str = [concat UTF8String]; unsigned char result[CC_MD5_DIGEST_LENGTH]; CC_MD5(concat_str, (CC_LONG)strlen(concat_str), result); diff --git a/WordPress/Classes/ViewRelated/Stats/JetpackSettingsViewController.m b/WordPress/Classes/ViewRelated/Stats/JetpackSettingsViewController.m index b36e320b2a99..a9dfe723a070 100644 --- a/WordPress/Classes/ViewRelated/Stats/JetpackSettingsViewController.m +++ b/WordPress/Classes/ViewRelated/Stats/JetpackSettingsViewController.m @@ -13,14 +13,15 @@ #import "BlogService.h" #import "ContextManager.h" -CGFloat const JetpackiOS7StatusBarOffset = 20.0; -CGFloat const JetpackStandardOffset = 16; -CGFloat const JetpackTextFieldWidth = 320.0; -CGFloat const JetpackMaxTextWidth = 289.0; -CGFloat const JetpackTextFieldHeight = 44.0; -CGFloat const JetpackIconVerticalOffset = 77; -CGFloat const JetpackSignInButtonWidth = 289.0; -CGFloat const JetpackSignInButtonHeight = 41.0; +static NSString * const JetpackMoreInformationURL = @"http://ios.wordpress.org/faq/#faq_15"; +static CGFloat const JetpackiOS7StatusBarOffset = 20.0; +static CGFloat const JetpackStandardOffset = 16.0; +static CGFloat const JetpackTextFieldWidth = 320.0; +static CGFloat const JetpackMaxTextWidth = 289.0; +static CGFloat const JetpackTextFieldHeight = 44.0; +static CGFloat const JetpackIconVerticalOffset = 77.0; +static CGFloat const JetpackSignInButtonWidth = 289.0; +static CGFloat const JetpackSignInButtonHeight = 41.0; @interface JetpackSettingsViewController () @end @@ -446,24 +447,24 @@ - (void)dismissKeyboard - (void)openInstallJetpackURL { [WPAnalytics track:WPAnalyticsStatSelectedInstallJetpack]; - [self openURL:[NSURL URLWithString:[_blog adminUrlWithPath:@"plugin-install.php?tab=plugin-information&plugin=jetpack"]] withUsername:_blog.username password:_blog.password wpLoginURL:[NSURL URLWithString:_blog.loginUrl]]; + NSURL *targetURL = [NSURL URLWithString:[_blog adminUrlWithPath:@"plugin-install.php?tab=plugin-information&plugin=jetpack"]]; + NSURL *loginURL = [NSURL URLWithString:_blog.loginUrl]; + [self openURL:targetURL username:_blog.username password:_blog.password wpLoginURL:loginURL]; } - (void)openMoreInformationURL { - [self openURL:[NSURL URLWithString:@"http://ios.wordpress.org/faq/#faq_15"] withUsername:nil password:nil wpLoginURL:nil]; + NSURL *targetURL = [NSURL URLWithString:JetpackMoreInformationURL]; + [self openURL:targetURL username:nil password:nil wpLoginURL:nil]; } -- (void)openURL:(NSURL *)url withUsername:(NSString *)username password:(NSString *)password wpLoginURL:(NSURL *)wpLoginURL +- (void)openURL:(NSURL *)url username:(NSString *)username password:(NSString *)password wpLoginURL:(NSURL *)wpLoginURL { WPWebViewController *webViewController = [[WPWebViewController alloc] init]; - [webViewController setUrl:url]; - if (username && password && wpLoginURL) { - - [webViewController setUsername:username]; - [webViewController setPassword:password]; - [webViewController setWpLoginURL:wpLoginURL]; - } + webViewController.url = url; + webViewController.username = username; + webViewController.password = password; + webViewController.wpLoginURL = wpLoginURL; if (self.navigationController) { [self.navigationController pushViewController:webViewController animated:YES]; diff --git a/WordPress/Classes/ViewRelated/Stats/StatsWebViewController.m b/WordPress/Classes/ViewRelated/Stats/StatsWebViewController.m index 3228b450552f..476ff0fc3b49 100644 --- a/WordPress/Classes/ViewRelated/Stats/StatsWebViewController.m +++ b/WordPress/Classes/ViewRelated/Stats/StatsWebViewController.m @@ -6,10 +6,11 @@ #import "JetpackSettingsViewController.h" #import "EditSiteViewController.h" #import "ReachabilityUtils.h" -#import "NSString+Helpers.h" +#import "WPURLRequest.h" #import "ContextManager.h" #import "AccountService.h" #import "WPCookie.h" +#import "UIDevice+Helpers.h" NSString * const WPStatsWebBlogKey = @"WPStatsWebBlogKey"; @@ -262,24 +263,15 @@ - (void)showRetryAlertView:(StatsWebViewController *)statsWebViewController - (void)authStats { - DDLogInfo(@"%@ %@", self, NSStringFromSelector(_cmd)); + DDLogMethod(); if (authed) { [self loadStats]; return; } - NSString *username = @""; - NSString *password = @""; - if ([blog isWPcom]) { - //use set username/pw for wpcom blogs - username = blog.username; - password = blog.password; - - } else { - username = blog.jetpackUsername; - password = blog.jetpackPassword; - } - + NSString *username = blog.isWPcom ? blog.username : blog.jetpackUsername; + NSString *password = blog.isWPcom ? blog.password : blog.jetpackPassword; + // Skip the auth call to reduce loadtime if its the same username as before. if ([WPCookie hasCookieForURL:[NSURL URLWithString:@"https://wordpress.com/"] andUsername:username]) { authed = YES; @@ -287,23 +279,21 @@ - (void)authStats return; } - NSMutableURLRequest *mRequest = [[NSMutableURLRequest alloc] init]; - NSString *requestBody = [NSString stringWithFormat:@"log=%@&pwd=%@&redirect_to=https://wordpress.com", - [username stringByUrlEncoding], - [password stringByUrlEncoding]]; - - [mRequest setURL:[NSURL URLWithString:@"https://wordpress.com/wp-login.php"]]; - [mRequest setHTTPBody:[requestBody dataUsingEncoding:NSUTF8StringEncoding]]; - [mRequest setValue:[NSString stringWithFormat:@"%d", [requestBody length]] forHTTPHeaderField:@"Content-Length"]; - [mRequest addValue:@"*/*" forHTTPHeaderField:@"Accept"]; - NSString *userAgent = [NSString stringWithFormat:@"%@", [webView stringByEvaluatingJavaScriptFromString:@"navigator.userAgent"]]; - [mRequest addValue:userAgent forHTTPHeaderField:@"User-Agent"]; - [mRequest setHTTPMethod:@"POST"]; + NSURL *loginURL = [NSURL URLWithString:@"https://wordpress.com/wp-login.php"]; + NSURL *redirectURL = [NSURL URLWithString:@"https://wordpress.com"]; + + NSURLRequest *request = [WPURLRequest requestForAuthenticationWithURL:loginURL + redirectURL:redirectURL + username:username + password:password + bearerToken:blog.authToken + userAgent:nil]; + // Clear cookies prior to auth so we don't get a false positive on the login cookie being correctly set in some cases. [self clearCookies]; - self.authRequest = [[AFHTTPRequestOperation alloc] initWithRequest:mRequest]; + self.authRequest = [[AFHTTPRequestOperation alloc] initWithRequest:request]; __weak StatsWebViewController *statsWebViewController = self; [authRequest setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) { @@ -369,8 +359,6 @@ - (void)loadStats NSMutableURLRequest *mRequest = [[NSMutableURLRequest alloc] init]; [mRequest setURL:[NSURL URLWithString:pathStr]]; [mRequest addValue:@"*/*" forHTTPHeaderField:@"Accept"]; - NSString *userAgent = [NSString stringWithFormat:@"%@",[webView stringByEvaluatingJavaScriptFromString:@"navigator.userAgent"]]; - [mRequest addValue:userAgent forHTTPHeaderField:@"User-Agent"]; [webView loadRequest:mRequest]; } @@ -424,7 +412,7 @@ - (BOOL)wpWebView:(WPWebView *)wpWebView if ([host rangeOfString:@"wordpress.com"].location == NSNotFound || [query rangeOfString:@"no-chrome"].location == NSNotFound) { WPWebViewController *webViewController = [[WPWebViewController alloc] init]; - [webViewController setUrl:request.URL]; + webViewController.url = request.URL; [self.navigationController pushViewController:webViewController animated:YES]; return NO; } diff --git a/WordPress/Classes/ViewRelated/System/WPTableViewController.m b/WordPress/Classes/ViewRelated/System/WPTableViewController.m index f9ebcedab481..73fd61aacfc6 100644 --- a/WordPress/Classes/ViewRelated/System/WPTableViewController.m +++ b/WordPress/Classes/ViewRelated/System/WPTableViewController.m @@ -477,11 +477,13 @@ - (void)sendUserToXMLOptionsFromAlert:(UIAlertView *)alertView WPWebViewController *webViewController = [[WPWebViewController alloc] init]; webViewController.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Cancel", nil) style:UIBarButtonItemStylePlain target:self action:@selector(dismissModal:)]; - [webViewController setUrl:[NSURL URLWithString:path]]; - [webViewController setUsername:self.blog.username]; - [webViewController setPassword:self.blog.password]; - [webViewController setWpLoginURL:[NSURL URLWithString:self.blog.loginUrl]]; + webViewController.url = [NSURL URLWithString:path]; + webViewController.authToken = self.blog.authToken; + webViewController.username = self.blog.username; + webViewController.password = self.blog.password; + webViewController.wpLoginURL = [NSURL URLWithString:self.blog.loginUrl]; webViewController.shouldScrollToBottom = YES; + // Probably should be modal. UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:webViewController]; navController.navigationBar.translucent = NO; diff --git a/WordPress/Classes/ViewRelated/Themes/ThemeDetailsViewController.m b/WordPress/Classes/ViewRelated/Themes/ThemeDetailsViewController.m index 478f4f0a420a..53d226aac4e5 100644 --- a/WordPress/Classes/ViewRelated/Themes/ThemeDetailsViewController.m +++ b/WordPress/Classes/ViewRelated/Themes/ThemeDetailsViewController.m @@ -228,10 +228,11 @@ - (IBAction)livePreviewPressed:(id)sender WPAccount *defaultAccount = [accountService defaultWordPressComAccount]; WPWebViewController *livePreviewController = [[WPWebViewController alloc] init]; + livePreviewController.url = [NSURL URLWithString:self.theme.previewUrl]; + livePreviewController.authToken = defaultAccount.authToken; livePreviewController.username = defaultAccount.username; livePreviewController.password = defaultAccount.password; - [livePreviewController setWpLoginURL:[NSURL URLWithString:self.theme.blog.loginUrl]]; - livePreviewController.url = [NSURL URLWithString:self.theme.previewUrl]; + livePreviewController.wpLoginURL = [NSURL URLWithString:self.theme.blog.loginUrl]; [self.navigationController pushViewController:livePreviewController animated:YES]; } diff --git a/WordPress/Classes/ViewRelated/Views/WPWebView.m b/WordPress/Classes/ViewRelated/Views/WPWebView.m index 78f6a3683ffb..b5ee4134d40c 100644 --- a/WordPress/Classes/ViewRelated/Views/WPWebView.m +++ b/WordPress/Classes/ViewRelated/Views/WPWebView.m @@ -4,6 +4,7 @@ #import "AFHTTPRequestOperation.h" #import "WordPressAppDelegate.h" #import "UIColor+Helpers.h" +#import "UIDevice+Helpers.h" NSString *refreshedWithOutValidRequestNotification = @"refreshedWithOutValidRequestNotification"; @@ -122,11 +123,7 @@ - (void)setupHeaders [self setDefaultHeader:@"Accept-Language" value:[NSString stringWithFormat:@"%@, en-us;q=0.8", preferredLanguageCodes]]; // User-Agent Header; see http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.43 - WordPressAppDelegate *appDelegate = (WordPressAppDelegate *)[[UIApplication sharedApplication] delegate]; - NSString *userAgent = [appDelegate applicationUserAgent]; - if (!userAgent){ - userAgent = [NSString stringWithFormat:@"%@",[webView stringByEvaluatingJavaScriptFromString:@"navigator.userAgent"]]; - } + NSString *userAgent = [[WordPressAppDelegate sharedWordPressApplicationDelegate] applicationUserAgent]; [self setDefaultHeader:@"User-Agent" value:userAgent]; } diff --git a/WordPress/WordPress.xcodeproj/project.pbxproj b/WordPress/WordPress.xcodeproj/project.pbxproj index 46ef1c37df43..00527d9be215 100644 --- a/WordPress/WordPress.xcodeproj/project.pbxproj +++ b/WordPress/WordPress.xcodeproj/project.pbxproj @@ -330,6 +330,7 @@ B5D7F2DC1A04180A006D3047 /* NSAttributedString+RichTextView.swift in Sources */ = {isa = PBXBuildFile; fileRef = B5D7F2D91A04180A006D3047 /* NSAttributedString+RichTextView.swift */; }; B5D7F2DD1A04180A006D3047 /* RichTextView.swift in Sources */ = {isa = PBXBuildFile; fileRef = B5D7F2DA1A04180A006D3047 /* RichTextView.swift */; }; B5D7F2DE1A04180A006D3047 /* UITextView+RichTextView.swift in Sources */ = {isa = PBXBuildFile; fileRef = B5D7F2DB1A04180A006D3047 /* UITextView+RichTextView.swift */; }; + B5E06E311A9CD31D00128985 /* WPURLRequest.m in Sources */ = {isa = PBXBuildFile; fileRef = B5E06E301A9CD31D00128985 /* WPURLRequest.m */; }; B5E167F419C08D18009535AA /* NSCalendar+Helpers.swift in Sources */ = {isa = PBXBuildFile; fileRef = B5E167F319C08D18009535AA /* NSCalendar+Helpers.swift */; }; B5E23BDF19AD0D00000D6879 /* NoteTableViewCell.xib in Resources */ = {isa = PBXBuildFile; fileRef = B5E23BDE19AD0D00000D6879 /* NoteTableViewCell.xib */; }; B5F015CB195DFD7600F6ECF2 /* WordPressActivity.m in Sources */ = {isa = PBXBuildFile; fileRef = B5F015CA195DFD7600F6ECF2 /* WordPressActivity.m */; }; @@ -406,7 +407,7 @@ E19DF741141F7BDD000002F3 /* libz.dylib in Frameworks */ = {isa = PBXBuildFile; fileRef = E19DF740141F7BDD000002F3 /* libz.dylib */; }; E1A03EE217422DCF0085D192 /* BlogToAccount.m in Sources */ = {isa = PBXBuildFile; fileRef = E1A03EE117422DCE0085D192 /* BlogToAccount.m */; }; E1A03F48174283E10085D192 /* BlogToJetpackAccount.m in Sources */ = {isa = PBXBuildFile; fileRef = E1A03F47174283E00085D192 /* BlogToJetpackAccount.m */; }; - E1A0FAE7162F11CF0063B098 /* UIDevice+WordPressIdentifier.m in Sources */ = {isa = PBXBuildFile; fileRef = E1A0FAE6162F11CE0063B098 /* UIDevice+WordPressIdentifier.m */; }; + E1A0FAE7162F11CF0063B098 /* UIDevice+Helpers.m in Sources */ = {isa = PBXBuildFile; fileRef = E1A0FAE6162F11CE0063B098 /* UIDevice+Helpers.m */; }; E1A386C814DB05C300954CF8 /* AVFoundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = E1A386C714DB05C300954CF8 /* AVFoundation.framework */; }; E1A386CA14DB05F700954CF8 /* CoreMedia.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = E1A386C914DB05F700954CF8 /* CoreMedia.framework */; }; E1A386CB14DB063800954CF8 /* MediaPlayer.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 83FB4D3E122C38F700DB9506 /* MediaPlayer.framework */; }; @@ -1057,6 +1058,8 @@ B55853F819630E7900FAF6C3 /* Notification.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; lineEnding = 0; path = Notification.h; sourceTree = ""; xcLanguageSpecificationIdentifier = xcode.lang.objcpp; }; B55853F919630E7900FAF6C3 /* Notification.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; lineEnding = 0; path = Notification.m; sourceTree = ""; xcLanguageSpecificationIdentifier = xcode.lang.objc; }; B558541019631A1000FAF6C3 /* Notifications.storyboard */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.storyboard; path = Notifications.storyboard; sourceTree = ""; }; + B57321601A97657500852584 /* NSURLRequest+Helpers.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "NSURLRequest+Helpers.h"; sourceTree = ""; }; + B57321611A97657500852584 /* NSURLRequest+Helpers.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "NSURLRequest+Helpers.m"; sourceTree = ""; }; B57B99D419A2C20200506504 /* NoteTableHeaderView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = NoteTableHeaderView.swift; sourceTree = ""; }; B57B99DC19A2DBF200506504 /* NSObject+Helpers.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "NSObject+Helpers.h"; sourceTree = ""; }; B57B99DD19A2DBF200506504 /* NSObject+Helpers.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "NSObject+Helpers.m"; sourceTree = ""; }; @@ -1089,6 +1092,8 @@ B5D7F2D91A04180A006D3047 /* NSAttributedString+RichTextView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "NSAttributedString+RichTextView.swift"; sourceTree = ""; }; B5D7F2DA1A04180A006D3047 /* RichTextView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = RichTextView.swift; sourceTree = ""; }; B5D7F2DB1A04180A006D3047 /* UITextView+RichTextView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "UITextView+RichTextView.swift"; sourceTree = ""; }; + B5E06E2F1A9CD31D00128985 /* WPURLRequest.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = WPURLRequest.h; sourceTree = ""; }; + B5E06E301A9CD31D00128985 /* WPURLRequest.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = WPURLRequest.m; sourceTree = ""; }; B5E167F319C08D18009535AA /* NSCalendar+Helpers.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "NSCalendar+Helpers.swift"; sourceTree = ""; }; B5E23BDE19AD0D00000D6879 /* NoteTableViewCell.xib */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.xib; path = NoteTableViewCell.xib; sourceTree = ""; }; B5F015C9195DFD7600F6ECF2 /* WordPressActivity.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = WordPressActivity.h; sourceTree = ""; }; @@ -1229,8 +1234,8 @@ E1A03EE117422DCE0085D192 /* BlogToAccount.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = BlogToAccount.m; sourceTree = ""; }; E1A03F46174283DF0085D192 /* BlogToJetpackAccount.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = BlogToJetpackAccount.h; sourceTree = ""; }; E1A03F47174283E00085D192 /* BlogToJetpackAccount.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = BlogToJetpackAccount.m; sourceTree = ""; }; - E1A0FAE5162F11CE0063B098 /* UIDevice+WordPressIdentifier.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "UIDevice+WordPressIdentifier.h"; sourceTree = ""; }; - E1A0FAE6162F11CE0063B098 /* UIDevice+WordPressIdentifier.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "UIDevice+WordPressIdentifier.m"; sourceTree = ""; }; + E1A0FAE5162F11CE0063B098 /* UIDevice+Helpers.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; lineEnding = 0; path = "UIDevice+Helpers.h"; sourceTree = ""; xcLanguageSpecificationIdentifier = xcode.lang.objcpp; }; + E1A0FAE6162F11CE0063B098 /* UIDevice+Helpers.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; lineEnding = 0; path = "UIDevice+Helpers.m"; sourceTree = ""; xcLanguageSpecificationIdentifier = xcode.lang.objc; }; E1A386C714DB05C300954CF8 /* AVFoundation.framework */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = wrapper.framework; name = AVFoundation.framework; path = System/Library/Frameworks/AVFoundation.framework; sourceTree = SDKROOT; }; E1A386C914DB05F700954CF8 /* CoreMedia.framework */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = wrapper.framework; name = CoreMedia.framework; path = System/Library/Frameworks/CoreMedia.framework; sourceTree = SDKROOT; }; E1A38C921581879D00439E55 /* WPTableViewControllerSubclass.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = WPTableViewControllerSubclass.h; sourceTree = ""; }; @@ -2165,6 +2170,8 @@ 5DA3EE11192508F700294E0B /* WPImageOptimizer+Private.m */, B5AB733B19901F85005F5044 /* WPNoResultsView+AnimatedBox.h */, B5AB733C19901F85005F5044 /* WPNoResultsView+AnimatedBox.m */, + B5E06E2F1A9CD31D00128985 /* WPURLRequest.h */, + B5E06E301A9CD31D00128985 /* WPURLRequest.m */, E1F5A1BA1771C90A00E0495F /* WPTableImageSource.h */, E1F5A1BB1771C90A00E0495F /* WPTableImageSource.m */, 8516972A169D42F4006C5DED /* WPToast.h */, @@ -2535,8 +2542,8 @@ B55853F21962337500FAF6C3 /* NSScanner+Helpers.m */, 296526FC105810E100597FA3 /* NSString+Helpers.h */, 296526FD105810E100597FA3 /* NSString+Helpers.m */, - E1A0FAE5162F11CE0063B098 /* UIDevice+WordPressIdentifier.h */, - E1A0FAE6162F11CE0063B098 /* UIDevice+WordPressIdentifier.m */, + E1A0FAE5162F11CE0063B098 /* UIDevice+Helpers.h */, + E1A0FAE6162F11CE0063B098 /* UIDevice+Helpers.m */, 834CAE9B122D56B1003DDF49 /* UIImage+Alpha.h */, 834CAE9D122D56B1003DDF49 /* UIImage+Alpha.m */, 834CAE7A122D528A003DDF49 /* UIImage+Resize.h */, @@ -2569,6 +2576,8 @@ 31EC15071A5B6675009FC8B3 /* WPStyleGuide+Suggestions.m */, 85CE4C1E1A703CF200780DFE /* NSBundle+VersionNumberHelper.h */, 85CE4C1F1A703CF200780DFE /* NSBundle+VersionNumberHelper.m */, + B57321601A97657500852584 /* NSURLRequest+Helpers.h */, + B57321611A97657500852584 /* NSURLRequest+Helpers.m */, FFAC890E1A96A85800CC06AC /* NSProcessInfo+Util.h */, FFAC890F1A96A85800CC06AC /* NSProcessInfo+Util.m */, ); @@ -3498,7 +3507,8 @@ 5D87E10C15F5120C0012C595 /* SettingsPageViewController.m in Sources */, 5DC3A44D1610B9BC00A890BE /* UINavigationController+Rotation.m in Sources */, B532D4E9199D4357006E4DF6 /* NoteBlockCommentTableViewCell.swift in Sources */, - E1A0FAE7162F11CF0063B098 /* UIDevice+WordPressIdentifier.m in Sources */, + B5E06E311A9CD31D00128985 /* WPURLRequest.m in Sources */, + E1A0FAE7162F11CF0063B098 /* UIDevice+Helpers.m in Sources */, 5D44EB351986D695008B7175 /* ReaderSiteServiceRemote.m in Sources */, 5DA5BF4318E32DCF005F11F9 /* MediaSearchFilterHeaderView.m in Sources */, 5DF94E441962BAA700359241 /* WPContentView.m in Sources */, diff --git a/WordPress/WordPressApi/WordPressComApi.h b/WordPress/WordPressApi/WordPressComApi.h index c4ecc7dece2c..5b82f8725429 100644 --- a/WordPress/WordPressApi/WordPressComApi.h +++ b/WordPress/WordPressApi/WordPressComApi.h @@ -34,7 +34,7 @@ extern NSString *const WordPressComApiPushAppId; Use this only for things that don't require an account, like signup or logged out reader */ + (WordPressComApi *)anonymousApi; -- (id)initWithOAuthToken:(NSString *)authToken; +- (instancetype)initWithOAuthToken:(NSString *)authToken; /** Reset the API instance diff --git a/WordPress/WordPressApi/WordPressComApi.m b/WordPress/WordPressApi/WordPressComApi.m index be7411920293..bd3b565ba9be 100644 --- a/WordPress/WordPressApi/WordPressComApi.m +++ b/WordPress/WordPressApi/WordPressComApi.m @@ -2,7 +2,7 @@ #import "WordPressComApiCredentials.h" #import "NSString+Helpers.h" #import -#import "UIDevice+WordPressIdentifier.h" +#import "UIDevice+Helpers.h" #import "WordPressAppDelegate.h" #import "NotificationsManager.h" @@ -58,7 +58,7 @@ @implementation WordPressComApi #pragma - Initializers -- (id)initWithOAuthToken:(NSString *)authToken +- (instancetype)initWithOAuthToken:(NSString *)authToken { NSParameterAssert([authToken isKindOfClass:[NSString class]]); @@ -66,14 +66,13 @@ - (id)initWithOAuthToken:(NSString *)authToken self = [super initWithBaseURL:url]; - if (self) - { + if (self) { _authToken = authToken; self.requestSerializer = [AFJSONRequestSerializer serializer]; [self setAuthorizationHeaderWithToken:_authToken]; - NSString* userAgent = [[WordPressAppDelegate sharedWordPressApplicationDelegate] applicationUserAgent]; + NSString *userAgent = [[WordPressAppDelegate sharedWordPressApplicationDelegate] applicationUserAgent]; [self.requestSerializer setValue:userAgent forHTTPHeaderField:@"User-Agent"]; } @@ -122,7 +121,7 @@ + (WordPressComApi *)anonymousApi { DDLogVerbose(@"Initializing anonymous API"); _anonymousApi = [[self alloc] initWithBaseURL:[NSURL URLWithString:WordPressComApiClientEndpointURL] ]; - NSString* userAgent = [[WordPressAppDelegate sharedWordPressApplicationDelegate] applicationUserAgent]; + NSString *userAgent = [[WordPressAppDelegate sharedWordPressApplicationDelegate] applicationUserAgent]; [_anonymousApi.requestSerializer setValue:userAgent forHTTPHeaderField:@"User-Agent"]; }); @@ -397,7 +396,7 @@ - (void)syncPushNotificationInfoWithDeviceToken:(NSString *)token @"device_model" : [UIDeviceHardware platform], @"os_version" : [[UIDevice currentDevice] systemVersion], @"app_version" : [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"], - @"device_uuid" : [[UIDevice currentDevice] wordpressIdentifier], + @"device_uuid" : [[UIDevice currentDevice] wordPressIdentifier], }; [self POST:@"devices/new" diff --git a/WordPress/WordPressTest/AccountServiceTests.swift b/WordPress/WordPressTest/AccountServiceTests.swift index 65771649606e..9e52ec2f3fcb 100644 --- a/WordPress/WordPressTest/AccountServiceTests.swift +++ b/WordPress/WordPressTest/AccountServiceTests.swift @@ -20,7 +20,7 @@ class AccountServiceTests: XCTestCase { } func testCreateWordPressComAccountUUID() { - let account = accountService.createOrUpdateWordPressComAccountWithUsername("username", password: "password", authToken: "authtoken") + let account = accountService.createOrUpdateWordPressComAccountWithUsername("username", authToken: "authtoken") XCTAssertNotNil(account.uuid, "UUID should be set") } @@ -30,7 +30,7 @@ class AccountServiceTests: XCTestCase { } func testSetDefaultWordPressComAccountCheckUUID() { - let account = accountService.createOrUpdateWordPressComAccountWithUsername("username", password: "password", authToken: "authtoken") + let account = accountService.createOrUpdateWordPressComAccountWithUsername("username", authToken: "authtoken") accountService.setDefaultWordPressComAccount(account) @@ -44,7 +44,7 @@ class AccountServiceTests: XCTestCase { } func testGetDefaultWordPressComAccount() { - let account = accountService.createOrUpdateWordPressComAccountWithUsername("username", password: "password", authToken: "authtoken") + let account = accountService.createOrUpdateWordPressComAccountWithUsername("username", authToken: "authtoken") accountService.setDefaultWordPressComAccount(account) @@ -58,14 +58,14 @@ class AccountServiceTests: XCTestCase { } func testNumberOfAccountsOneAccount() { - let account = accountService.createOrUpdateWordPressComAccountWithUsername("username", password: "password", authToken: "authtoken") + let account = accountService.createOrUpdateWordPressComAccountWithUsername("username", authToken: "authtoken") XCTAssertTrue(1 == accountService.numberOfAccounts(), "There should be one account") } func testNumberOfAccountsTwoAccounts() { - let account = accountService.createOrUpdateWordPressComAccountWithUsername("username", password: "password", authToken: "authtoken") - let account2 = accountService.createOrUpdateWordPressComAccountWithUsername("username2", password: "password", authToken: "authtoken2") + let account = accountService.createOrUpdateWordPressComAccountWithUsername("username", authToken: "authtoken") + let account2 = accountService.createOrUpdateWordPressComAccountWithUsername("username2", authToken: "authtoken2") XCTAssertTrue(2 == accountService.numberOfAccounts(), "There should be two accounts") } @@ -79,7 +79,7 @@ class AccountServiceTests: XCTestCase { func testRemoveDefaultWordPressComAccountAccountSet() { accountService.removeDefaultWordPressComAccount() - let account = accountService.createOrUpdateWordPressComAccountWithUsername("username", password: "password", authToken: "authtoken") + let account = accountService.createOrUpdateWordPressComAccountWithUsername("username", authToken: "authtoken") accountService.setDefaultWordPressComAccount(account) diff --git a/WordPress/WordPressTest/BlogJetpackTest.m b/WordPress/WordPressTest/BlogJetpackTest.m index aa36cf815fbc..3a0610eafa53 100644 --- a/WordPress/WordPressTest/BlogJetpackTest.m +++ b/WordPress/WordPressTest/BlogJetpackTest.m @@ -58,7 +58,7 @@ - (void)testAssertionsOnWPcom { self.testContextManager.testExpectation = saveExpectation; AccountService *accountService = [[AccountService alloc] initWithManagedObjectContext:self.testContextManager.mainContext]; - WPAccount *wpComAccount = [accountService createOrUpdateWordPressComAccountWithUsername:@"user" password:@"pass" authToken:@"token"]; + WPAccount *wpComAccount = [accountService createOrUpdateWordPressComAccountWithUsername:@"user" authToken:@"token"]; // Wait on the merge to be completed [self waitForExpectationsWithTimeout:2.0 handler:nil]; @@ -149,14 +149,14 @@ - (void)testJetpackSetupDoesntReplaceDotcomAccount { self.testContextManager.testExpectation = saveExpectation; AccountService *accountService = [[AccountService alloc] initWithManagedObjectContext:[ContextManager sharedInstance].mainContext]; - WPAccount *wpComAccount = [accountService createOrUpdateWordPressComAccountWithUsername:@"user" password:@"pass" authToken:@"token"]; + WPAccount *wpComAccount = [accountService createOrUpdateWordPressComAccountWithUsername:@"user" authToken:@"token"]; [self waitForExpectationsWithTimeout:2.0 handler:nil]; WPAccount * defaultAccount = [accountService defaultWordPressComAccount]; XCTAssertEqualObjects(wpComAccount, defaultAccount); saveExpectation = [self expectationWithDescription:@"Context save expectation"]; self.testContextManager.testExpectation = saveExpectation; - [accountService createOrUpdateWordPressComAccountWithUsername:@"test1" password:@"test1" authToken:@"token1"]; + [accountService createOrUpdateWordPressComAccountWithUsername:@"test1" authToken:@"token1"]; [self waitForExpectationsWithTimeout:2.0 handler:nil]; defaultAccount = [accountService defaultWordPressComAccount]; XCTAssertEqualObjects(wpComAccount, defaultAccount); @@ -174,7 +174,7 @@ - (void)testWPCCShouldntDuplicateBlogs { AccountService *accountService = [[AccountService alloc] initWithManagedObjectContext:self.testContextManager.mainContext]; BlogService *blogService = [[BlogService alloc] initWithManagedObjectContext:self.testContextManager.mainContext]; - WPAccount *wpComAccount = [accountService createOrUpdateWordPressComAccountWithUsername:@"user" password:@"pass" authToken:@"token"]; + WPAccount *wpComAccount = [accountService createOrUpdateWordPressComAccountWithUsername:@"user" authToken:@"token"]; Blog *dotcomBlog = [blogService createBlogWithAccount:wpComAccount]; dotcomBlog.xmlrpc = @"http://dotcom1.wordpress.com/xmlrpc.php"; diff --git a/WordPress/WordPressTest/BlogServiceTest.m b/WordPress/WordPressTest/BlogServiceTest.m index 97d385dc6b7e..3c310c53666d 100644 --- a/WordPress/WordPressTest/BlogServiceTest.m +++ b/WordPress/WordPressTest/BlogServiceTest.m @@ -24,7 +24,7 @@ - (void)setUp self.blogService = [[BlogService alloc] initWithManagedObjectContext:[[ContextManager sharedInstance] mainContext]]; AccountService *service = [[AccountService alloc] initWithManagedObjectContext:[ContextManager sharedInstance].mainContext]; - WPAccount *account = [service createOrUpdateWordPressComAccountWithUsername:@"test" password:@"test" authToken:@"token"]; + WPAccount *account = [service createOrUpdateWordPressComAccountWithUsername:@"test" authToken:@"token"]; self.blog = (Blog *)[NSEntityDescription insertNewObjectForEntityForName:@"Blog" inManagedObjectContext:self.testContextManager.mainContext]; self.blog.xmlrpc = @"http://test.blog/xmlrpc.php"; self.blog.url = @"http://test.blog/";