diff --git a/MVMCore/MVMCore.xcodeproj/project.pbxproj b/MVMCore/MVMCore.xcodeproj/project.pbxproj index 7daaf14..197bd30 100644 --- a/MVMCore/MVMCore.xcodeproj/project.pbxproj +++ b/MVMCore/MVMCore.xcodeproj/project.pbxproj @@ -940,7 +940,7 @@ FRAMEWORK_SEARCH_PATHS = "$(PROJECT_DIR)/../../SharedFrameworks"; INFOPLIST_FILE = MVMCore/Info.plist; INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks"; - IPHONEOS_DEPLOYMENT_TARGET = 9.0; + IPHONEOS_DEPLOYMENT_TARGET = 10.0; LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks"; LIBRARY_SEARCH_PATHS = ( "$(inherited)", @@ -968,7 +968,7 @@ FRAMEWORK_SEARCH_PATHS = "$(PROJECT_DIR)/../../SharedFrameworks"; INFOPLIST_FILE = MVMCore/Info.plist; INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks"; - IPHONEOS_DEPLOYMENT_TARGET = 9.0; + IPHONEOS_DEPLOYMENT_TARGET = 10.0; LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks"; LIBRARY_SEARCH_PATHS = ( "$(inherited)", diff --git a/MVMCore/MVMCore/ActionHandling/MVMCoreActionDelegateProtocol.h b/MVMCore/MVMCore/ActionHandling/MVMCoreActionDelegateProtocol.h index 0a6fd5a..35609a2 100644 --- a/MVMCore/MVMCore/ActionHandling/MVMCoreActionDelegateProtocol.h +++ b/MVMCore/MVMCore/ActionHandling/MVMCoreActionDelegateProtocol.h @@ -28,8 +28,8 @@ // Prepares to call the previous submit request again. Can overwrite for special loading. Be sure to call submit() block to perform the actual load. - (void)prepareRequestForPreviousSubmission:(nullable NSDictionary *)actionInformation additionalData:(nullable NSDictionary *)additionalData submit:(nonnull void (^)(MVMCoreRequestParameters * _Nonnull requestParameters, NSDictionary * _Nullable dataForPage))submit; -// Handles the linkaway action. Return YES if successful, NO if an error should be returned. -- (BOOL)linkedAwaySuccessfullyWithURL:(nullable NSString *)URL appURL:(nullable NSString *)appURL actionInformation:(nullable NSDictionary *)actionInformation additionalData:(nullable NSDictionary *)additionalData; +// Handles the linkaway action. Call the block to continue to linkaway. +- (void)shouldLinkAwayWithURL:(nullable NSURL *)URL appURL:(nullable NSURL *)appURL actionInformation:(nullable NSDictionary *)actionInformation additionalData:(nullable NSDictionary *)additionalData linkAwayBlock:(nonnull void (^)(NSURL * _Nullable appURL, NSURL * _Nullable URL, NSDictionary * _Nullable actionInformation, NSDictionary * _Nullable additionalData))linkAwayBlock; // Gives the delegate a chance to alter the alert object - (void)willShowPopupWithAlertObject:(nonnull MVMCoreAlertObject *)alertObject alertJson:(nonnull NSDictionary *)alertJson; diff --git a/MVMCore/MVMCore/ActionHandling/MVMCoreActionHandler.h b/MVMCore/MVMCore/ActionHandling/MVMCoreActionHandler.h index 0df0809..e3bfcbf 100644 --- a/MVMCore/MVMCore/ActionHandling/MVMCoreActionHandler.h +++ b/MVMCore/MVMCore/ActionHandling/MVMCoreActionHandler.h @@ -36,9 +36,6 @@ extern NSString * _Nonnull const KeyActionTypeOpen; // Tries to open a page - (void)openPageAction:(nullable NSDictionary *)actionInformation additionalData:(nullable NSDictionary *)additionalData delegate:(nullable NSObject *)delegate; -// Links away to app or browser -- (void)linkAwayAction:(nullable NSDictionary *)actionInformation additionalData:(nullable NSDictionary *)additionalData delegate:(nullable NSObject *)delegate; - // restarts the app - (void)restartAction:(nullable NSDictionary *)actionInformation additionalData:(nullable NSDictionary *)additionalData delegate:(nullable NSObject *)delegate; @@ -78,6 +75,20 @@ extern NSString * _Nonnull const KeyActionTypeOpen; // Handles action errors. - (void)handleActionError:(nullable MVMCoreErrorObject *)error actionInformation:(nullable NSDictionary *)actionInformation additionalData:(nullable NSDictionary *)additionalData delegate:(nullable NSObject *)delegate; +#pragma mark - Link away action + +// Links away to app or browser +- (void)linkAwayAction:(nullable NSDictionary *)actionInformation additionalData:(nullable NSDictionary *)additionalData delegate:(nullable NSObject *)delegate; + +// Can subclass to add to urls if needed at global level (delegate is also called) +- (void)prepareLinkAwayWithURL:(nullable NSURL *)url appURL:(nullable NSURL *)appURL actionInformation:(nullable NSDictionary *)actionInformation additionalData:(nullable NSDictionary *)additionalData delegate:(nullable NSObject *)delegate; + +// Opens the url +- (void)openURL:(nullable NSURL *)url appURL:(nullable NSURL *)appURL actionInformation:(nullable NSDictionary *)actionInformation additionalData:(nullable NSDictionary *)additionalData delegate:(nullable NSObject *)delegate; + +// opens the url in a webview. +- (void)openURLInWebView:(nullable NSURL *)url actionInformation:(nullable NSDictionary *)actionInformation additionalData:(nullable NSDictionary *)additionalData delegate:(nullable NSObject *)delegate; + #pragma mark - Default Action Protocol Functions // Sends the request to the load handler. diff --git a/MVMCore/MVMCore/ActionHandling/MVMCoreActionHandler.m b/MVMCore/MVMCore/ActionHandling/MVMCoreActionHandler.m index b533541..86ea2d6 100644 --- a/MVMCore/MVMCore/ActionHandling/MVMCoreActionHandler.m +++ b/MVMCore/MVMCore/ActionHandling/MVMCoreActionHandler.m @@ -27,6 +27,7 @@ #import "MVMCoreSessionObject.h" #import "MVMCoreObject.h" #import "MVMCorePresentationDelegateProtocol.h" +#import NSString * const KeyActionType = @"actionType"; NSString * const KeyActionTypeLinkAway = @"openURL"; @@ -127,25 +128,6 @@ NSString * const KeyActionTypeOpen = @"openPage"; } } -- (void)linkAwayAction:(nullable NSDictionary *)actionInformation additionalData:(nullable NSDictionary *)additionalData delegate:(nullable NSObject *)delegate { - // Linkaway to the browser or app. - NSString *appURL = [actionInformation stringForKey:KeyLinkAwayAppURL]; - NSString *otherURL = [actionInformation stringForKey:KeyLinkAwayURL]; - - BOOL successful = NO; - if ([delegate respondsToSelector:@selector(linkedAwaySuccessfullyWithURL:appURL:actionInformation:additionalData:)]) { - successful = [delegate linkedAwaySuccessfullyWithURL:otherURL appURL:appURL actionInformation:actionInformation additionalData:additionalData]; - } else { - successful = [MVMCoreActionUtility linkAway:otherURL appURLString:appURL]; - } - - // Cannot linkaway, show error. - if (!successful) { - MVMCoreErrorObject *error = error = [[MVMCoreErrorObject alloc] initWithTitle:[MVMCoreGetterUtility hardcodedStringWithKey:HardcodedErrorTitle] message:[MVMCoreGetterUtility hardcodedStringWithKey:HardcodedErrorUnableToProcess] code:ErrorCodeLinkawayFailed domain:ErrorDomainNative location:[NSString stringWithFormat:@"%@_%@",NSStringFromClass([delegate class]),KeyActionTypeLinkAway]]; - [self handleActionError:error actionInformation:actionInformation additionalData:additionalData delegate:delegate]; - } -} - - (void)restartAction:(nullable NSDictionary *)actionInformation additionalData:(nullable NSDictionary *)additionalData delegate:(nullable NSObject *)delegate { // Invalidates the session before restarting. @@ -291,6 +273,78 @@ NSString * const KeyActionTypeOpen = @"openPage"; } } +#pragma mark - open url functions + +- (void)linkAwayAction:(nullable NSDictionary *)actionInformation additionalData:(nullable NSDictionary *)additionalData delegate:(nullable NSObject *)delegate { + + // Gets the app url + NSURL *appURL = nil; + NSString *appURLString = [actionInformation string:KeyLinkAwayAppURL]; + if (appURLString.length > 0) { + appURL = [NSURL URLWithString:appURLString]; + } + + // Gets the browser url + NSURL *otherURL = nil; + NSString *otherURLString = [actionInformation string:KeyLinkAwayURL]; + if (otherURLString.length > 0) { + otherURL = [NSURL URLWithString:otherURLString]; + } + + // Provide the URL and App URL to be modified if needed by a subclass or delegate. + [self prepareLinkAwayWithURL:otherURL appURL:appURL actionInformation:actionInformation additionalData:additionalData delegate:delegate]; +} + +- (void)prepareLinkAwayWithURL:(nullable NSURL *)url appURL:(nullable NSURL *)appURL actionInformation:(nullable NSDictionary *)actionInformation additionalData:(nullable NSDictionary *)additionalData delegate:(nullable NSObject *)delegate { + + void(^openURL)(NSURL *, NSURL *, NSDictionary *, NSDictionary *) = ^(NSURL *appURL, NSURL *URL, NSDictionary *actionInformation, NSDictionary *additionalData) { + [self openURL:url appURL:appURL actionInformation:actionInformation additionalData:additionalData delegate:delegate]; + }; + + // Allow delegate to modify before opening the url. + if ([delegate respondsToSelector:@selector(shouldLinkAwayWithURL:appURL:actionInformation:additionalData:linkAwayBlock:)]) { + [delegate shouldLinkAwayWithURL:url appURL:appURL actionInformation:actionInformation additionalData:additionalData linkAwayBlock:openURL]; + } else { + openURL(appURL,url,actionInformation,additionalData); + } +} + +- (void)openURL:(nullable NSURL *)url appURL:(nullable NSURL *)appURL actionInformation:(nullable NSDictionary *)actionInformation additionalData:(nullable NSDictionary *)additionalData delegate:(nullable NSObject *)delegate { + + [MVMCoreDispatchUtility performBlockOnMainThread:^{ + + // First try to open the application. + if (appURL && [[UIApplication sharedApplication] canOpenURL:appURL]) { + [[UIApplication sharedApplication] openURL:appURL options:@{} completionHandler:NULL]; + } else if (url && [[UIApplication sharedApplication] canOpenURL:url]) { + + // Check if we should load in webview + BOOL openInWebview = [actionInformation boolForKey:@"openInWebview"]; + if (openInWebview) { + [self openURLInWebView:url actionInformation:actionInformation additionalData:additionalData delegate:delegate]; + } else { + [[UIApplication sharedApplication] openURL:url options:@{} completionHandler:NULL]; + } + } else { + [MVMCoreDispatchUtility performBlockInBackground:^{ + // Cannot linkaway, show error. + MVMCoreErrorObject *error = error = [[MVMCoreErrorObject alloc] initWithTitle:[MVMCoreGetterUtility hardcodedStringWithKey:HardcodedErrorTitle] message:[MVMCoreGetterUtility hardcodedStringWithKey:HardcodedErrorUnableToProcess] code:ErrorCodeLinkawayFailed domain:ErrorDomainNative location:[NSString stringWithFormat:@"%@_%@",NSStringFromClass([delegate class]),KeyActionTypeLinkAway]]; + [self handleActionError:error actionInformation:actionInformation additionalData:additionalData delegate:delegate]; + }]; + } + }]; +} + +- (void)openURLInWebView:(nullable NSURL *)url actionInformation:(nullable NSDictionary *)actionInformation additionalData:(nullable NSDictionary *)additionalData delegate:(nullable NSObject *)delegate { + + // Presents standard webview. + SFSafariViewController *safariViewController = [[SFSafariViewController alloc] initWithURL:url]; + //safariViewController.delegate = self; + safariViewController.preferredBarTintColor = [UIColor whiteColor]; + safariViewController.preferredControlTintColor = [UIColor blackColor]; + [[MVMCoreNavigationHandler sharedNavigationHandler] presentViewController:safariViewController animated:YES]; +} + #pragma mark - Default Action Protocol Functions + (void)defaultLogAction:(nullable NSDictionary *)actionInformation additionalData:(nullable NSDictionary *)additionalData delegate:(nullable NSObject *)delegate{