iOS URL Scheme - load custom UIView inside my application

大憨熊 提交于 2019-12-14 04:02:17

问题


I referred this IOS url Scheme launch custom UIView inside my application, but i am not clear what he s trying to answer.

Is it possible to load custom UIVIew over my app login screen through URL Scheme ?

My Scenario- I am showing custom view as a pop up in login view process, where it checks, whether app has accepted terms and conditions or not. It works fine in when app s running in background as well app as app is launched from installed app.

But when i launch app from URL Scheme section, it works fine when app s running in background, but when app get terminated, I open app from URL Schemes, App is launched but custom view gets hide immediately.

- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
 NSString *appDomain = [[NSBundle mainBundle] bundleIdentifier];
 [[NSUserDefaults standardUserDefaults] removePersistentDomainForName:appDomain];

 self.isCalledFromSafariBrowser = true;
 AppCodeViewController_iPad *appCodeViewController = [[AppCodeViewController_iPad alloc]init];
 if (!url)
 {
    return NO;
}

 NSString *URLString = [url absoluteString];
 NSArray *arr = [URLString componentsSeparatedByString:@"?"];
 arr = [[arr objectAtIndex:1] componentsSeparatedByString:@"&"];
 NSArray *usernameArray = [[arr objectAtIndex:0] componentsSeparatedByString:@"="];
 NSLog(@"user name = %@",[usernameArray objectAtIndex:1]);

NSArray *passwordArray =[[arr objectAtIndex:1] componentsSeparatedByString:@"="];
NSLog(@"password name = %@",[passwordArray objectAtIndex:1]);

NSUserDefaults * standardUserDefaults = [NSUserDefaults standardUserDefaults];
NSString * appcode = [standardUserDefaults stringForKey:@"Appcode"];
NSLog(@"app code vlaue == %@",appcode);

if (appcode == (id)[NSNull null] || appcode.length == 0 )
{
    [appCodeViewController userLogin:[usernameArray objectAtIndex:1] andPassword:[passwordArray objectAtIndex:1]];
}
else
{
    self.username =[usernameArray objectAtIndex:1];
    self.SubscriberPwd = [passwordArray objectAtIndex:1];
    [self chkPermissions];
}
return YES;
}

回答1:


You need load all resources and rootViewController in delegate method

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(nullable NSDictionary *)launchOptions 

and in this method if you opened app by url, launchOptions has a key UIApplicationLaunchOptionsURLKey. Get objects by this key and handle it like in your

- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation

Something like this

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

application.delegate.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    application.delegate.window.rootViewController = [[YourViewController alloc] init];
    [application.delegate.window makeKeyAndVisible];

    NSURL *url = launchOptions[UIApplicationLaunchOptionsURLKey];

    if (url) {
        NSString *appDomain = [[NSBundle mainBundle] bundleIdentifier];
        [[NSUserDefaults standardUserDefaults] removePersistentDomainForName:appDomain];

        self.isCalledFromSafariBrowser = true;
        AppCodeViewController_iPad *appCodeViewController = [[AppCodeViewController_iPad alloc]init];                
        NSString *URLString = [url absoluteString];
        NSArray *arr = [URLString componentsSeparatedByString:@"?"];
        arr = [[arr objectAtIndex:1] componentsSeparatedByString:@"&"];
        NSArray *usernameArray = [[arr objectAtIndex:0] componentsSeparatedByString:@"="];
        NSLog(@"user name = %@",[usernameArray objectAtIndex:1]);

        NSArray *passwordArray =[[arr objectAtIndex:1] componentsSeparatedByString:@"="];
        NSLog(@"password name = %@",[passwordArray objectAtIndex:1]);

        NSUserDefaults * standardUserDefaults = [NSUserDefaults standardUserDefaults];
        NSString * appcode = [standardUserDefaults stringForKey:@"Appcode"];
        NSLog(@"app code vlaue == %@",appcode);

        if (appcode == (id)[NSNull null] || appcode.length == 0 )
        {
            [appCodeViewController userLogin:[usernameArray objectAtIndex:1] andPassword:[passwordArray objectAtIndex:1]];
        }
        else
        {
            self.username =[usernameArray objectAtIndex:1];
            self.SubscriberPwd = [passwordArray objectAtIndex:1];
            [self chkPermissions];
        }
    }
return YES;
}

And don't forget present your custom view controller from rootViewController



来源:https://stackoverflow.com/questions/37677349/ios-url-scheme-load-custom-uiview-inside-my-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!