Skip to content

Commit

Permalink
feat(inIOS): 支持在小程序中使用
Browse files Browse the repository at this point in the history
  • Loading branch information
fjc0k committed Jul 23, 2020
1 parent ba90ab8 commit e804fb8
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 6 deletions.
34 changes: 32 additions & 2 deletions src/utils/inIOS.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/// <reference types="miniprogram-api-typings" />

Object.defineProperty(window.navigator, 'platform', {
writable: true,
})
Expand All @@ -7,17 +9,45 @@ describe('inIOS', () => {
jest.resetModules()
})

test('不在 iOS 设备中', async () => {
test('<浏览器> 不在 iOS 设备中', async () => {
const { inIOS } = await import('./inIOS')
// @ts-ignore
window.navigator.platform = 'x'
expect(inIOS()).toBeFalse()
})

test('在 iOS 设备中', async () => {
test('<浏览器> 在 iOS 设备中', async () => {
const { inIOS } = await import('./inIOS')
// @ts-ignore
window.navigator.platform = 'xx iPhone yy'
expect(inIOS()).toBeTrue()
})

test('<小程序> 不在 iOS 设备中', async () => {
const { inIOS } = await import('./inIOS')
// @ts-ignore
window.wx = {
getSystemInfoSync() {
return {
platform: 'devtools',
system: '123',
}
},
} as Partial<WechatMiniprogram.Wx>
expect(inIOS()).toBeFalse()
})

test('<小程序> 在 iOS 设备中', async () => {
const { inIOS } = await import('./inIOS')
// @ts-ignore
window.wx = {
getSystemInfoSync() {
return {
platform: 'ios',
system: '123',
}
},
} as Partial<WechatMiniprogram.Wx>
expect(inIOS()).toBeTrue()
})
})
15 changes: 11 additions & 4 deletions src/utils/inIOS.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { inBrowser } from './inBrowser'
import { inMiniProgram } from './inMiniProgram'

let yes!: boolean

Expand All @@ -15,10 +16,16 @@ let yes!: boolean
*/
export function inIOS(): boolean {
if (yes == null) {
yes =
inBrowser() &&
typeof window.navigator === 'object' &&
/iPad|iPhone|iPod/i.test(window.navigator.platform || '')
const mp = inMiniProgram()
if (mp) {
const sysInfo = mp.getSystemInfoSync()
yes = sysInfo.platform === 'ios' || /iOS/i.test(sysInfo.system)
} else {
yes =
inBrowser() &&
typeof window.navigator === 'object' &&
/iPad|iPhone|iPod/i.test(window.navigator.platform || '')
}
}
return yes
}

0 comments on commit e804fb8

Please sign in to comment.