iOS URL Scheme - 在我的应用程序中加载自定义UIView

时间:2016-06-07 10:52:21

标签: ios objective-c url url-scheme

我提到了IOS url Scheme launch custom UIView inside my application,但我不清楚他想回答什么。

是否可以通过URL Scheme在我的应用登录屏幕上加载自定义UIVIew?

我的场景 - 我在登录视图流程中显示自定义视图,它会检查应用是否已接受条款和条件。当应用程序在后台运行时以及应用程序从已安装的应用程序启动时,它可以正常工作。

但是当我从URL Scheme部分启动应用程序时,它在后台运行应用程序时工作正常,但是当应用程序终止时,我从URL Schemes打开应用程序,启动应用程序但立即隐藏自定义视图。

C(n, n) = 1
C(n, 0) = 1
C(n, 1) = n

1 个答案:

答案 0 :(得分:2)

您需要在委托方法

中加载所有资源和rootViewController
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(nullable NSDictionary *)launchOptions 

并且在此方法中,如果您通过url打开app,则launchOptions具有密钥UIApplicationLaunchOptionsURLKey。通过此键获取对象并像在

中一样处理它
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation

像这样的东西

- (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;
}

并且不要忘记从rootViewController中提供自定义视图控制器