简体   繁体   English

使用Rails生成apple-touch-icon

[英]Generating apple-touch-icon using Rails

I am trying to add the apple-touch-icon link to my application head so that it will be displayed on homescreen bookmarks. 我正在尝试将apple-touch-icon链接添加到我的应用程序头,以便它将显示在主屏幕书签上。 The Rails guides state the following: Rails指南说明如下:

Mobile Safari looks for a different link tag, pointing to an image that will be used if you add the page to the home screen of an iOS device. Mobile Safari会查找不同的链接标记,指向将页面添加到iOS设备主屏幕时将使用的图像。 The following call would generate such a tag: 以下调用将生成这样的标记:

favicon_link_tag 'mb-icon.png', rel: 'apple-touch-icon', type: 'image/png'
<link href="/assets/mb-icon.png" rel="apple-touch-icon" type="image/png" />

However when I use the following (timestamps removed): 但是,当我使用以下(删除时间戳)时:

<%= favicon_link_tag 'favicon.ico' %>
<%= favicon_link_tag 'apple-icon.jpeg', rel: 'apple-touch-icon', type:'image/jpeg' %>

it does not correctly generate the apple-touch-icon (favicon works as expected). 它没有正确生成apple-touch-icon(favicon按预期工作)。 It inserts "images" and not "assets" into the href. 它将“图像”而不是“资产”插入到href中。 The code generated is: 生成的代码是:

<link href="/assets/favicon.ico" rel="shortcut icon" type="image/vnd.microsoft.icon">
<link href="/images/apple-icon.jpeg" rel="apple-touch-icon" type="image/jpeg">

I have also tried just putting the link right into the application file without using the TagHelper, but it doesn't append the timestamp to the file, so the file isn't correct. 我还尝试过将链接直接放入应用程序文件而不使用TagHelper,但它没有将时间戳附加到文件中,因此文件不正确。

You can create a _favicon.html.erb partial with the following content: 您可以使用以下内容创建_favicon.html.erb部分:

<% %w(76 120 152 167 180).each do |size| %>
  <%= favicon_link_tag "apple-touch-icon-#{size}x#{size}.png", rel: 'apple-touch-icon', type: 'image/png', sizes: "#{size}x#{size}" %>
<% end %>

<% %w(16 32).each do |size| %>
  <%= favicon_link_tag "favicon-#{size}x#{size}.png", rel: 'icon', type: 'image/png', sizes: "#{size}x#{size}" %>
<% end %>

Include it in your application.html.erb : 将其包含在您的application.html.erb中

<%= render 'favicon' %>

This will create apple touch icons and favicons in different sizes (optimized for different devices). 这将创建不同大小的苹果触摸图标和图标(针对不同设备进行了优化)。 You can then generate these icons via one of the many websites out there such as iconifier and place them in your app/assets/images/ directory. 然后,您可以通过诸如iconifier之类的众多网站之一生成这些图标,并将它们放置在您的app / assets / images /目录中。

This is what I've done and it works really well for me. 这就是我所做的,它对我来说非常有效。

Firstly, apple touch icons need to be in PNG format, not JPEG. 首先,苹果触摸图标需要是PNG格式,而不是JPEG格式。 They should also be named 'apple-touch-icon'. 它们也应该被命名为'apple-touch-icon'。

As for the problem, the first but not ideal solution is to place your apple icon into your public directory and hard code the link. 至于问题,第一个但不理想的解决方案是将苹果图标放入公共目录并对链接进行硬编码。

<link rel="apple-touch-icon" href="/apple-touch-icon.png" />

Alternatively on that same note you could add it /public/images so that the generated link is correct. 或者在同一个音符上添加/ public / images,以便生成的链接正确。

I suspect however that a plugin such as bootstrap is appending the link as it sees fit. 但我怀疑像bootstrap这样的插件正在附加链接,因为它认为合适。 In the case you are using bootstrap or something similar I would be searching the HAML for 'apple-touch-icon' and seeing what you can find. 在你使用bootstrap或类似东西的情况下,我会在HAML中搜索“apple-touch-icon”并查看你能找到的内容。 Most probably something along the lines of: 最有可能是:

 %link{:href => "images/apple-touch-icon.png", :rel => "apple-touch-icon"}

If you find it, remove it. 如果找到它,请将其删除。

That aside its potentially rails being too smart and realising that the filename/filetype are incorrect and appending the link. 除了它的潜在轨道太聪明,并意识到文件名/文件类型不正确并附加链接。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM