uniquejava / blog

My notes regarding the vibrating frontend :boom and the plain old java :rofl.
Creative Commons Zero v1.0 Universal
11 stars 5 forks source link

ionic2 #42

Open uniquejava opened 8 years ago

uniquejava commented 8 years ago

安装

npm install -g ionic cordova ios-sim ios-deploy

40+ Ionic Framework 2 Resources

http://mcgivery.com/15-ionic-framework-2-resources/

http://www.joshmorony.com/ionic-2-first-look-series-your-first-ionic-2-app-explained/

https://github.com/XueRainey/Ioniclub

http://ionicframework.com/docs/v2/

books

introducing ionic 2 code: https://bitbucket.org/mchauvinc/introducing-ionic-2

npm install -g ionic
ionic start hello (default template: tabbed app)
cd hello
ionic serve
##use other templates
ionic start hello blank 
ionic start hello sidemenu
## more params
ionic start hello sidemenu  --appname "awesome hello app" --id "com.moaztech.hello"

## display ios and android UI side by side
ionic serve --lab

##
ionic platform add ios
ionic emulate ios
ionic run ios

## create a page named 'search'
ionic generate page search
uniquejava commented 8 years ago

full code: git clone https://bitbucket.org/mchauvinc/introducing-ionic-2.git

➜  hello  $ ionic g --list
Available generators:
 * component
 * directive
 * page
 * pipe
 * provider
 * tabs
➜  hello  $ ionic generate page search
√ Create app/pages/search/search.html
√ Create app/pages/search/search.scss
√ Create app/pages/search/search.ts

Don't forget to add an import for search.scss in app/themes/app.core.scss:

  @import "../pages/search/search.scss";

➜  hello  $

navigation

this.nav.push(SearchPage);
this.nav.setRoot(SearchPage);

<button primary (click)="goToSearch()">Go to search</button>
<button primary [navPush]="theSearchPage">Go to search 2</button>

<button nav-pop>go back</button>

list

<ion-content padding class="search">
  <ion-list>
    <ion-item *ngFor="#result of results">
      <ion-thumbnail item-left>
        <img [src]="result.artworkUrl100" alt="">
      </ion-thumbnail>
      <h2 [innerText]="result.trackName"></h2>
      <p>{{result.artistName}}</p>
      <ion-icon item-right [name]="result.kind==='song' ? 'musical-notes' : 'film'"></ion-icon>
      <ion-icon item-right person [hidden]="result.kind !== 'song'"></ion-icon>
    </ion-item>
  </ion-list>
</ion-content>

search bar

  <ion-searchbar [(ngModel)]="keyword"
  (cancel)="userPressedCancel()"
  (keyup)="keyHasBeenPressed($event)"></ion-searchbar>
export class SearchPage {
  public results: any;
  public keyword: string;
  constructor(public nav: NavController) {
    this.results = this.getResults();
    this.keyword = '';
  }

  userPressedCancel(){
    console.debug('User pressed cancel');
    this.results = this.getResults();
    this.keyword = '';
  }

  keyHasBeenPressed(e){
    if(e.keyIdentifier === 'Enter'){
      this.results = this.getResults().filter((item) => item.trackName.toLowerCase().includes(this.keyword.toLowerCase()));
    }
  }

radio

ionic generate page settings

<ion-content padding class="settings">
  <ion-list radio-group>
    <ion-list-header *ngIf="!selectCountry">
      Pick a country
    </ion-list-header>
    <ion-list-header *ngIf="selectCountry">
      Country selected: <p>{{selectCountry.local_name}}</p>
    </ion-list-header>
    <ion-item *ngFor="#country of countries" [class.active]="selectCountry === country">
      <ion-label>{{country.name}}</ion-label>
       <ion-radio (select)="selectCountry = country" [value]="country.code"></ion-radio>
    </ion-item>
  </ion-list>
</ion-content>
export class SettingsPage {
  private countries: [any];
  constructor(public nav: NavController) {
    this.countries = [
      {
        name: 'United States',
        local_name: 'USA',
        code: 'us',
        currency: '$'
      },
      {
        name: 'United Kingdom',
        code: 'gb',
        local_name: 'The UK',
        currency: '£'
      },
      {
        name: 'Russia',
        local_name: 'Россия',
        code: 'ru'
      }
    ];
  }
}

http

app.ts

import {HTTP_PROVIDERS} from 'angular2/http';
@App({
  templateUrl: 'build/app.html',
  providers: [HTTP_PROVIDERS],
  config: {} // http://ionicframework.com/docs/v2/api/config/Config/
})

search.ts

import {Http} from 'angular2/http';
import {Inject} from 'angular2/core';
export class SearchPage {
  private http: Http;
  constructor(public nav: NavController, @Inject(Http) httpService) {
    this.http = httpService;
    this.results = this.getResults();
    this.keyword = '';
  }
  keyHasBeenPressed(e){
    if(e.keyIdentifier === 'Enter'){
      //this.results = this.getResults().filter((item) => item.trackName.toLowerCase().includes(this.keyword.toLowerCase()));
      this.http.get('https://redapesolutions.com/itunes?term=' + this.keyword)
      .subscribe((response) => {
        this.results = response.json().results;
      });
    }
  }
uniquejava commented 8 years ago

create your own service

➜  hello  $ ionic g provider itunes
√ Create app/providers/itunes/itunes.ts
uniquejava commented 7 years ago

1. 在网页中调试ionic2

Open DevTools

Cmd+Opt+I Cmd+Opt+J (同上并打开console) Cmd+Shift+C (同上并开启/关闭inspect elements模式)

在network页上可以no throttling限制网速(模拟网速慢或开启offline模式)

IOS Mode

ionic serve --platform=ios 或者在url后面加上?platform-mode=ios

Dealing with CORS issues

ionic.config.json中加入proxy

