【问题标题】:How to make universal iPhone/iPad app WITHOUT SHARING VIEWS? [closed]如何制作没有共享视图的通用 iPhone/iPad 应用程序? [关闭]
【发布时间】:2013-08-18 19:36:07
【问题描述】:

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

【问题讨论】:

  • 在执行此操作之前,您应该确保 Apple 允许这样做。预计 iPhone 和 iPad 之间的显示/次要功能更改,但如果前。 iPhone版是时钟应用,iPad版订机票,不知道苹果会不会批准。
  • 这两个应用程序的想法相同,但它们的不同之处在于不仅仅是调整大小的问题。它们在代码上根本不同,但在功能方面,它们非常相似。

标签: iphone ios ipad ios-universal-app


【解决方案1】:

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

您应该尽快运行设备特定代码,该代码位于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 可以拥有处理通知或 URL 处理的超类 AppDelegate

【讨论】:

    【解决方案2】:

    这只是来自 Apple 的基本 universal 项目的完成启动方法:

    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á,工作完成了。

    【讨论】:

    • 请注意,如果您命名您的 XIB NameViewController~ipad.xib 和 NameViewController~iphone.xib XCode 会自动使用正确的 Xib 文件。
    • @CedricSoubrie,是的,如果是这样,那也很有用。
    猜你喜欢
    • 2012-04-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多