如何在不分享视图的情况下制作通用iPhone / iPad应用程序?

时间:2013-08-18 19:36:07

标签: iphone ios ipad ios-universal-app

我意识到这里有很多关于制作通用应用程序的问题,但不是关于如何制作具有完全不同视图和功能的通用应用程序。我希望用户能够下载一个应用程序,但每个版本都完全不同。是唯一的方法来使用if语句,感知用户拥有的设备,然后从那里加载正确的视图控制器(即在委托中,加载正确的第一个视图控制器)? 感谢

2 个答案:

答案 0 :(得分:5)

基本上你想“在一个二进制文件中部署两个不同的应用程序”。如果您已经有两个不共享类名称的应用程序(或其他顶级对象名称),那应该非常简单。

您应该尽快运行特定于设备的代码 ,并且在main.m中。 为iPhone和iPad传递不同的应用程序委托类。其余应该正常工作,不应使用其他“设备”的类。

int main(int argc, char * argv[]) {
    @autoreleasepool {
        UIUserInterfaceIdiom idiom = [[UIDevice currentDevice] userInterfaceIdiom];
        Class appDelegateClass = Nil;
        if (idiom == UIUserInterfaceIdiomPhone) {
            appDelegateClass = [iPhoneAppDelegate class];
        }
        else if (idiom == UIUserInterfaceIdiomPad) {
            appDelegateClass = [iPadAppDelegate class];
        }
        NSCAssert(appDelegateClass, @"Unexpected idiom! Maybe iWatch?");
        return UIApplicationMain(argc, argv, nil, appDelegateClass));
    }
}

您也可以选择不同的分割点,例如分配不同的根视图控制器。

如果你想要共享一些代码,你可以创建通用超类,例如iPhoneAppDelegateiPadAppDelegate可以有超类AppDelegate来处理通知或URL处理。< / p>

答案 1 :(得分:2)

这只是Apple的基本通用项目的完成启动方法:

<强> AppDelegate.m

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
        self.viewController = [[ViewController alloc] initWithNibName:@"ViewController_iPhone" bundle:nil];
    } else {
        self.viewController = [[ViewController alloc] initWithNibName:@"ViewController_iPad" bundle:nil];
    }
    self.window.rootViewController = self.viewController;
    [self.window makeKeyAndVisible];
    return YES;
}

...但是,您可以为不同的界面习语加载不同的视图控制器:

<强> AppDelegate.m

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
        self.window.rootViewController = [[UIPhoneVersionViewController alloc] initWithNibName:@"UIPhoneVersionViewController" bundle:nil];
    } else {
        self.window.rootViewController = [[UIPadVersionViewController alloc] initWithNibName:@"UIPadVersionViewController" bundle:nil];
    }

    [self.window makeKeyAndVisible];
    return YES;
}

...和violá,工作完成了。