我的macos编程之旅-应用程序关联

对objective-c做了基础的入门之后,我就开始了365看图的开发。我个人偏好通过实战来掌握新知识,碰到问题后再来阅读相关的资料并解决问题。从苹果开发者网站找到了一个IKImageViewDemo示例,这个例子足够简单,也满足我要开发的365看图的基本功能。以IKImageViewDemo作为模板,365看图就有了显示、放大、缩小、旋转图片等功能。接下来需要实现一个功能:关联应用程序到图片类型。简单的说,就是在Finder中鼠标右键点击图片文件,能够选择365看图打开图片,或者将图片文件拖拽到365看图即打开。

注册文档类型

在Windows系统中,有一个叫做注册表的东东来集中管理文档类型和能够打开文档的应用程序列表,在Mac系统中,则是通过应用程序包中的info.plist。在info.plist中,声明应用程序能够处理的文档类型(通过文件名后缀)。在365看图项目中有一个ImageViewer-Info.plist的文件,内容如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "https://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
  <key>CFBundleDevelopmentRegion</key>
  <string>zh_CN</string>
    <key>CFBundleDocumentTypes</key>
    <array>
        <dict>
            <key>CFBundleTypeExtensions</key>
            <array>
                <string>jpg</string>
                <string>png</string>
            </array>
            <key>CFBundleTypeIconFile</key>
            <string></string>
            <key>CFBundleTypeRole</key>
            <string>Viewer</string>
        </dict>
    </array>
  <key>CFBundleExecutable</key>
  <string>${EXECUTABLE_NAME}</string>
  <key>CFBundleIconFile</key>
  <string></string>
  <key>CFBundleIdentifier</key>
  <string>com.mogoweb.${PRODUCT_NAME:rfc1034identifier}</string>
  <key>CFBundleInfoDictionaryVersion</key>
  <string>6.0</string>
  <key>CFBundleName</key>
  <string>${PRODUCT_NAME}</string>
  <key>CFBundlePackageType</key>
  <string>APPL</string>
  <key>CFBundleShortVersionString</key>
  <string>1.0</string>
  <key>CFBundleSignature</key>
  <string>????</string>
  <key>CFBundleVersion</key>
  <string>1</string>
  <key>LSApplicationCategoryType</key>
  <string>public.app-category.photography</string>
  <key>LSMinimumSystemVersion</key>
  <string>${MACOSX_DEPLOYMENT_TARGET}</string>
  <key>NSHumanReadableCopyright</key>
  <string>Copyright © 2014年(betway官网首页) betway官网首页. All rights reserved.</string>
  <key>NSMainNibFile</key>
  <string>MainMenu</string>
  <key>NSPrincipalClass</key>
  <string>NSApplication</string>
</dict>
</plist>

其中关键的地方在于:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<key>CFBundleDocumentTypes</key>
    <array>
        <dict>
            <key>CFBundleTypeExtensions</key>
            <array>
                <string>jpg</string>
                <string>png</string>
            </array>
            <key>CFBundleTypeIconFile</key>
            <string></string>
            <key>CFBundleTypeRole</key>
            <string>Viewer</string>
        </dict>
    </array>

指明了应用程序能够处理jpg, png为后缀的文件。

获取文件名

通过右键点击开启应用程序,app是如何得到这个文件名呢?请看下面的代码:

1
2
3
4
5
- (BOOL) application : (NSApplication*) sender openFile : (NSString*) pathname
{
    NSLog(@"application is asking to open %@", pathname);
    return YES;
}

调试技巧

通过拖拽打开应用程序如何调试(比如在application:openFile中打断点)呢?方法如下:

  1. 在Xcode中点击菜单 Product > Scheme > Edit Scheme ,在 Info 页修改 Lauch 选项为:Wait for executable to be launched
  2. 在 application:openFile 方法中设置断点。
  3. 打开 Finder, 将图片文件拖拽到您的应用程序上。[注:您可以在XCode的Project Navigator中右键点击Porducts节点下的xxx.app,然后点击Show in Finder菜单项来定位你的app可执行文件的位置]

(betway官网首页) betway官网首页