diff --git a/iphone/Maps/Bridging-Header.h b/iphone/Maps/Bridging-Header.h index e41abb02b..a73391cf2 100644 --- a/iphone/Maps/Bridging-Header.h +++ b/iphone/Maps/Bridging-Header.h @@ -10,6 +10,7 @@ #import +#import "Bridging.h" #import "DeepLinkRouteStrategyAdapter.h" #import "EAGLView.h" #import "FirstSession.h" diff --git a/iphone/Maps/Bridging.h b/iphone/Maps/Bridging.h new file mode 100644 index 000000000..a0a6f0f77 --- /dev/null +++ b/iphone/Maps/Bridging.h @@ -0,0 +1,7 @@ +@interface Bridging: NSObject + + ++ (void)saveOauthTokenFrom:(NSString *)oauthCode; + + +@end diff --git a/iphone/Maps/Bridging.mm b/iphone/Maps/Bridging.mm new file mode 100644 index 000000000..f5aa563b9 --- /dev/null +++ b/iphone/Maps/Bridging.mm @@ -0,0 +1,23 @@ +#import "Bridging.h" + +#include "base/logging.hpp" +#include "editor/server_api.hpp" +#include "platform/platform.hpp" +#include "private.h" + +#import "MWMAuthorizationCommon.h" + +using namespace osm; + +@implementation Bridging + + ++ (void)saveOauthTokenFrom:(NSString * _Nonnull)oauthCode +{ + NSString *oauthToken = @(OsmOAuth::ServerAuth().FinishAuthorization([oauthCode UTF8String]).c_str()); + OsmOAuth::ServerAuth().SetAuthToken([oauthToken UTF8String]); + osm_auth_ios::AuthorizationStoreCredentials([oauthToken UTF8String]); +} + + +@end diff --git a/iphone/Maps/Categories/UIKitCategories.h b/iphone/Maps/Categories/UIKitCategories.h index 3f74fa118..ba0fff9e8 100644 --- a/iphone/Maps/Categories/UIKitCategories.h +++ b/iphone/Maps/Categories/UIKitCategories.h @@ -73,6 +73,13 @@ static inline CGFloat LengthCGPoint(CGPoint point) /// @param externally: If true, try to open URL in installed application or in Safari, otherwise open in internal browser without leaving the app. - (BOOL)openUrl:(NSString *)urlString externally:(BOOL)externally; +/// Open URL externally in installed application (or in Safari if there are no appropriate application) if possible or internally in SFSafariViewController. Returns NO (false) if the url id invalid. +/// +/// @param urlString: URL string to open. +/// @param externally: If true, try to open URL in installed application or in Safari, otherwise open in internal browser without leaving the app. +/// @param skipEncoding: If true, extra URL encoding will be skipped +- (BOOL)openUrl:(NSString *)urlString externally:(BOOL)externally skipEncoding:(BOOL)skipEncoding; + @end @interface UIImage (ImageWithColor) diff --git a/iphone/Maps/Categories/UIKitCategories.m b/iphone/Maps/Categories/UIKitCategories.m index de878a689..f88358618 100644 --- a/iphone/Maps/Categories/UIKitCategories.m +++ b/iphone/Maps/Categories/UIKitCategories.m @@ -175,6 +175,11 @@ } - (BOOL)openUrl:(NSString *)urlString externally:(BOOL)externally +{ + return [self openUrl:urlString externally:externally skipEncoding:NO]; +} + +- (BOOL)openUrl:(NSString *)urlString externally:(BOOL)externally skipEncoding:(BOOL)skipEncoding { // TODO: This is a temporary workaround to open cyrillic/non-ASCII URLs. // URLs in OSM are stored in UTF-8. NSURL constructor documentation says: @@ -183,13 +188,19 @@ // 1. Split the (non-ASCII) string into components (host, path, query, fragment, etc.) // 2. Encode each component separately (they have different allowed characters). // 3. Merge them back into the string and create NSURL. - NSMutableCharacterSet * charset = [[NSMutableCharacterSet alloc] init]; - [charset formUnionWithCharacterSet:NSCharacterSet.URLHostAllowedCharacterSet]; - [charset formUnionWithCharacterSet:NSCharacterSet.URLPathAllowedCharacterSet]; - [charset formUnionWithCharacterSet:NSCharacterSet.URLQueryAllowedCharacterSet]; - [charset formUnionWithCharacterSet:NSCharacterSet.URLFragmentAllowedCharacterSet]; - [charset addCharactersInString:@"#;/?:@&=+$,"]; - NSString * encoded = [urlString stringByAddingPercentEncodingWithAllowedCharacters:charset]; + NSString * encoded; + if (skipEncoding) { + encoded = urlString; + } else { + NSMutableCharacterSet * charset = [[NSMutableCharacterSet alloc] init]; + [charset formUnionWithCharacterSet:NSCharacterSet.URLHostAllowedCharacterSet]; + [charset formUnionWithCharacterSet:NSCharacterSet.URLPathAllowedCharacterSet]; + [charset formUnionWithCharacterSet:NSCharacterSet.URLQueryAllowedCharacterSet]; + [charset formUnionWithCharacterSet:NSCharacterSet.URLFragmentAllowedCharacterSet]; + [charset addCharactersInString:@"#;/?:@&=+$,"]; + encoded = [urlString stringByAddingPercentEncodingWithAllowedCharacters:charset]; + } + // Matrix has an url with two hashes which doesn't work for NSURL and NSURLComponent. NSRange const matrixUrl = [encoded rangeOfString:@"#/#"]; if (matrixUrl.location != NSNotFound) diff --git a/iphone/Maps/Classes/CustomAlert/MWMOsmAuthAlert.mm b/iphone/Maps/Classes/CustomAlert/MWMOsmAuthAlert.mm index 6823948b8..24040228d 100644 --- a/iphone/Maps/Classes/CustomAlert/MWMOsmAuthAlert.mm +++ b/iphone/Maps/Classes/CustomAlert/MWMOsmAuthAlert.mm @@ -36,15 +36,14 @@ extern NSString * const kMap2GoogleLoginSegue; - (IBAction)osmTap { [self close:^{ - [self.alertController.ownerViewController performSegueWithIdentifier:kMap2OsmLoginSegue - sender:nil]; + [self.alertController.ownerViewController openUrl:@(osm::OsmOAuth::ServerAuth().BuildOAuth2Url().c_str()) externally:NO skipEncoding:YES]; }]; } - (IBAction)signUpTap { [self close:^{ - [self.alertController.ownerViewController openUrl:@(osm::OsmOAuth::ServerAuth().GetRegistrationURL().c_str())]; + [self.alertController.ownerViewController openUrl:@(osm::OsmOAuth::ServerAuth().GetRegistrationURL().c_str()) externally:YES]; }]; } diff --git a/iphone/Maps/Classes/CustomAlert/MWMOsmAuthAlert.xib b/iphone/Maps/Classes/CustomAlert/MWMOsmAuthAlert.xib index 124ff8b88..ab0df9b28 100644 --- a/iphone/Maps/Classes/CustomAlert/MWMOsmAuthAlert.xib +++ b/iphone/Maps/Classes/CustomAlert/MWMOsmAuthAlert.xib @@ -1,7 +1,9 @@ - + + - + + @@ -9,11 +11,11 @@ - + - + - -