  "proxies": [
    {
      "path": "/api/forecast",
      "proxyUrl": "https://api.darksky.net/forecast/YOUR_API_KEY"
    }
  ]

或者在Chrome中禁用CORS

open -a -n /Applications/Google\ Chrome.app --args --user-data-dir="/tmp/chrome_dev_session" --disable-web-security

The -a flag opens the specified application. The -n flag will open a new instance of the application, even if one is already running. It is the two arguments that occur after the --args flag, are one that disables CORS and the other that defines a directory for any user data.

2. 在emulator中调试

启动参数

ionic emulate ios --consolelogs --serverlogs
ionic emulate android --consolelogs --serverlogs
#or the shorthand version:
ionic run ios -c -s
ionic run android -c -s

还可以加上-l (--livereload), 但是由于security方面的原因, 这个选项不一定好用.

使用Safari调试iOS (3个步骤)

  1. Safari > Preferences > Advanced, then enable Show Develop menu in menu bar.

  2. 打开iPhone手机设置app 选择Safari,找到高级选项,有JavaScript开关web检查器开关,讲两个开关都打开

  3. 手机通过USB线连上电脑.

image

使用Chrome调试Android

详见这个页面: https://developers.google.com/web/tools/chrome-devtools/remote-debugging/

  1. More Tools > Remote devices > 找到你的Device

  2. 如果没有找到device, 则在手机的设置中先关一下开发者模式, 然后重新打开一次.

  3. 在Remote devices页面上 打开或选择想要打开的tab(即页面) 点inspect, 会弹出一个新的窗口 (这个窗口会一直发白)

  4. 最后显示404的错, 原因: https://blog.csdn.net/GodnessIsMyMine/article/details/84786286

image

解决办法是 FQ, 保证chrome能上 谷歌的网站.

3. 真机调试

ionic run ios
ionic run android

这个命令能自动生成ipa和apk文件.

android

需要在系统版本号上点7次开启developer模式., 将生成的apk文件扔到百度盘或邮箱, 然后在手机上下载并安装这个apk文件. If you can see your device in the inspect devices section, but you can’t see the Cordova webview you may need to add android:debuggable="true" in the <application> node of your AndroidManifest.xml.

iOS

需要使用你的development certificate sign IPA文件.

打印app初始化时的信息

只有在app初始化完成后, remote debugging tool才能访问app, 没法看到app初始化时的日志.

解决办法: 点DevTools中Reload按钮或者在console中执行window.location.reload();, 这样会让WebView强制刷新(就能看到app初始化时的调试信息)

4. 工具

GapDebug(一键安装apk/ipa, 内置safari/chrome调试工具, 有win和mac版本) https://www.genuitec.com/products/gapdebug/

Augury (Angular2/Ionic2调试插件, 可以在DevTools中查看component tree,原名Batarangle) https://augury.angular.io/

Ripple(模拟使用cordova plugin所需的硬件环境, 比如模拟相机, 摇啊摇) http://ripple.incubator.apache.org/

VS的必装插件: Tools for Apache Cordova (TACO)

VSC的必装插件: Cordova Tools Extension for Visual Studio Code

uniquejava commented 7 years ago

升级xcode到8.3.3出现问题

2017-08-18 10:54:33.993 ios-deploy[6433:63564] [ !! ] Unable to locate DeviceSupport directory. This probably means you don't have Xcode installed, you will need to launch the app manually and logging output will not be shown! Error: Error code 253 for command: ios-deploy with args: --justlaunch,--no-wifi,-d,-b,/Users/cyper/tryndrop/snapaday3/platforms/ios/build/device/MyApp.app

解决办法: https://github.com/phonegap/ios-deploy/issues/292

cd `xcode-select --print-path`/Platforms/iPhoneOS.platform/DeviceSupport
ln -sfn "`find . -type d -maxdepth 1 -exec basename {} \; | sort -n | tail -1`" Latest